0

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?

1 Answer 1

2

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();
}
1
  • 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. Sep 24, 2021 at 4:12

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.