Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active August 20, 2021 06:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sepiariver/302ab3cc7bd47233f0136f4379de89cf to your computer and use it in GitHub Desktop.
Save sepiariver/302ab3cc7bd47233f0136f4379de89cf to your computer and use it in GitHub Desktop.
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.
*
* Example:
* [[cbGetLayoutSetting? &layout=`8` &setting=`layout-setting-key`]]
*
* @var modX $modx
* @var array $scriptProperties
*/
$resource = (isset($scriptProperties['resource']) && $scriptProperties['resource'] != $modx->resource->get('id')) ? $modx->getObject('modResource', $scriptProperties['resource']) : $modx->resource;
if (!$resource) {
return '';
}
$layout = $modx->getOption('layout', $scriptProperties, '');
$setting = $modx->getOption('setting', $scriptProperties, '');
$showDebug = $modx->getOption('showDebug', $scriptProperties, false, true);
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
$debug = array('scriptProperties' => $scriptProperties, 'message' => '');
$output = '';
if (empty($layout) || empty($setting)) {
$showDebug = true;
$debug['message'] .= 'layout and setting properties are required. ';
} else {
// Make sure this is a contentblocks-enabled resource
$enabled = $resource->getProperty('_isContentBlocks', 'contentblocks');
if ($enabled !== true) return;
$debug['cbenabled'] = (bool) $enabled;
$cbcontent = $resource->getProperty('content', 'contentblocks');
$debug['cbcontent'] = $cbcontent;
$cbcontent = $modx->fromJSON($cbcontent);
if (is_array($cbcontent) && !empty($cbcontent)) {
foreach ($cbcontent as $index => $item) {
if (($item['layout'] == $layout) && isset($item['settings'][$setting])) {
$output = $item['settings'][$setting];
break;
}
}
} else {
$debug['message'] .= 'cbcontent was not an array or was empty. ';
}
if (empty($output)) {
$debug['message'] .= 'setting was not found. ';
} else {
$debug['output'] = $output;
}
//$debug['resource_props'] = $resource->get('properties');
}
if ($showDebug) $output = $modx->toJSON($debug);
if (empty($toPlaceholder)) return $output;
$modx->setPlaceholder($toPlaceholder, $output);
@Jako
Copy link

Jako commented Aug 20, 2021

Thanks, a modified snippet for getting a templated list of ContentBlocks layout settings could be found on https://gist.github.com/Jako/bebdbbac3bf1798c35605a0cfdea72ec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment