Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / EntityPermissionProvider.php
1 <?php
2
3 namespace Drupal\entity;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6
7 /**
8  * Provides generic entity permissions.
9  *
10  * Intended for content entity types, since config entity types usually rely
11  * on a single "administer" permission.
12  *
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
21  *
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
25  * are necessary.
26  *
27  * Example annotation:
28  * @code
29  *  handlers = {
30  *    "access" = "Drupal\entity\EntityAccessControlHandler",
31  *    "permission_provider" = "Drupal\entity\EntityPermissionProvider",
32  *  }
33  * @endcode
34  *
35  * @see \Drupal\entity\EntityAccessControlHandler
36  * @see \Drupal\entity\EntityPermissions
37  */
38 class EntityPermissionProvider extends EntityPermissionProviderBase {
39
40   /**
41    * Builds permissions for the entity_type granularity.
42    *
43    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
44    *   The entity type.
45    *
46    * @return array
47    *   The permissions.
48    */
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();
53
54     $permissions["view {$entity_type_id}"] = [
55       'title' => $this->t('View @type', [
56         '@type' => $plural_label,
57       ]),
58     ];
59
60     return $permissions;
61   }
62
63   /**
64    * Builds permissions for the bundle granularity.
65    *
66    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
67    *   The entity type.
68    *
69    * @return array
70    *   The permissions.
71    */
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();
77
78     $permissions["view {$entity_type_id}"] = [
79       'title' => $this->t('View @type', [
80         '@type' => $plural_label,
81       ]),
82     ];
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,
88         ]),
89       ];
90     }
91
92     return $permissions;
93   }
94
95 }