BundleEntityAccessControlHandler.php in Entity API 8
File
src/BundleEntityAccessControlHandler.php
View source
<?php
namespace Drupal\entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler as CoreEntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
class BundleEntityAccessControlHandler extends CoreEntityAccessControlHandler {
protected $viewLabelOperation = TRUE;
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'view label') {
$bundle = $entity
->id();
$entity_type_id = $this->entityType
->getBundleOf();
$permissions = [
$this->entityType
->getAdminPermission() ?: "administer {$entity_type_id}",
"view {$entity_type_id}",
"view {$bundle} {$entity_type_id}",
"view own {$entity_type_id}",
"view any {$entity_type_id}",
"view own {$bundle} {$entity_type_id}",
"view any {$bundle} {$entity_type_id}",
];
return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
}
else {
return parent::checkAccess($entity, $operation, $account);
}
}
}