Score:0

How to use hook_block_access() with custom block created in UI

gb flag

I have a custom block created using Drupal 9 Custom block library. I want to use hook_block_access() to control access to it, but, as it is a custom block, it does not have a block machine name. The only ID I can find (using Devel variables) is:

stdClass Object ( [CLASS] => Drupal\block_content\Entity\BlockContent [theme:protected] => [values:protected] => Array
( [id] => Array ( [x-default] => 4 )

        [revision_id] => Array
            (
                [x-default] => 4
            )

        [type] => Array
            (
                [x-default] => basic
            )

        [uuid] => Array
            (
                [x-default] => f0c17429-584a-4221-995c-c00f4c41c714
            )

        [langcode] => Array
            (
                [x-default] => en
            )

        [revision_user] => Array
            (
                [x-default] => 
            )

        [revision_created] => Array
            (
                [x-default] => 1632243859
            )

        [revision_log] => Array
            (
                [x-default] => 
            )

        [revision_default] => Array
            (
                [x-default] => 1
            )

        [isDefaultRevision] => Array
            (
                [x-default] => 1
            )

        [status] => Array
            (
                [x-default] => 1
            )

        [info] => Array
            (
                [x-default] => Add Content
            )

        [changed] => Array
            (
                [x-default] => 1632261438
            )

        [default_langcode] => Array
            (
                [x-default] => 1
            )

        [revision_translation_affected] => Array
            (
                [x-default] => 1
            )

        [reusable] => Array
            (
                [x-default] => 1
            )

        [body] => Array
            (
                [x-default] => Array
                    (
                        [0] => Array
                            (
                                [value] => 

etc...

I get to the block edit by going to: http://mysite.com/block/4

How can I use hook_block_access(Block $block, $operation, AccountInterface $account) to control access to this block? How do I identify this block in the $block variable?

Score:2
cn flag

After you've created the custom block in UI you can place it in different regions. Each of these instances gets a machine name and hook_block_access() works as intended.

If you want to control access on content entity level, this makes sense because you can use block content not only in block layout, implement an access hook for the entity type 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() == 4) {
      return AccessResult::forbidden();
    }
  }
  return AccessResult::neutral();
}
SomebodySysop avatar
gb flag
Discovered that hook_block_access() does not work at all when the block is placed using layout builder. hook_entity_type_access(), on the other hand, works perfectly. Thank you for that.
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.