3 namespace Drupal\taxonomy;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Session\AccountInterface;
11 * Defines the access control handler for the taxonomy term entity type.
13 * @see \Drupal\taxonomy\Entity\Term
15 class TermAccessControlHandler extends EntityAccessControlHandler {
20 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
21 if ($account->hasPermission('administer taxonomy')) {
22 return AccessResult::allowed()->cachePerPermissions();
27 $access_result = AccessResult::allowedIf($account->hasPermission('access content') && $entity->isPublished())
28 ->cachePerPermissions()
29 ->addCacheableDependency($entity);
30 if (!$access_result->isAllowed()) {
31 $access_result->setReason("The 'access content' permission is required and the taxonomy term must be published.");
33 return $access_result;
36 if ($account->hasPermission("edit terms in {$entity->bundle()}")) {
37 return AccessResult::allowed()->cachePerPermissions();
40 return AccessResult::neutral()->setReason("The following permissions are required: 'edit terms in {$entity->bundle()}' OR 'administer taxonomy'.");
43 if ($account->hasPermission("delete terms in {$entity->bundle()}")) {
44 return AccessResult::allowed()->cachePerPermissions();
47 return AccessResult::neutral()->setReason("The following permissions are required: 'delete terms in {$entity->bundle()}' OR 'administer taxonomy'.");
51 return AccessResult::neutral()->cachePerPermissions();
58 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
59 return AccessResult::allowedIfHasPermissions($account, ["create terms in $entity_bundle", 'administer taxonomy'], 'OR');