You are here

class UncacheableEntityPermissionProvider in Entity API 8

Provides generic entity permissions which are cached per user.

Intended for content entity types, since config entity types usually rely on a single "administer" permission.

Provided permissions:

  • The declared "admin_permission" of the entity type (or "administer $entity_type" if the entity type does not declare an administrative permission)
  • access $entity_type overview
  • view own unpublished $entity_type
  • view (own|any) ($bundle) $entity_type
  • update (own|any) ($bundle) $entity_type
  • duplicate (own|any) ($bundle) $entity_type
  • delete (own|any) ($bundle) $entity_type
  • create $bundle $entity_type

Important: Provides "view own ($bundle) $entity_type" permissions, which require caching pages per user. This can significantly increase the size of caches, impacting site performance. Use \Drupal\entity\EntityPermissionProvider if those permissions are not necessary.

Example annotation:


 handlers = {
   "access" = "Drupal\entity\UncacheableEntityAccessControlHandler",
   "permission_provider" = "Drupal\entity\UncacheableEntityPermissionProvider",
 }

Hierarchy

Expanded class hierarchy of UncacheableEntityPermissionProvider

See also

\Drupal\entity\EntityAccessControlHandler

\Drupal\entity\EntityPermissions

2 files declare their use of UncacheableEntityPermissionProvider
UncacheableEntityAccessControlHandlerTest.php in tests/src/Unit/UncacheableEntityAccessControlHandlerTest.php
UncacheableEntityPermissionProviderTest.php in tests/src/Unit/UncacheableEntityPermissionProviderTest.php

File

src/UncacheableEntityPermissionProvider.php, line 43

Namespace

Drupal\entity
View source
class UncacheableEntityPermissionProvider extends EntityPermissionProviderBase {

  /**
   * Builds permissions for the entity_type granularity.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return array
   *   The permissions.
   */
  protected function buildEntityTypePermissions(EntityTypeInterface $entity_type) {
    $permissions = parent::buildEntityTypePermissions($entity_type);
    $entity_type_id = $entity_type
      ->id();
    $has_owner = $entity_type
      ->entityClassImplements(EntityOwnerInterface::class);
    $plural_label = $entity_type
      ->getPluralLabel();
    if ($has_owner) {
      $permissions["view any {$entity_type_id}"] = [
        'title' => $this
          ->t('View any @type', [
          '@type' => $plural_label,
        ]),
      ];
      $permissions["view own {$entity_type_id}"] = [
        'title' => $this
          ->t('View own @type', [
          '@type' => $plural_label,
        ]),
      ];
    }
    else {
      $permissions["view {$entity_type_id}"] = [
        'title' => $this
          ->t('View @type', [
          '@type' => $plural_label,
        ]),
      ];
    }
    return $permissions;
  }

  /**
   * Builds permissions for the bundle granularity.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return array
   *   The permissions.
   */
  protected function buildBundlePermissions(EntityTypeInterface $entity_type) {
    $permissions = parent::buildBundlePermissions($entity_type);
    $entity_type_id = $entity_type
      ->id();
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    $has_owner = $entity_type
      ->entityClassImplements(EntityOwnerInterface::class);
    $plural_label = $entity_type
      ->getPluralLabel();
    if ($has_owner) {
      $permissions["view any {$entity_type_id}"] = [
        'title' => $this
          ->t('View any @type', [
          '@type' => $plural_label,
        ]),
      ];
      $permissions["view own {$entity_type_id}"] = [
        'title' => $this
          ->t('View own @type', [
          '@type' => $plural_label,
        ]),
      ];
    }
    else {
      $permissions["view {$entity_type_id}"] = [
        'title' => $this
          ->t('View @type', [
          '@type' => $plural_label,
        ]),
      ];
    }
    foreach ($bundles as $bundle_name => $bundle_info) {
      if ($has_owner) {
        $permissions["view any {$bundle_name} {$entity_type_id}"] = [
          'title' => $this
            ->t('@bundle: View any @type', [
            '@bundle' => $bundle_info['label'],
            '@type' => $plural_label,
          ]),
        ];
        $permissions["view own {$bundle_name} {$entity_type_id}"] = [
          'title' => $this
            ->t('@bundle: View own @type', [
            '@bundle' => $bundle_info['label'],
            '@type' => $plural_label,
          ]),
        ];
      }
      else {
        $permissions["view {$bundle_name} {$entity_type_id}"] = [
          'title' => $this
            ->t('@bundle: View @type', [
            '@bundle' => $bundle_info['label'],
            '@type' => $plural_label,
          ]),
        ];
      }
    }
    return $permissions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPermissionProviderBase::$entityTypeBundleInfo protected property The entity type bundle info.
EntityPermissionProviderBase::buildPermissions public function Builds permissions for the given entity type. Overrides EntityPermissionProviderInterface::buildPermissions
EntityPermissionProviderBase::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
EntityPermissionProviderBase::processPermissions protected function Adds the provider and converts the titles to strings to allow sorting.
EntityPermissionProviderBase::__construct public function Constructs a new EntityPermissionProvider object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UncacheableEntityPermissionProvider::buildBundlePermissions protected function Builds permissions for the bundle granularity. Overrides EntityPermissionProviderBase::buildBundlePermissions
UncacheableEntityPermissionProvider::buildEntityTypePermissions protected function Builds permissions for the entity_type granularity. Overrides EntityPermissionProviderBase::buildEntityTypePermissions