What use function hook_entity_access In drupal 9

By toswebdev, 27 January, 2022
What use function hook_entity_access In drupal 9

use Drupal\Core\Access\AccessResult;
 * Implements hook_entity_access().
function drupalbook_examples_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
  if ($entity->getEntityTypeId() == 'node' && $entity->getType() == 'article' && $operation == 'view' && in_array('administrator', $account->getRoles())) {


There Main Three types AccessResult as Below 

  1. return AccessResult::allowed();  - This one give Almost all permission line CRUD.
  2. return AccessResult::neutral(); - This One give Only View Node or entity.
  3. return AccessResult::forbidden(); - This give you access denied.

Entity Access Is used if you want to give some specific access to any specific role at that time you can use this.


All comments go through moderation, so your comment won't display immediately.