1

I am trying to create a solution programmatically where I can give access to edit a particular block to a user.

I know using hook_entity_access() I can grant permission to the user to access the block, But the block doesn't show the "edit" icon which I need to help the user to navigate to the edit page.

0

I found the solution for it.

to reach to the edit button hook_entity_access() is the correct Hook but to provide access to that block content, we first need access to class

Drupal\Core\Access\AccessResult

then to provide access we need to return

AccessResult::allowed()

A complete example where we might want to give access to a block_content would look like this.

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

function block_user_access_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {

  if($entity->getEntityTypeId() == 'block_content'){
    return AccessResult::allowed();
  }
}

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.