Skip to content

Instantly share code, notes, and snippets.

@jonleverrier
Last active December 8, 2017 13:31
Show Gist options
  • Save jonleverrier/d4f26dbb332553b6bcff2f57c5ea79b7 to your computer and use it in GitHub Desktop.
Save jonleverrier/d4f26dbb332553b6bcff2f57c5ea79b7 to your computer and use it in GitHub Desktop.
Used as a folder selector for Content Blocks. Intended to be used inside a field setting, with the field type set to "select". If the manager user is an Admin, display a set of folders. If the manager user is a Content Editor, display a restricted set of folders
// folder selector for content blocks
// put this snippet inside a field setting with the field type of "select"
// get current resource id
$id = (!empty($id) ? $id : $modx->resource->get('id'));
// get current context key
// if $resourceid isn't a resource object, it will fallback to web
if ($resourceid = $modx->getObject('modResource', $id)) {
$currentContext = $resourceid->get('context_key');
}
else {
$currentContext = 'web';
}
// if the user is logged into the manager as an admin,
// do something...
if ($modx->user->isMember('Administrator') ) {
// trigger pdoResources to display output for admin users
// fetch resources that are folders only and are published.
// show only resources for the current context
$isAdmin = $modx->runSnippet('pdoResources', array(
'parents' => '0',
'tpl' => '@INLINE [[+id]]==[[+pagetitle]] ([[+id]])',
'limit' => '0',
'sortby' => 'pagetitle',
'sortdir' => 'ASC',
'where' => '{"isfolder:=":"1", "published:=":"1"}',
'context' => $currentContext
));
return $isAdmin;
} else {
// trigger pdoResources to display output for content editor users
// fetch resources that are folders only and are published.
// show only resources for the current context.
// ** hide folders that belong to the style guide or are restricted.
$isContentEditor = $modx->runSnippet('pdoResources', array(
'parents' => '0',
'tpl' => '@INLINE [[+id]]==[[+pagetitle]] ([[+id]])',
'limit' => '0',
'sortby' => 'pagetitle',
'sortdir' => 'ASC',
'where' => '{"isfolder:=":"1", "published:=":"1"}',
'resources' => '-82,-86,-100,-2,-24,-25,-52,-158,-37,-31,-42,-71,-109',
'context' => $currentContext
));
if (empty($isContentEditor)) {
echo "No results available";
}
else {
return $isContentEditor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment