Score:0

How to change the condition to AND in accessing content by role in a view-block?

br flag

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?

Score:2
in flag

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.
Steve Siddu avatar
br flag
How can I do the same with normal block which is not view block?
in flag
@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](https://www.drupal.org/node/1961370). They're a different type of plugin but operates similar to (and just as overridable as) `@ViewAccess` plugins.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.