function MediaHooks::entityAccess

Implements hook_entity_access().

File

core/modules/media/src/Hook/MediaHooks.php, line 104

Class

MediaHooks
Hook implementations for media.

Namespace

Drupal\media\Hook

Code

public function entityAccess(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();
}

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