php 检测当前MODX资源是否在预定义的“部分”列表中,如果是,则返回字符串(例如CSS类名)。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 检测当前MODX资源是否在预定义的“部分”列表中,如果是,则返回字符串(例如CSS类名)。相关的知识,希望对你有一定的参考价值。

<?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);

以上是关于php 检测当前MODX资源是否在预定义的“部分”列表中,如果是,则返回字符串(例如CSS类名)。的主要内容,如果未能解决你的问题,请参考以下文章

php MODX所有资源都改变了模板

php 示例导出脚本,用于将MODX资源转换为CSV条目,以便在另一个站点中导入。

在 MODx 革命中,如何将容器内资源的编辑限制为用户组?

如何实现 SEF 分页(同上)- MODx?

Modx Evo / Debian 9:无法打开文件(/assets/cache//sitePublishing.idx.php)

Modx 管理器自定义页面检查用户是不是已登录并具有管理器访问权限