Skip to content

Instantly share code, notes, and snippets.

@Jako
Last active August 20, 2021 08:46
Show Gist options
  • Save Jako/bebdbbac3bf1798c35605a0cfdea72ec to your computer and use it in GitHub Desktop.
Save Jako/bebdbbac3bf1798c35605a0cfdea72ec to your computer and use it in GitHub Desktop.
Get a templated list of ContentBlocks layout settings
<?php
/**
* Use the cbGetLayoutSettings snippet to get a templated list of settings from a particular layout.
* Based on cbGetLayoutSetting snippet https://gist.github.com/sepiariver/302ab3cc7bd47233f0136f4379de89cf
*
* @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 (int) &layout - (required) ID of ContentBlocks layout from which to fetch the settings.
* @param (string) &tpl - name of chunk for templating the settings of one layout.
* @param (int) &limit - limits the number of settings returned.
* @param (int) &offset - offset of the returned settings to skip.
*
* Example:
* [[cbGetLayoutSettings? &layout=`8` &tpl=`chunkName`]]
*
* @author YJ Tso @sepiariver <[email protected]>
* @author Thomas Jakobi @Jako <[email protected]>
*
* @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 = (int)$modx->getOption('layout', $scriptProperties, '');
$tpl = $modx->getOption('tpl', $scriptProperties, '');
$showDebug = (bool)$modx->getOption('showDebug', $scriptProperties, false, true);
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
$limit = (int)$modx->getOption('limit', $scriptProperties, 0);
$offset = (int)$modx->getOption('offset', $scriptProperties, 0);
$_cache = array();
$debug = array('scriptProperties' => $scriptProperties, 'message' => array());
$output = [];
if (empty($layout)) {
$showDebug = true;
$debug['message'][] = 'layout property is required.';
} else {
// Make sure this is a contentblocks-enabled resource
$enabled = $resource->getProperty('_isContentBlocks', 'contentblocks');
if ($enabled !== true) {
$debug['message'][] = 'the ressource is not a contentblocks resource.';
return;
} else {
$debug['cbenabled'] = $enabled;
$cbcontent = $resource->getProperty('content', 'contentblocks');
$debug['cbcontentsettings'] = array();
$cbcontent = json_decode($cbcontent, true);
if (is_array($cbcontent) && !empty($cbcontent)) {
foreach ($cbcontent as $index => $item) {
if ($item['layout'] == $layout) {
$debug['cbcontentsettings'] = $item['settings'];
if (isset($item['settings'])) {
if (!empty($tpl)) {
$chunk = null;
if (!isset($_cache['@CHUNK'])) {
$_cache['@CHUNK'] = array();
}
if (!array_key_exists($tpl, $_cache['@CHUNK'])) {
if ($chunk = $modx->getObject('modChunk', array('name' => $tpl))) {
$_cache['@CHUNK'][$tpl] = $chunk->toArray('', true);
} else {
$_cache['@CHUNK'][$tpl] = false;
}
} elseif (is_array($_cache['@CHUNK'][$tpl])) {
$chunk = $modx->newObject('modChunk');
$chunk->fromArray($_cache['@CHUNK'][$tpl], '', true, true, true);
}
if (is_object($chunk)) {
$chunk->setCacheable(false);
$output[] = $chunk->process($item['settings']);
} else {
$output[] = print_r($item['settings'], true);
$debug['message'][] = '"' . $tpl . '" is not a valid chunkname.';
}
} else {
$output[] = print_r($item['settings'], true);
}
} else {
$debug['message'][] = 'cbcontent was not an array or was empty.';
}
}
}
} else {
$debug['message'][] = 'cbcontent was not an array or was empty.';
}
}
if (empty($output)) {
$debug['message'][] = 'layout was not found.';
} else {
$debug['output'] = $output;
}
}
if ($showDebug) {
$output = json_encode($debug);
} else {
if ($offset) {
if ($limit) {
$output = array_slice($output, $offset, $limit);
} else {
$output = array_slice($output, $offset);
}
}
$output = implode("\n", $output);
}
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