Score:1

How to alter Layout Builder block visiblity?

us flag

Is there a hook to alter block access for blocks placed using Layout Builder? I see there is hook_block_access; but sadly this is not triggered when the block is placed using Layout Builder.

There is also work going on here: https://www.drupal.org/project/drupal/issues/2916876#comment-14279293 to allow standard block visibility settings to be made available to Layout Builder blocks; but this only allows for the defined visibilify plugins. Is this my only option, to create a custom block visiblity plugin and use it with that patch?

in flag
I believe Drupal is gradually phasing out hooks in favor of plugins and event subscribers (you can many find articles about why they're better). I wouldn't be surprised if the patch will end up not providing a hook implementation. Besides, blocks placed in Block Layout already use Condition plugins. No sense creating a whole different mechanism when that can be used instead.
liquidcms avatar
us flag
@Joseph, so any idea then what the event is that controls access to blocks placed with layout builder?
in flag
The patch you mention makes use of [Condition plugins](https://www.drupal.org/node/1961370), the same kind that are used on blocks in Block Layout. [You can build your own Condition plugins](https://drupalsun.com/2021/04/29/condition-plugins-visibility-drupal-8-9) just like the built-in ones.
berliner avatar
bd flag
@liquidcms Can you describe in a bit more detail what you want to achieve?
liquidcms avatar
us flag
@Joseph, thanks for the link and yes, i know that with the patch i mention (as LB doesn't already use Condition plugins) i could do this with a plugin - but it seems overkill as this provides an unneeded UI. A hook (or event handler) would be more efficient.
Score:1
cn flag

Is this my only option, to create a custom block visiblity plugin and use it with that patch?

No, configuring visibility of a block instance is not the only option. You can also control access on plugin level. If you add BlockPluginTrait::blockAccess to the plugin class you control all block instances of this plugin.

However, most common in Layout Builder are inline custom blocks. The plugin for these blocks implements already this method, checking access of the underlying block content entity. So in this case you can use a hook, but for block_content:

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_ENTITY_TYPE_access().
 */
function mymodule_block_content_access(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($operation == 'view') {
    if ($entity->id() == 123) {
      return AccessResult::forbidden();
    }
  }
  return AccessResult::neutral();
}
liquidcms avatar
us flag
i have never found this: "most common in Layout Builder are inline custom blocks" to be the case. Anyway, i didnt think necessary but i should have mentioned this is a Views block. And for that, the block_content_access hook is not used (but good info to know). Still looking.
4uk4 avatar
cn flag
This is a main feature of the Layout Builder UI that you can create non reusable blocks on the fly. BTW the Views block plugin class implements `blockAccess()` checking the access of the View. So you can configure access there or even write your own Views access plugin if you don't find the access options you are looking for.
liquidcms avatar
us flag
Yes, I could write a plugin which is more ui that is not required. I think i likely described the question incorrectly. What i have is 2 (view) blocks which display or not based on contents. I did manage to accomplish what i was doing with getting rid of one of the blocks and incorporating the condition part within the View using twig. Thanks for the help. Sorry i didnt explain well enough.
4uk4 avatar
cn flag
There is nothing wrong with the original question, otherwise I wouldn't have answered it. But you couldn't expect that it would get this specific. You can apply my answer to this specific task, too. Embed the two views in a block and put the condition part in blockAccess().
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.