We started a new project, a website that lists products based on regions (for example US and Europe). We have all products in 1 channel (to make it dynamic to add more regions later on) and have a region field added to those entries (a checkbox field). We now want to hide products for specific users in the CP.

Through this thread I was able to add custom permissions via my plugin and it pointed me towards this event onBeforeBuildElementsQuery that I could use to add my custom filtering. I think I may need to add a custom param with the value of what the regio field needs to be to the ElementCriteriaModel.

But I have no idea how to build the next steps. I don't really understand what that ElementCriteriaModel returns. It's just a model with all basic attributes, no custom fields or anything. No clear section it targets...

The next steps should be:

  1. Is the event triggered by the CP?
  2. If so, check the logged in user's permissions and decide what regio should be filtered
  3. Add an extra param to the criteria that filters to correct region
  4. Send the changed ElementCriterioModel (or Query) to wherever it needs to go

So, how do I insert a custom parameter into that ElementCriteriaModel and will it alter the entries that are visible in the CP? Am I on the right track here?

1 Answer 1


After some digging I came up with some answers myself. For future reference, here they are:

Is the event triggered by the CP:


Check the logged in user's permissions:

$userPermissions = craft()->userPermissions->getPermissionsByUserId($user->id);

or use a permission directly:


Add an extra param to the criteria that filters to correct region:

$query = $event->params['query'];
$params = array( ':regio' => '["us","eu"]' );
$query->andWhere('content.field_regio LIKE :regio', $params);

Send the changed ElementCriterioModel (or query) to wherever it needs to go:

After setting that query, the function just executes and filters the list.

If you use something like this, make sure you filter out the correct section to check on (because the onBuildElementsQuery is fired on almost everything in the CP). But that's where I stopped my search as eventually, we will not be needing it in this project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.