function BreakLockLink::preRenderLock

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php \Drupal\Core\TempStore\Element\BreakLockLink::preRenderLock()
  2. 8.9.x core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php \Drupal\Core\TempStore\Element\BreakLockLink::preRenderLock()
  3. 11.x core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php \Drupal\Core\TempStore\Element\BreakLockLink::preRenderLock()

Pre-render callback: Renders a lock into #markup.

Parameters

array $element: A structured array with the following keys:

  • #label: The label of the object that is locked.
  • #lock: The lock object.
  • #url: The URL object with the destination to the break lock form.

Return value

array The passed-in element containing a rendered lock in '#markup'.

File

core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php, line 116

Class

BreakLockLink
Provides a link to break a tempstore lock.

Namespace

Drupal\Core\TempStore\Element

Code

public function preRenderLock($element) {
  if (isset($element['#lock']) && isset($element['#label']) && isset($element['#url'])) {
    /** @var \Drupal\Core\TempStore\Lock $lock */
    $lock = $element['#lock'];
    $age = $this->dateFormatter
      ->formatTimeDiffSince($lock->getUpdated());
    $owner = $this->entityTypeManager
      ->getStorage('user')
      ->load($lock->getOwnerId());
    $username = [
      '#theme' => 'username',
      '#account' => $owner,
    ];
    $element['#markup'] = $this->t('This @label is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href=":url">break this lock</a>.', [
      '@label' => $element['#label'],
      '@user' => $this->renderer
        ->render($username),
      '@age' => $age,
      ':url' => $element['#url']->toString(),
    ]);
  }
  return $element;
}

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