Skip to content

Instantly share code, notes, and snippets.

View sepiariver's full-sized avatar

YJ Tso sepiariver

View GitHub Profile
@sepiariver
sepiariver / getJsonProperty.snippet.php
Last active December 21, 2017 12:32
Useful in conjunction with ContentBlocks for example to get nested field content (from a repeater)
<?php
/**
* getJsonProperty
*
* Example usage:
* [[getJsonProperty?
* &tpl=`myChunkTpl`
* &key=`0.rows.0.repeater.rows`
* &json=`[[cbGetFieldContent? &field=`28` &returnAsJSON=`1`]]`
* ]]
@sepiariver
sepiariver / cbGetLayoutSetting.snippet.php
Last active August 20, 2021 06:16
Gets a ContentBlocks Layout setting.
<?php
/**
* Use the cbGetLayoutSetting snippet to get a setting from a particular layout.
* @author YJ Tso @sepiariver <yj@modx.com>
* @param (id) &resource allows checking for fields on other resources.
* @param (bool) &showDebug return debugging info as JSON.
* @param (string) &toPlaceholder set a placeholder with this string as key, instead of returning.
* @param required (int) &layout - ID of ContentBlocks layout from which to fetch the setting.
* @param required (string) &setting - key of layout setting to return.
*
<?php
/**
* Based on @garryn Garry Nutting's amazingly fast sitemap generator:
* http://www.modx360.com/blog/2013/09/03/google-sitemap-thousands-of-resources/
*
* Modified by @sepiariver for multi-context support
* GPL, no warranties, etc.
*
*/
@sepiariver
sepiariver / getTVInputsFromJson.snippet.php
Last active December 21, 2017 12:35
Turns JSON into TV input options in MODX.
<?php
/*
* getTVInputsFromJson
* @author @sepiariver <yj@modx.com>
*
* Example usage:
* 1. Create a Chunk with this Snippet call: [[getTVInputsFromJson? &jsonFile=`http://schema.org/docs/tree.jsonld`]]
* 2. Create a Single-Select TV with @CHUNK binding for "Input Options": @CHUNK my_schema_tv_chunk
* 3. Resources with the TV enabled will get a list of schema.org itemtype property values, like this screenshot: http://sepiariver.co/1KUr4LJ
*
@sepiariver
sepiariver / SaveProcessedContent.plugin.php
Last active December 21, 2017 12:13
OnDocFormSave, process MODX Resource content and save to a TV for search indexing.
<?php
/*
* SaveProcessedContent
* @author @theboxer
* @comments @sepiariver
*
* GPL, no warranties, etc.
*
* USAGE:
* Enable 'OnDocFormSave' event in Plugin properties, and supply a valid "tvName" value.
@sepiariver
sepiariver / setResourceProps.plugin.php
Last active December 21, 2017 12:13
Sets Resource properties OnDocFormSave. To be used with getResourceProps.snippet.php
<?php
/**
*
* @author @sepiariver
*
**/
if ($modx->context->get('key') !== 'mgr' || $modx->event->name !== 'OnDocFormSave') return;
if (!($resource instanceof modResource)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'setResourceProps Plugin did not have access to a valid resource object on line: ' . __LINE__);
return;
@sepiariver
sepiariver / getResourceProps.snippet.php
Last active December 21, 2017 12:13
Gets Resource Properties and sets placeholders
<?php
/**
* getter function for resource properties. sets placeholders with all values.
* optionally allows direct return of one element within the namespaced properties sub-array
* @author @sepiariver
*
**/
// OPTIONS
$id = (int) $modx->getOption('id', $scriptProperties, 0);
@sepiariver
sepiariver / NormalizeUriSuffix.plugin.php
Last active December 21, 2017 12:17
Plugin to fix trailing slash inconsistencies in MODX Resource URIs
<?php
/**
* Steps to implement
* 1. Set HTML content-type default suffix to '/'
* 2. Ensure Resources have isFolder = 1 as default
* 3. If you still have issues with Resources being routed to the URI without a trailing slash,
* create this Plugin and enable it 'OnWebPageInit'.
**/
if ($modx->event->name !== 'OnWebPageInit') return;
if (substr($_SERVER['REQUEST_URI'], -1) !== '/') $modx->sendRedirect($_SERVER['REQUEST_URI'] . '/', array(
@sepiariver
sepiariver / FormIt.validator.isPhone.snippet.php
Created October 30, 2015 07:12
FormIt validator for phone number based on: https://regex101.com/r/xX3hO8/1
<?php
$success = false;
if (!empty($value)) {
$re = "/^((((\+[\d\-.]{1,5})?[ \-.]?\d{3})|(\+[\d\-.]{1,5})?[ \-.]?\((\d{3}\)))?[ \-.]?\d{3}[ \-.]?\d{4}\s?(e?x?t?\.?\s?\d{1,7})?)?$/i";
if (preg_match($re, $value) === 1) $success = true;
}
if (!$success) {
$validator->addError($key, 'Please enter a valid phone number.');
return false;
} else {
@sepiariver
sepiariver / cbGetFieldContent.snippet.php
Last active December 21, 2017 12:18
Modified Content Blocks snippet tries a comma separated list of fields, in order of priority, until a value is found or nothing is returned.
<?php
/**
* Use the cbGetFieldContent snippet to get the content of a particular field.
*
* For example, this can be useful if you need to get a bit of content
* in a getResources call
*
* Example usage:
*
* [[cbGetFieldContent?