class BlockContentPermissions

Same name in other branches
  1. 10 core/modules/block_content/src/BlockContentPermissions.php \Drupal\block_content\BlockContentPermissions

Provide dynamic permissions for blocks of different types.

Hierarchy

Expanded class hierarchy of BlockContentPermissions

File

core/modules/block_content/src/BlockContentPermissions.php, line 15

Namespace

Drupal\block_content
View source
class BlockContentPermissions implements ContainerInjectionInterface {
    use StringTranslationTrait;
    use BundlePermissionHandlerTrait;
    
    /**
     * Constructs a BlockContentPermissions instance.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
     *   Entity type manager.
     */
    public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'));
    }
    
    /**
     * Build permissions for each block type.
     *
     * @return array
     *   The block type permissions.
     */
    public function blockTypePermissions() {
        return $this->generatePermissions($this->entityTypeManager
            ->getStorage('block_content_type')
            ->loadMultiple(), [
            $this,
            'buildPermissions',
        ]);
    }
    
    /**
     * Return all the permissions available for a block type.
     *
     * @param \Drupal\block_content\Entity\BlockContentType $type
     *   The block type.
     *
     * @return array
     *   Permissions available for the given block type.
     */
    protected function buildPermissions(BlockContentType $type) {
        $type_id = $type->id();
        $type_params = [
            '%type_name' => $type->label(),
        ];
        return [
            "create {$type_id} block content" => [
                'title' => $this->t('%type_name: Create new content block', $type_params),
            ],
            "edit any {$type_id} block content" => [
                'title' => $this->t('%type_name: Edit content block', $type_params),
            ],
            "delete any {$type_id} block content" => [
                'title' => $this->t('%type_name: Delete content block', $type_params),
            ],
            "view any {$type_id} block content history" => [
                'title' => $this->t('%type_name: View content block history pages', $type_params),
            ],
            "revert any {$type_id} block content revisions" => [
                'title' => $this->t('%type_name: Revert content block revisions', $type_params),
            ],
            "delete any {$type_id} block content revisions" => [
                'title' => $this->t('%type_name: Delete content block revisions', $type_params),
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BlockContentPermissions::blockTypePermissions public function Build permissions for each block type.
BlockContentPermissions::buildPermissions protected function Return all the permissions available for a block type.
BlockContentPermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
BlockContentPermissions::__construct public function Constructs a BlockContentPermissions instance.
BundlePermissionHandlerTrait::generatePermissions protected function Builds a permissions array for the supplied bundles.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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.

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