Commit 402c4ea2 authored by webchick's avatar webchick

Issue #2767025 by tstoeckler, Sam152, kristiaanvandeneynde, timmillwood, Gábor...

Issue #2767025 by tstoeckler, Sam152, kristiaanvandeneynde, timmillwood, Gábor Hojtsy, Berdir, dawehner, xjm: Add entity type label for a collection of entities
parent 844a6059
......@@ -175,6 +175,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.
*
......@@ -726,6 +733,17 @@ public function getLowercaseLabel() {
return Unicode::strtolower($this->getLabel());
}
/**
* {@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}
*/
......
......@@ -634,6 +634,14 @@ public function getLabel();
*/
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.
*
......
......@@ -316,13 +316,16 @@ protected function getCollectionRoute(EntityTypeInterface $entity_type) {
// 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);
......
......@@ -18,6 +18,7 @@
* @ContentEntityType(
* id = "node",
* label = @Translation("Content"),
* label_collection = @Translation("Content"),
* label_singular = @Translation("content item"),
* label_plural = @Translation("content items"),
* label_count = @PluralTranslation(
......
......@@ -318,6 +318,16 @@ public function testGetGroupLabel() {
$this->assertSame($default_label, $entity_type->getGroupLabel());
}
/**
* @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
*/
......
......@@ -14,6 +14,7 @@
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 @@ public function providerTestGetCollectionRoute() {
$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',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment