You are here

function content_lock_entity_operation in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 content_lock.module \content_lock_entity_operation()

Implements hook_entity_operation().

File

./content_lock.module, line 352
Content lock - Main functions of the module.

Code

function content_lock_entity_operation(EntityInterface $entity) {
  $operations = [];

  /** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
  $lock_service = \Drupal::service('content_lock');
  if ($lock_service
    ->isLockable($entity)) {
    $lock = $lock_service
      ->fetchLock($entity
      ->id(), NULL, NULL, $entity
      ->getEntityTypeId());
    $user = \Drupal::currentUser();
    if ($lock && $user
      ->hasPermission('break content lock')) {
      $entity_type = $entity
        ->getEntityTypeId();
      $route_parameters = [
        'entity' => $entity
          ->id(),
        'langcode' => $lock_service
          ->isTranslationLockEnabled($entity_type) ? $entity
          ->language()
          ->getId() : LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'form_op' => '*',
      ];
      $url = 'content_lock.break_lock.' . $entity
        ->getEntityTypeId();
      $operations['break_lock'] = [
        'title' => t('Break lock'),
        'url' => Url::fromRoute($url, $route_parameters),
        'weight' => 50,
      ];
    }
  }
  return $operations;
}