3

Are calls to the access() method on entity objects which is provided by ContentEntityBase affected by hook_ENTITY_TYPE_access()?

I am asking because the Paragraphs module, in src/ParagraphAccessControllHandler.php, contains the following code.

protected function checkAccess(EntityInterface $paragraph, $operation, AccountInterface $account) {
// Allowed when the operation is not view or the status is true.
/** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
if ($paragraph->getParentEntity() != NULL) {
  // Delete permission on the paragraph, should just depend on 'update'
  // access permissions on the parent.
  $operation = ($operation == 'delete') ? 'update' : $operation;
  $parent_access = $paragraph->getParentEntity()->access($operation, $account, TRUE);
  return AccessResult::allowedIf($operation != 'view' || $paragraph->status->value)
    ->andIf($parent_access);
}
return AccessResult::allowedIf($operation != 'view' || $paragraph->status->value);
}

The following hook is in the Block content permissions module, in block_content_permissions.module.

function block_content_permissions_block_content_access(EntityInterface $entity, $operation, AccountInterface $account) {
  // Disallow operation access to entity based on user's permissions.
  switch ($operation) {
  case 'delete':
  case 'update':
  // Get block content type.
  $bundle_type = $entity->bundle();
  // Check permissions for block content.
  // Negate to hide operation link.
  return AccessResult::forbiddenIf(!(
    $account->hasPermission('administer block content') &&
    $account->hasPermission("$operation any $bundle_type block content")
    ));
 }
 }

By using kint(), I can tell that checkAccess() is being called, but the hook isn't running. I realize that the block content module is experimental, but theoretically this should work and is in fact working for performing actions on blocks themselves.

5
  • Yes, $entity->access() calls the entity access control handler which usually invokes the hooks. you'll have to provide some more information if something isn't working for you, including your code and so on – Berdir Mar 30 '17 at 8:08
  • As shown from @Berdir, this is a yes/no question. To make it an acceptable question, you should at least show the code you are using and/or tell us what you find in the documentation that makes you think you misunderstood something. – apaderno Mar 30 '17 at 8:21
  • @kiamlaluno Question updated with specific examples. – ummdorian Mar 30 '17 at 13:25
  • Are you logged in as an admin user? Because hook_ENTITY_TYPE_access() is not invoked when you are logged in as admin... – Robin Mar 30 '17 at 18:46
  • @Robin I was testing on a non-user-1 account, thanks for the heads up though. – ummdorian Mar 30 '17 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.