You are here

function media_update_8500 in Drupal 8

Introduce per-bundle permissions.

File

core/modules/media/media.install, line 181
Install, uninstall and update hooks for Media module.

Code

function media_update_8500() {
  $media_types = \Drupal::entityQuery('media_type')
    ->execute();

  /** @var \Drupal\user\RoleInterface $role */
  foreach (Role::loadMultiple() as $role) {
    if ($role
      ->hasPermission('update media')) {
      foreach ($media_types as $media_type) {
        $role
          ->grantPermission("edit own {$media_type} media");
      }
    }
    if ($role
      ->hasPermission('update any media')) {
      foreach ($media_types as $media_type) {
        $role
          ->grantPermission("edit any {$media_type} media");
      }
    }
    if ($role
      ->hasPermission('delete media')) {
      foreach ($media_types as $media_type) {
        $role
          ->grantPermission("delete own {$media_type} media");
      }
    }
    if ($role
      ->hasPermission('delete any media')) {
      foreach ($media_types as $media_type) {
        $role
          ->grantPermission("delete any {$media_type} media");
      }
    }
    if ($role
      ->hasPermission('create media')) {
      foreach ($media_types as $media_type) {
        $role
          ->grantPermission("create {$media_type} media");
      }
    }
    $role
      ->save();
  }
}