diff --git a/core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php b/core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php new file mode 100644 index 0000000000..f5222ae664 --- /dev/null +++ b/core/lib/Drupal/Core/TempStore/Element/BreakLockLink.php @@ -0,0 +1,136 @@ + 'lock', + * '#label' => $this->t('example item'), + * '#lock' => $tempstore->getMetadata('example_key'), + * '#url' => \Drupal\Core\Url::fromRoute('examples.break_lock_form'), + * ]; + * @endcode + * + * @RenderElement("break_lock_link") + */ +class BreakLockLink extends RenderElement implements ContainerFactoryPluginInterface { + + /** + * The date formatter. + * + * @var \Drupal\Core\Datetime\DateFormatterInterface + */ + protected $dateFormatter; + + /** + * The user storage. + * + * @var \Drupal\user\UserStorageInterface + */ + protected $userStorage; + + /** + * The renderer. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** + * Constructs a new BreakLockLink. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter + * The date formatter. + * @param \Drupal\user\UserStorageInterface $user_storage + * The user storage. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, UserStorageInterface $user_storage, RendererInterface $renderer) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->dateFormatter = $date_formatter; + $this->userStorage = $user_storage; + $this->renderer = $renderer; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('date.formatter'), + $container->get('entity_type.manager')->getStorage('user'), + $container->get('renderer') + ); + } + + /** + * {@inheritdoc} + */ + public function getInfo() { + return [ + '#pre_render' => [ + [$this, 'preRenderLock'], + ], + ]; + } + + /** + * Pre-render callback: Renders a lock into #markup. + * + * @param 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 array + * The passed-in element containing a rendered lock in '#markup'. + */ + 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->userStorage->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 break this lock.', [ + '@label' => $element['#label'], + '@user' => $this->renderer->render($username), + '@age' => $age, + ':url' => $element['#url']->toString(), + ]); + } + return $element; + } + +} diff --git a/core/lib/Drupal/Core/TempStore/Lock.php b/core/lib/Drupal/Core/TempStore/Lock.php index ec4321572a..9cfb684a09 100644 --- a/core/lib/Drupal/Core/TempStore/Lock.php +++ b/core/lib/Drupal/Core/TempStore/Lock.php @@ -3,7 +3,7 @@ namespace Drupal\Core\TempStore; /** - * Provides an object representing the lock from a TempStore. + * Provides a value object representing the lock from a TempStore. */ final class Lock { @@ -44,16 +44,6 @@ public function getOwnerId() { return $this->ownerId; } - /** - * Gets the owner of the lock. - * - * @return \Drupal\Core\Session\AccountInterface|null - * The owner account if one exists, NULL otherwise. - */ - public function getOwner() { - return $this->entityTypeManager()->getStorage('user')->load($this->getOwnerId()); - } - /** * Gets the timestamp of the last update to the lock. * @@ -64,36 +54,6 @@ public function getUpdated() { return $this->updated; } - /** - * Gets the age of the lock. - * - * @return string - * A string representation of the age of the lock. - */ - public function getAge() { - return $this->dateFormatter()->formatTimeDiffSince($this->getUpdated()); - } - - /** - * Wraps the date formatter. - * - * @return \Drupal\Core\Datetime\DateFormatterInterface - * The date formatter. - */ - private function dateFormatter() { - return \Drupal::service('date.formatter'); - } - - /** - * Wraps the entity type manager. - * - * @return \Drupal\Core\Entity\EntityTypeManagerInterface - * The entity type manager. - */ - private function entityTypeManager() { - return \Drupal::entityTypeManager(); - } - /** * Provides backwards compatibility for using the lock as a \stdClass object. */ diff --git a/core/modules/views_ui/src/Form/BreakLockForm.php b/core/modules/views_ui/src/Form/BreakLockForm.php index e45f07806a..1c5e0fc55f 100644 --- a/core/modules/views_ui/src/Form/BreakLockForm.php +++ b/core/modules/views_ui/src/Form/BreakLockForm.php @@ -71,7 +71,7 @@ public function getQuestion() { */ public function getDescription() { $locked = $this->tempStore->getMetadata($this->entity->id()); - $account = $locked->getOwner(); + $account = $this->entityManager->getStorage('user')->load($locked->getOwnerId()); $username = [ '#theme' => 'username', '#account' => $account, diff --git a/core/modules/views_ui/src/ViewEditForm.php b/core/modules/views_ui/src/ViewEditForm.php index 7d0fff621f..1d4434fe0d 100644 --- a/core/modules/views_ui/src/ViewEditForm.php +++ b/core/modules/views_ui/src/ViewEditForm.php @@ -128,20 +128,16 @@ public function form(array $form, FormStateInterface $form_state) { $form['#attributes']['class'] = ['form-edit']; if ($view->isLocked()) { - $username = [ - '#theme' => 'username', - '#account' => $view->getLock()->getOwner(), - ]; - $lock_message_substitutions = [ - '@user' => \Drupal::service('renderer')->render($username), - '@age' => $view->getLock()->getAge(), - ':url' => $view->toUrl('break-lock-form')->toString(), - ]; $form['locked'] = [ '#type' => 'container', '#attributes' => ['class' => ['view-locked', 'messages', 'messages--warning']], - '#children' => $this->t('This view is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to break this lock.', $lock_message_substitutions), '#weight' => -10, + 'message' => [ + '#type' => 'break_lock_link', + '#label' => $view->getEntityType()->getSingularLabel(), + '#lock' => $view->getLock(), + '#url' => $view->toUrl('break-lock-form'), + ], ]; } else {