MediaPermissions.php in Drupal 10        
                          
                  
                        
  
  
  
  
File
  core/modules/media/src/MediaPermissions.php
  
    View source  
  <?php
namespace Drupal\media;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\BundlePermissionHandlerTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaPermissions implements ContainerInjectionInterface {
  use BundlePermissionHandlerTrait;
  use StringTranslationTrait;
  
  protected $entityTypeManager;
  
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }
  
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'));
  }
  
  public function mediaTypePermissions() {
    
    $media_types = $this->entityTypeManager
      ->getStorage('media_type')
      ->loadMultiple();
    return $this
      ->generatePermissions($media_types, [
      $this,
      'buildPermissions',
    ]);
  }
  
  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),
      ],
    ];
  }
}