diff --git a/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php index 6bc8a23..e6608d7 100644 --- a/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityAccessControllerInterface.php @@ -45,8 +45,8 @@ public function access(EntityInterface $entity, $operation, $langcode = Language * Checks access to create an entity. * * @param string $entity_bundle - * (optional) The bundle of the entity. Required if the entity supports - * bundles, defaults to NULL otherwise. + * (optional) The bundle of the entity. If no bundle has been given, access + * is checked for any available bundle. * @param \Drupal\Core\Session\AccountInterface $account * (optional) The user session for which to check access, or NULL to check * access for the current user. Defaults to NULL. diff --git a/core/lib/Drupal/Core/Entity/EntityListBuilder.php b/core/lib/Drupal/Core/Entity/EntityListBuilder.php index dcde281..860c5b2 100644 --- a/core/lib/Drupal/Core/Entity/EntityListBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php @@ -8,15 +8,25 @@ namespace Drupal\Core\Entity; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Utility\LinkGeneratorInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Component\Utility\String; -use Drupal\Core\Url; /** * Defines a generic implementation to build a listing of entities. */ class EntityListBuilder extends EntityControllerBase implements EntityListBuilderInterface, EntityControllerInterface { + use StringTranslationTrait; + + /** + * The entity access controller. + * + * @var \Drupal\Core\Entity\EntityAccessControllerInterface + */ + protected $entityAccess; + /** * The entity storage class. * @@ -39,12 +49,24 @@ class EntityListBuilder extends EntityControllerBase implements EntityListBuilde protected $entityType; /** + * The link generator. + * + * @var \Drupal\Core\Utility\LinkGeneratorInterface + */ + protected $linkGenerator; + + /** * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { + /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */ + $entity_manager = $container->get('entity.manager'); + return new static( $entity_type, - $container->get('entity.manager')->getStorage($entity_type->id()) + $entity_manager->getStorage($entity_type->id()), + $entity_manager->getAccessController($entity_type->id()), + $container->get('link_generator') ); } @@ -55,11 +77,17 @@ 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\EntityAccessControllerInterface $entity_access + * The entity access controller. + * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator + * The link generator. */ - public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage) { + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityAccessControllerInterface $entity_access, LinkGeneratorInterface $link_generator) { + $this->entityAccess = $entity_access; $this->entityTypeId = $entity_type->id(); $this->storage = $storage; $this->entityType = $entity_type; + $this->linkGenerator = $link_generator; } /** @@ -182,16 +210,19 @@ public function buildOperations(EntityInterface $entity) { * {@inheritdoc} * * Builds the entity listing as renderable array for theme_table(). - * - * @todo Add a link to add a new item to the #empty text. */ public function render() { + $empty_text = $this->getEmptyText(); + $add_link = $this->createAddLink(); + if ($add_link) { + $empty_text .= ' ' . $add_link; + } $build = array( '#type' => 'table', '#header' => $this->buildHeader(), '#title' => $this->getTitle(), '#rows' => array(), - '#empty' => $this->t('There is no @label yet. !add_link', array('@label' => $this->entityType->getLabel(), '!add_link' => $this->createAddLink())), + '#empty' => $empty_text, ); foreach ($this->load() as $entity) { if ($row = $this->buildRow($entity)) { @@ -202,30 +233,43 @@ public function render() { } /** - * Creates link to add entity of current entity type. + * Gets the text to display when the list is empty. * - * @return string HTML anchor element + * @return string */ - protected function createAddLink() { - $add_link = ''; - if ($link_template = $this->entityType->getLinkTemplate('add-entity-form')) { - $url = new Url($link_template); - $add_link = \Drupal::linkGenerator()->generateFromUrl( - $this->t('Add @label.', array('@label' => $this->entityType->getLabel())), - $url - ); - } + protected function getEmptyText() { + return $this->t('There is no @label yet.', array( + '@label' => $this->entityType->getLabel(), + )); + } - return $add_link; + /** + * Gets the text for the "Add new entity" link. + * + * @return string + */ + protected function getAddLinkText() { + return $this->t('Add a new @label.', array( + '@label' => $this->entityType->getLabel(), + )); } /** - * Translates a string to the current language or to a given language. + * Creates a link to add a new entity. * - * See the t() documentation for details. + * @return string|false + * The HTML link or FALSE if no link is available. */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager()->translate($string, $args, $options); + protected function createAddLink() { + if ($this->entityType->hasLinkTemplate('add') && $this->entityAccess->createAccess()) { + return $this->linkGenerator->generate( + $this->getAddLinkText(), + $this->entityType->getLinkTemplate('add') + ); + } + else { + return FALSE; + } } /** diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php index dab73c6..cfddf58 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php @@ -40,7 +40,7 @@ * "delete-form" = "custom_block.delete", * "edit-form" = "custom_block.edit", * "admin-form" = "custom_block.type_edit", - * "add-entity-form" = "custom_block.add_page" + * "add" = "custom_block.add_page" * }, * fieldable = TRUE, * translatable = TRUE, diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php index 013ea95..8fae07c 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlockType.php @@ -36,7 +36,7 @@ * links = { * "delete-form" = "custom_block.type_delete", * "edit-form" = "custom_block.type_edit", - * "add-entity-form" = "custom_block.type_add" + * "add" = "custom_block.type_add" * } * ) */ diff --git a/core/modules/image/lib/Drupal/image/Entity/ImageStyle.php b/core/modules/image/lib/Drupal/image/Entity/ImageStyle.php index 034b406..4ce771c 100644 --- a/core/modules/image/lib/Drupal/image/Entity/ImageStyle.php +++ b/core/modules/image/lib/Drupal/image/Entity/ImageStyle.php @@ -44,7 +44,7 @@ * "flush-form" = "image.style_flush", * "edit-form" = "image.style_edit", * "delete-form" = "image.style_delete", - * "add-entity-form" = "image.style_add" + * "add" = "image.style_add" * } * ) */ diff --git a/core/modules/node/lib/Drupal/node/Entity/Node.php b/core/modules/node/lib/Drupal/node/Entity/Node.php index 060dcca..cea4e7f 100644 --- a/core/modules/node/lib/Drupal/node/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Entity/Node.php @@ -56,7 +56,7 @@ * "edit-form" = "node.page_edit", * "version-history" = "node.revision_overview", * "admin-form" = "node.type_edit", - * "add-entity-form" = "node.add_page" + * "add" = "node.add_page" * } * ) */ diff --git a/core/modules/node/lib/Drupal/node/Entity/NodeType.php b/core/modules/node/lib/Drupal/node/Entity/NodeType.php index 3640b5b..01f7e47 100644 --- a/core/modules/node/lib/Drupal/node/Entity/NodeType.php +++ b/core/modules/node/lib/Drupal/node/Entity/NodeType.php @@ -38,7 +38,7 @@ * "add-form" = "node.add", * "edit-form" = "node.type_edit", * "delete-form" = "node.type_delete_confirm", - * "add-entity-form" = "node.type_add" + * "add" = "node.type_add" * } * ) */ diff --git a/core/modules/node/lib/Drupal/node/NodeListBuilder.php b/core/modules/node/lib/Drupal/node/NodeListBuilder.php index fc9c0ba..2a30077 100644 --- a/core/modules/node/lib/Drupal/node/NodeListBuilder.php +++ b/core/modules/node/lib/Drupal/node/NodeListBuilder.php @@ -9,11 +9,13 @@ use Drupal\Component\Utility\String; use Drupal\Core\Datetime\Date; +use Drupal\Core\Entity\EntityAccessControllerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityListBuilder; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Language\Language; +use Drupal\Core\Utility\LinkGeneratorInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -37,11 +39,15 @@ class NodeListBuilder extends EntityListBuilder { * The entity type definition. * @param \Drupal\Core\Entity\EntityStorageInterface $storage * The entity storage class. + * @param \Drupal\Core\Entity\EntityAccessControllerInterface $entity_access + * The entity access controller. + * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator + * The link generator. * @param \Drupal\Core\Datetime\Date $date_service * The date service. */ - public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, Date $date_service) { - parent::__construct($entity_type, $storage); + public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityAccessControllerInterface $entity_access, LinkGeneratorInterface $link_generator, Date $date_service) { + parent::__construct($entity_type, $storage, $entity_access, $link_generator); $this->dateService = $date_service; } @@ -50,9 +56,14 @@ public function __construct(EntityTypeInterface $entity_type, EntityStorageInter * {@inheritdoc} */ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { + /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */ + $entity_manager = $container->get('entity.manager'); + return new static( $entity_type, - $container->get('entity.manager')->getStorage($entity_type->id()), + $entity_manager->getStorage($entity_type->id()), + $entity_manager->getAccessController($entity_type->id()), + $container->get('link_generator'), $container->get('date') ); } @@ -60,6 +71,20 @@ public static function createInstance(ContainerInterface $container, EntityTypeI /** * {@inheritdoc} */ + protected function getEmptyText() { + return $this->t('There is no content yet.'); + } + + /** + * {@inheritdoc} + */ + protected function getAddLinkText() { + return $this->t('Add new content.'); + } + + /** + * {@inheritdoc} + */ public function buildHeader() { // Enable language column and filter if multiple languages are added. $header = array( diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php b/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php index 1eb8375..ae9e15e 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php @@ -35,7 +35,7 @@ * links = { * "edit-form" = "responsive_image.mapping_page_edit", * "duplicate-form" = "responsive_image.mapping_page_duplicate", - * "add-entity-form" = "responsive_image.mapping_page_add" + * "add" = "responsive_image.mapping_page_add" * } * ) */ diff --git a/core/modules/system/lib/Drupal/system/Entity/DateFormat.php b/core/modules/system/lib/Drupal/system/Entity/DateFormat.php index d7d6d35..01dbb90 100644 --- a/core/modules/system/lib/Drupal/system/Entity/DateFormat.php +++ b/core/modules/system/lib/Drupal/system/Entity/DateFormat.php @@ -34,7 +34,7 @@ * links = { * "delete-form" = "system.date_format_delete", * "edit-form" = "system.date_format_edit", - * "add-entity-form" = "system.date_format_add" + * "add" = "system.date_format_add" * } * ) */