diff --git a/core/lib/Drupal/Core/Entity/EntityListBuilder.php b/core/lib/Drupal/Core/Entity/EntityListBuilder.php index 7191fae8d4..feb3d59649 100644 --- a/core/lib/Drupal/Core/Entity/EntityListBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php @@ -22,6 +22,13 @@ class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderIn */ protected $storage; + /** + * The access control handler. + * + * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface + */ + protected $accessControlHandler; + /** * The entity type ID. * @@ -52,7 +59,8 @@ class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderIn public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { return new static( $entity_type, - $container->get('entity.manager')->getStorage($entity_type->id()) + $container->get('entity.manager')->getStorage($entity_type->id()), + $container->get('entity_type.manager')->getAccessControlHandler($entity_type->id()) ); } @@ -63,11 +71,14 @@ public static function createInstance(ContainerInterface $container, EntityTypeI * The entity type definition. * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The entity storage class. + * @param \Drupal\Core\Entity\EntityAccessControlHandlerInterface $access_control_handler + * The access control handler. */ - public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) { + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityAccessControlHandlerInterface $access_control_handler) { $this->entityTypeId = $entity_type->id(); $this->storage = $storage; $this->entityType = $entity_type; + $this->accessControlHandler = $access_control_handler; } /** @@ -214,8 +225,6 @@ public function buildOperations(EntityInterface $entity) { * {@inheritdoc} * * Builds the entity listing as renderable array for table.html.twig. - * - * @todo Add a link to add a new item to the #empty text. */ public function render() { $build['table'] = [ @@ -223,7 +232,7 @@ public function render() { '#header' => $this->buildHeader(), '#title' => $this->getTitle(), '#rows' => [], - '#empty' => $this->t('There is no @label yet.', ['@label' => $this->entityType->getLabel()]), + '#empty' => $this->getEmptyText(), '#cache' => [ 'contexts' => $this->entityType->getListCacheContexts(), 'tags' => $this->entityType->getListCacheTags(), @@ -264,4 +273,63 @@ protected function ensureDestination(Url $url) { return $url->mergeOptions(['query' => $this->getRedirectDestination()->getAsArray()]); } + /** + * Returns the text to display when the list is empty. + * + * @return string + * The translated empty text. + */ + protected function getEmptyText() { + $empty_text = (string) $this->t('There is no @label yet.', [ + '@label' => $this->entityType->getLabel(), + ]); + + // Append the add link if it exists and is accessible. + $add_link = $this->getAddLink(); + if (!empty($add_link)) { + $empty_text = $empty_text . ' ' . (string) $add_link; + } + + return $empty_text; + } + + /** + * Returns the text for the "Add new entity" link. + * + * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * The translatable link text. + */ + protected function getAddLinkText() { + return $this->t('Add a new @label entity.', [ + '@label' => $this->entityType->getLabel(), + ]); + } + + /** + * Returns a link to add a new entity. + * + * @return string|false + * The HTML link or FALSE if no link is available. + */ + protected function getAddLink() { + if (!$this->accessControlHandler->createAccess()) { + return FALSE; + } + + if ($this->entityType->hasLinkTemplate('add-page')) { + return \Drupal::linkGenerator()->generate( + $this->getAddLinkText(), + Url::fromUri('internal:' . $this->entityType->getLinkTemplate('add-page')) + ); + } + elseif ($this->entityType->hasLinkTemplate('add-form')) { + return \Drupal::linkGenerator()->generate( + $this->getAddLinkText(), + Url::fromUri('internal:' . $this->entityType->getLinkTemplate('add-form')) + ); + } + + return FALSE; + } + }