class MediaPermissions

Same name in other branches
  1. 9 core/modules/media/src/MediaPermissions.php \Drupal\media\MediaPermissions
  2. 8.9.x core/modules/media/src/MediaPermissions.php \Drupal\media\MediaPermissions
  3. 10 core/modules/media/src/MediaPermissions.php \Drupal\media\MediaPermissions

Provides dynamic permissions for each media type.

Hierarchy

Expanded class hierarchy of MediaPermissions

File

core/modules/media/src/MediaPermissions.php, line 14

Namespace

Drupal\media
View source
class MediaPermissions implements ContainerInjectionInterface {
    use BundlePermissionHandlerTrait;
    use StringTranslationTrait;
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * MediaPermissions constructor.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'));
    }
    
    /**
     * Returns an array of media type permissions.
     *
     * @return array
     *   The media type permissions.
     *
     * @see \Drupal\user\PermissionHandlerInterface::getPermissions()
     */
    public function mediaTypePermissions() {
        // Generate media permissions for all media types.
        $media_types = $this->entityTypeManager
            ->getStorage('media_type')
            ->loadMultiple();
        return $this->generatePermissions($media_types, [
            $this,
            'buildPermissions',
        ]);
    }
    
    /**
     * Returns a list of media permissions for a given media type.
     *
     * @param \Drupal\media\MediaTypeInterface $type
     *   The media type.
     *
     * @return array
     *   An associative array of permission names and descriptions.
     */
    protected function buildPermissions(MediaTypeInterface $type) {
        $type_id = $type->id();
        $type_params = [
            '%type_name' => $type->label(),
        ];
        return [
            "create {$type_id} media" => [
                'title' => $this->t('%type_name: Create new media', $type_params),
            ],
            "edit own {$type_id} media" => [
                'title' => $this->t('%type_name: Edit own media', $type_params),
            ],
            "edit any {$type_id} media" => [
                'title' => $this->t('%type_name: Edit any media', $type_params),
            ],
            "delete own {$type_id} media" => [
                'title' => $this->t('%type_name: Delete own media', $type_params),
            ],
            "delete any {$type_id} media" => [
                'title' => $this->t('%type_name: Delete any media', $type_params),
            ],
            "view any {$type_id} media revisions" => [
                'title' => $this->t('%type_name: View any media revision pages', $type_params),
            ],
            "revert any {$type_id} media revisions" => [
                'title' => $this->t('Revert %type_name: Revert media revisions', $type_params),
            ],
            "delete any {$type_id} media revisions" => [
                'title' => $this->t('Delete %type_name: Delete media revisions', $type_params),
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BundlePermissionHandlerTrait::generatePermissions protected function Builds a permissions array for the supplied bundles.
MediaPermissions::$entityTypeManager protected property The entity type manager service.
MediaPermissions::buildPermissions protected function Returns a list of media permissions for a given media type.
MediaPermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
MediaPermissions::mediaTypePermissions public function Returns an array of media type permissions.
MediaPermissions::__construct public function MediaPermissions constructor.
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.