Last active
August 20, 2021 06:16
-
-
Save sepiariver/302ab3cc7bd47233f0136f4379de89cf to your computer and use it in GitHub Desktop.
Gets a ContentBlocks Layout setting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Use the cbGetLayoutSetting snippet to get a setting from a particular layout. | |
* @author YJ Tso @sepiariver <[email protected]> | |
* @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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, a modified snippet for getting a templated list of ContentBlocks layout settings could be found on https://gist.github.com/Jako/bebdbbac3bf1798c35605a0cfdea72ec