Implements hook_entity_operation_alter().

Fichier

modules/entity_clone_extras/entity_clone_extras.module, line 32

Code

function entity_clone_extras_entity_operation_alter(array &$operations, EntityInterface $entity) {
    // Check if entity type supports cloning:
    if ($entity->hasLinkTemplate('clone-form')) {
        // If it does, get the current user.
        $user = \Drupal::currentUser();
        $entity_types = _entity_clone_extra_supported_entity_types();
        if (in_array($entity->getEntityTypeId(), $entity_types)) {
            if (!$user->hasPermission('clone ' . $entity->getEntityTypeId() . ' entity')) {
                // If we are dealing with a supported entity type, we handle permissions
                // per bundle.
                $bundle = $entity->bundle();
                $entity_type = $entity->getEntityTypeId();
                // Check the bundle access.
                if (!$user->hasPermission("clone {$bundle} {$entity_type} entities")) {
                    // Remove the operation if the user has no access to it.
                    unset($operations['clone']);
                }
            }
        }
    }
}