summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2017-01-30 15:15:08 -0800
committerwebchick2017-01-30 15:20:19 -0800
commit402c4ea2e341c59642456e09babafd767b0cbaaf (patch)
tree6ceedf7aa6ff4411d5906130f04ab80d92a64a55
parent844a60598376c0e209ea151b9a2b7345e6bc0a41 (diff)
Issue #2767025 by tstoeckler, Sam152, kristiaanvandeneynde, timmillwood, Gábor Hojtsy, Berdir, dawehner, xjm: Add entity type label for a collection of entities
-rw-r--r--core/lib/Drupal/Core/Entity/EntityType.php18
-rw-r--r--core/lib/Drupal/Core/Entity/EntityTypeInterface.php8
-rw-r--r--core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php9
-rw-r--r--core/modules/node/src/Entity/Node.php1
-rw-r--r--core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php10
-rw-r--r--core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php7
6 files changed, 48 insertions, 5 deletions
diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php
index 8728fb9..9f2587e 100644
--- a/core/lib/Drupal/Core/Entity/EntityType.php
+++ b/core/lib/Drupal/Core/Entity/EntityType.php
@@ -176,6 +176,13 @@ class EntityType extends PluginDefinition implements EntityTypeInterface {
protected $label = '';
/**
+ * The human-readable label for a collection of entities of the type.
+ *
+ * @var string
+ */
+ protected $label_collection = '';
+
+ /**
* The indefinite singular name of the type.
*
* @var string
@@ -729,6 +736,17 @@ class EntityType extends PluginDefinition implements EntityTypeInterface {
/**
* {@inheritdoc}
*/
+ public function getCollectionLabel() {
+ if (empty($this->label_collection)) {
+ $label = $this->getLabel();
+ $this->label_collection = new TranslatableMarkup('@label entities', ['@label' => $label], [], $this->getStringTranslation());
+ }
+ return $this->label_collection;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function getSingularLabel() {
if (empty($this->label_singular)) {
$lowercase_label = $this->getLowercaseLabel();
diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php
index e27aeec..eb5295f 100644
--- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php
+++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php
@@ -635,6 +635,14 @@ interface EntityTypeInterface extends PluginDefinitionInterface {
public function getLowercaseLabel();
/**
+ * Gets the collection label of the entity type.
+ *
+ * @return string
+ * The collection label.
+ */
+ public function getCollectionLabel();
+
+ /**
* Gets the singular label of the entity type.
*
* @return string
diff --git a/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php b/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
index fd23018..695dd7d 100644
--- a/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
+++ b/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php
@@ -316,13 +316,16 @@ class DefaultHtmlRouteProvider implements EntityRouteProviderInterface, EntityHa
// If the entity type does not provide an admin permission, there is no way
// to control access, so we cannot provide a route in a sensible way.
if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass() && ($admin_permission = $entity_type->getAdminPermission())) {
+ /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
+ $label = $entity_type->getCollectionLabel();
+
$route = new Route($entity_type->getLinkTemplate('collection'));
$route
->addDefaults([
'_entity_list' => $entity_type->id(),
- // @todo Improve this in https://www.drupal.org/node/2767025
- '_title' => '@label entities',
- '_title_arguments' => ['@label' => $entity_type->getLabel()],
+ '_title' => $label->getUntranslatedString(),
+ '_title_arguments' => $label->getArguments(),
+ '_title_context' => $label->getOption('context'),
])
->setRequirement('_permission', $admin_permission);
diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php
index 68afc83..bb0e36a 100644
--- a/core/modules/node/src/Entity/Node.php
+++ b/core/modules/node/src/Entity/Node.php
@@ -18,6 +18,7 @@ use Drupal\user\UserInterface;
* @ContentEntityType(
* id = "node",
* label = @Translation("Content"),
+ * label_collection = @Translation("Content"),
* label_singular = @Translation("content item"),
* label_plural = @Translation("content items"),
* label_count = @PluralTranslation(
diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php
index d633afc..06cf5e1 100644
--- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php
+++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php
@@ -319,6 +319,16 @@ class EntityTypeTest extends UnitTestCase {
}
/**
+ * @covers ::getCollectionLabel
+ */
+ public function testGetCollectionLabel() {
+ $translatable_label = new TranslatableMarkup('Entity test collection', [], [], $this->getStringTranslationStub());
+ $entity_type = $this->setUpEntityType(['label_collection' => $translatable_label]);
+ $entity_type->setStringTranslation($this->getStringTranslationStub());
+ $this->assertEquals('Entity test collection', $entity_type->getCollectionLabel());
+ }
+
+ /**
* @covers ::getSingularLabel
*/
public function testGetSingularLabel() {
diff --git a/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php b/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
index 170d0c1..7e853bc 100644
--- a/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
+++ b/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php
@@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
@@ -282,13 +283,15 @@ class DefaultHtmlRouteProviderTest extends UnitTestCase {
$entity_type4->getAdminPermission()->willReturn('administer the entity type');
$entity_type4->id()->willReturn('the_entity_type_id');
$entity_type4->getLabel()->willReturn('The entity type');
+ $entity_type4->getCollectionLabel()->willReturn(new TranslatableMarkup('Test entities'));
$entity_type4->getLinkTemplate('collection')->willReturn('/the/collection/link/template');
$entity_type4->entityClassImplements(FieldableEntityInterface::class)->willReturn(FALSE);
$route = (new Route('/the/collection/link/template'))
->setDefaults([
'_entity_list' => 'the_entity_type_id',
- '_title' => '@label entities',
- '_title_arguments' => ['@label' => 'The entity type'],
+ '_title' => 'Test entities',
+ '_title_arguments' => [],
+ '_title_context' => '',
])
->setRequirements([
'_permission' => 'administer the entity type',