3 namespace Drupal\entity;
5 use Drupal\Core\Entity\EntityTypeInterface;
8 * Provides generic entity permissions.
10 * Intended for content entity types, since config entity types usually rely
11 * on a single "administer" permission.
13 * Provided permissions:
14 * - administer $entity_type
15 * - access $entity_type overview
16 * - view own unpublished $entity_type
17 * - view ($bundle) $entity_type
18 * - update (own|any) ($bundle) $entity_type
19 * - delete (own|any) ($bundle) $entity_type
20 * - create $bundle $entity_type
22 * Does not provide "view own ($bundle) $entity_type" permissions, because
23 * they require caching pages per user. Please use
24 * \Drupal\entity\UncacheableEntityPermissionProvider if those permissions
30 * "access" = "Drupal\entity\EntityAccessControlHandler",
31 * "permission_provider" = "Drupal\entity\EntityPermissionProvider",
35 * @see \Drupal\entity\EntityAccessControlHandler
36 * @see \Drupal\entity\EntityPermissions
38 class EntityPermissionProvider extends EntityPermissionProviderBase {
41 * Builds permissions for the entity_type granularity.
43 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
49 protected function buildEntityTypePermissions(EntityTypeInterface $entity_type) {
50 $permissions = parent::buildEntityTypePermissions($entity_type);
51 $entity_type_id = $entity_type->id();
52 $plural_label = $entity_type->getPluralLabel();
54 $permissions["view {$entity_type_id}"] = [
55 'title' => $this->t('View @type', [
56 '@type' => $plural_label,
64 * Builds permissions for the bundle granularity.
66 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
72 protected function buildBundlePermissions(EntityTypeInterface $entity_type) {
73 $permissions = parent::buildBundlePermissions($entity_type);
74 $entity_type_id = $entity_type->id();
75 $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
76 $plural_label = $entity_type->getPluralLabel();
78 $permissions["view {$entity_type_id}"] = [
79 'title' => $this->t('View @type', [
80 '@type' => $plural_label,
83 foreach ($bundles as $bundle_name => $bundle_info) {
84 $permissions["view {$bundle_name} {$entity_type_id}"] = [
85 'title' => $this->t('@bundle: View @type', [
86 '@bundle' => $bundle_info['label'],
87 '@type' => $plural_label,