trait BundlePermissionHandlerTrait

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/BundlePermissionHandlerTrait.php \Drupal\Core\Entity\BundlePermissionHandlerTrait
  2. 10 core/lib/Drupal/Core/Entity/BundlePermissionHandlerTrait.php \Drupal\Core\Entity\BundlePermissionHandlerTrait

Provides a method to simplify generating bundle level permissions.

Hierarchy

5 files declare their use of BundlePermissionHandlerTrait
BlockContentPermissions.php in core/modules/block_content/src/BlockContentPermissions.php
BundlePermissionHandlerTraitTest.php in core/tests/Drupal/KernelTests/Core/Entity/BundlePermissionHandlerTraitTest.php
MediaPermissions.php in core/modules/media/src/MediaPermissions.php
NodePermissions.php in core/modules/node/src/NodePermissions.php
TaxonomyPermissions.php in core/modules/taxonomy/src/TaxonomyPermissions.php

File

core/lib/Drupal/Core/Entity/BundlePermissionHandlerTrait.php, line 8

Namespace

Drupal\Core\Entity
View source
trait BundlePermissionHandlerTrait {
    
    /**
     * Builds a permissions array for the supplied bundles.
     *
     * @param \Drupal\Core\Entity\EntityInterface[] $bundles
     *   An array of bundles to generate permissions for.
     * @param callable $permission_builder
     *   A callable to generate the permissions for a particular bundle. Returns
     *   an array of permissions. See PermissionHandlerInterface::getPermissions()
     *   for the array structure.
     *
     * @return array
     *   Permissions array. See PermissionHandlerInterface::getPermissions() for
     *   the array structure.
     *
     * @see \Drupal\user\PermissionHandlerInterface::getPermissions()
     */
    protected function generatePermissions(array $bundles, callable $permission_builder) {
        $permissions = [];
        foreach ($bundles as $bundle) {
            $permissions += array_map(function (array $perm) use ($bundle) {
                // This permission is generated on behalf of a bundle, therefore
                // add the bundle as a config dependency.
                $perm['dependencies'][$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName();
                return $perm;
            }, $permission_builder($bundle));
        }
        return $permissions;
    }

}

Members

Title Sort descending Modifiers Object type Summary
BundlePermissionHandlerTrait::generatePermissions protected function Builds a permissions array for the supplied bundles.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.