MODX Community

pdoResources - Show data based on Resource Groups

Hello, I know it’s possible using pdoResources to filter the results based on Resource Groups using:

checkPermissions=`load,list,view

But is it possible to display a different content instead of hide it?

Thanks!

I’ll respond this my self:

I create a snippet called getResourceGroup inside the pdoResourrces tpl :

This snippet is to compare the user permissions with the permissions of the resource that is being called trough pdoResourrces

Snippet: [[!getResourceGroup? courseId=[[+id]]]]

<?php
// Get resource ResourceGroup
$page = $modx->getObject('modResource', $courseId);
$resources = $page->getOne('ResourceGroupResources');
$resId = $resources->get('document_group');
$userGroup = $modx->GetObject('modUserGroup', array ('id'=>$resId));
$modx->setPlaceholder('resourceGroup', $userGroup->get('name'));

//Get user Permissions
$userPermission = $modx->user->isMember($userGroup->get('name'));
$modx->setPlaceholder('userPermission', $userPermission);

Save the result into a placeholder that will return “1” if the user has access to the resource.
Then use a placeholder to filter the results and display A or B based on the response.

[[!+userPermission:is=1:then=content A:else=content B]]

That is it! :grin:

Just a word of caution: Your solution may work currently on your system, but it won’t be correct in all cases.

$resId = $resources->get('document_group');
$userGroup = $modx->GetObject('modUserGroup', array ('id'=>$resId));

These 2 lines only work if a resource-group is always connected to a user-group with the same id.
If those ids get out of sync you will get wrong results.