diff --git a/core/lib/Drupal/Core/Entity/EntityListBuilder.php b/core/lib/Drupal/Core/Entity/EntityListBuilder.php index 13ae20474a..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; } /** @@ -301,26 +312,24 @@ protected function getAddLinkText() { * The HTML link or FALSE if no link is available. */ protected function getAddLink() { - if (has add-page) { - use add-page; - } - elseif (has add-form) { - use add-form + if (!$this->accessControlHandler->createAccess()) { + return FALSE; } - return FALSE; - // @todo Dependency injection. - if ($this->entityType->hasLinkTemplate('add-entity-form') && \Drupal::entityManager()->getAccessController($this->entityTypeId)->createAccess()) { + if ($this->entityType->hasLinkTemplate('add-page')) { return \Drupal::linkGenerator()->generate( $this->getAddLinkText(), - // @todo The link templates are now defined as a URI instead of a route. - $this->entityType->getLinkTemplate('add-entity-form') + Url::fromUri('internal:' . $this->entityType->getLinkTemplate('add-page')) ); } - // @todo Micro-optimization: this else statement is not needed. - else { - return FALSE; + elseif ($this->entityType->hasLinkTemplate('add-form')) { + return \Drupal::linkGenerator()->generate( + $this->getAddLinkText(), + Url::fromUri('internal:' . $this->entityType->getLinkTemplate('add-form')) + ); } + + return FALSE; } }