2

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?

5
  • 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.
    – Joseph
    Commented Nov 8, 2021 at 20:22
  • @Joseph, so any idea then what the event is that controls access to blocks placed with layout builder?
    – liquidcms
    Commented Nov 8, 2021 at 21:41
  • The patch you mention makes use of Condition plugins, the same kind that are used on blocks in Block Layout. You can build your own Condition plugins just like the built-in ones.
    – Joseph
    Commented Nov 8, 2021 at 23:19
  • @liquidcms Can you describe in a bit more detail what you want to achieve?
    – berliner
    Commented Nov 9, 2021 at 9:32
  • @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.
    – liquidcms
    Commented Nov 9, 2021 at 14:40

1 Answer 1

1

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();
}
4
  • 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.
    – liquidcms
    Commented Nov 9, 2021 at 14:42
  • 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.
    – 4uk4
    Commented Nov 9, 2021 at 15:14
  • 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.
    – liquidcms
    Commented Nov 9, 2021 at 15:48
  • 1
    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().
    – 4uk4
    Commented Nov 9, 2021 at 16:06

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.