Last active
December 21, 2017 12:24
-
-
Save sepiariver/07d64afea211016a7d7bc3a4f3e5d770 to your computer and use it in GitHub Desktop.
Detects if the current MODX Resource is in a predefined list of "sections", and if so returns a string (e.g. a CSS classname).
This file contains hidden or 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 | |
// The Resource ID to test | |
$id = (int) $modx->getOption('id', $scriptProperties, $modx->resource->get('id'), true); | |
// Option to return early if Current Resource matches $id | |
$matchCurrent = $modx->getOption('matchCurrent', $scriptProperties, true); | |
// IDs of Resources deemed as "sections", against which to test | |
$sectionIds = array_filter(array_map('trim', explode(',', $modx->getOption('sectionIds', $scriptProperties, '')))); | |
// IDs of parent Resources, the direct children of which will be added to $sectionIds | |
$sectionParentIds = array_filter(array_map('trim', explode(',', $modx->getOption('sectionParentIds', $scriptProperties, '')))); | |
// If the test passes, meaning the Resource has a $sectionId as an ancestor, output this | |
$inSectionOutput = $modx->getOption('inSectionOutput', $scriptProperties, 'active'); | |
// Otherwise output this | |
$defaultOutput = $modx->getOption('defaultOutput', $scriptProperties, ''); | |
// Handy | |
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, ''); | |
// Current Resource | |
$current = (int) $modx->resource->get('id'); | |
// Return early if matching enabled and matched | |
if ($matchCurrent && ($current === $id)) return $inSectionOutput; | |
// Loop through sectionParentIds | |
foreach ($sectionParentIds as $spi) { | |
$sectionIds = array_merge($sectionIds, $modx->getChildIds($spi, 1)); | |
} | |
$sectionIds = array_unique($sectionIds); | |
// We need the parents of the current Resource | |
$pids = $modx->getParentIds($current); | |
// Store interecting values between the parent IDs and section IDs | |
$matches = array_intersect($sectionIds, $pids); | |
//var_dump($id, $current, $pids, $sections, $matches); | |
// If ID to test is in the array of the intersection of sectionIds and parentIds, we're a match | |
// Set output value | |
$output = (in_array($id, $matches)) ? $inSectionOutput : $defaultOutput; | |
// 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