You are here

function media_entity_access in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/media.module \media_entity_access()
  2. 10 core/modules/media/media.module \media_entity_access()

Implements hook_entity_access().

File

core/modules/media/media.module, line 96
Provides media items.

Code

function media_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($operation === 'delete' && $entity instanceof FieldConfigInterface && $entity
    ->getTargetEntityTypeId() === 'media') {

    /** @var \Drupal\media\MediaTypeInterface $media_type */
    $media_type = \Drupal::entityTypeManager()
      ->getStorage('media_type')
      ->load($entity
      ->getTargetBundle());
    return AccessResult::forbiddenIf($entity
      ->id() === 'media.' . $media_type
      ->id() . '.' . $media_type
      ->getSource()
      ->getConfiguration()['source_field']);
  }
  return AccessResult::neutral();
}