I have a view-block 'Expo', I want to display this block only if current user has two specific user roles i.e., 'Actor' and 'Singer'.

When I checked the both 'Actor' and 'Singer' , The block is displaying in users who have either 'Actor' or 'Singer' user role. When the user has two user roles it's displaying two blocks.

As per my understanding, It's using conditional OR in between the checked user roles. How can I change it to conditional AND?


Yes, the Role access plugin is written in a way that its access method works effectively like an OR:

public function access(AccountInterface $account) {
  return array_intersect(array_filter($this->options['role']), $account->getRoles());

To change this behavior, you can do it in two ways:

  • Creating a new ViewsAccess plugin that mostly works like Role but operates with an AND. To do this, extend the Role class, override the access method, and then declare this class as a new @ViewsAccess plugin (remember to give it a unique ID and a new name). This new plugin will show up beside the existing Role plugin.

  • Alternatively, you can add an option on the existing Role plugin to make it behave like an AND when told to. To do this, you extend the existing Role class, override buildOptionsForm method to add the option, override the access method to only work like an AND if it's configured to, then alter the existing Role plugin's class to point to your extended version instead of the one from Views.

    • If you're familiar with working with form alters and view alters to modify forms and how things render based on the form values, this approach is almost similar.
  • How can I do the same with normal block which is not view block? Nov 2 '21 at 5:27
  • @SteveSiddu Remember the part in Block Layout where you limit a block's visibility by Role, Content Type, and/or Path? Those are @Condition plugins. They're a different type of plugin but operates similar to (and just as overridable as) @ViewAccess plugins.
    – Joseph
    Nov 2 '21 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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