You are here

public function ContentLock::locking in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 src/ContentLock/ContentLock.php \Drupal\content_lock\ContentLock\ContentLock::locking()

Try to lock a document for editing.

Parameters

int $entity_id: The entity id.

string $langcode: The translation language of the entity.

string $form_op: The entity form operation.

int $uid: The user id to lock the node for.

string $entity_type: The entity type.

bool $quiet: Suppress any normal user messages.

string $destination: Destination to redirect when break. Defaults to current page.

Return value

bool FALSE, if a document has already been locked by someone else.

Throws

\InvalidArgumentException An exception will be thrown if the

File

src/ContentLock/ContentLock.php, line 400

Class

ContentLock
Class ContentLock.

Namespace

Drupal\content_lock\ContentLock

Code

public function locking($entity_id, $langcode, $form_op, $uid, $entity_type = 'node', $quiet = FALSE, $destination = NULL) {
  $translation_lock = $this
    ->isTranslationLockEnabled($entity_type);
  if (!$translation_lock) {
    $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  }
  if (!$this
    ->isFormOperationLockEnabled($entity_type)) {
    $form_op = '*';
  }

  // Check locking status.
  $lock = $this
    ->fetchLock($entity_id, $langcode, $form_op, $entity_type);

  // No lock yet.
  if ($lock === FALSE || !is_object($lock)) {

    // Save locking into database.
    $this
      ->lockingSave($entity_id, $langcode, $form_op, $uid, $entity_type);
    if ($this
      ->verbose() && !$quiet) {
      if ($translation_lock) {
        $this->messenger
          ->addStatus($this
          ->t('This content translation is now locked against simultaneous editing. This content translation will remain locked if you navigate away from this page without saving or unlocking it.'));
      }
      else {
        $this->messenger
          ->addStatus($this
          ->t('This content is now locked against simultaneous editing. This content will remain locked if you navigate away from this page without saving or unlocking it.'));
      }
    }

    // Post locking hook.
    $this->moduleHandler
      ->invokeAll('content_lock_locked', [
      $entity_id,
      $langcode,
      $form_op,
      $uid,
      $entity_type,
    ]);

    // Send success flag.
    return TRUE;
  }
  else {

    // Currently locking by other user.
    if ($lock->uid != $uid) {

      // Send message.
      $message = $this
        ->displayLockOwner($lock, $translation_lock);
      $this->messenger
        ->addWarning($message);

      // Higher permission user can unblock.
      if ($this->currentUser
        ->hasPermission('break content lock')) {
        $link = Link::createFromRoute($this
          ->t('Break lock'), 'content_lock.break_lock.' . $entity_type, [
          'entity' => $entity_id,
          'langcode' => $langcode,
          'form_op' => $form_op,
        ], [
          'query' => [
            'destination' => isset($destination) ? $destination : $this->currentRequest
              ->getRequestUri(),
          ],
        ])
          ->toString();

        // Let user break lock.
        $this->messenger
          ->addWarning($this
          ->t('Click here to @link', [
          '@link' => $link,
        ]));
      }

      // Return FALSE flag.
      return FALSE;
    }
    else {

      // Save locking into database.
      $this
        ->lockingSave($entity_id, $langcode, $form_op, $uid, $entity_type);

      // Locked by current user.
      if ($this
        ->verbose() && !$quiet) {
        if ($translation_lock) {
          $this->messenger
            ->addStatus($this
            ->t('This content translation is now locked by you against simultaneous editing. This content translation will remain locked if you navigate away from this page without saving or unlocking it.'));
        }
        else {
          $this->messenger
            ->addStatus($this
            ->t('This content is now locked by you against simultaneous editing. This content will remain locked if you navigate away from this page without saving or unlocking it.'));
        }
      }

      // Send success flag.
      return TRUE;
    }
  }
}