diff --git a/core/lib/Drupal/Core/Annotation/PluralTranslation.php b/core/lib/Drupal/Core/Annotation/PluralTranslation.php index b9420c0..47074c4 100644 --- a/core/lib/Drupal/Core/Annotation/PluralTranslation.php +++ b/core/lib/Drupal/Core/Annotation/PluralTranslation.php @@ -29,10 +29,10 @@ * above: * @code * // Returns: 1 item - * echo $entity_type->entityTypeLabel(\Drupal\Core\Entity\EntityTypeInterface::LABEL_COUNT, 1); + * $entity_type->entityTypeLabel(\Drupal\Core\Entity\EntityTypeInterface::LABEL_COUNT, 1); * * // Returns: 5 items - * echo $entity_type->entityTypeLabel(\Drupal\Core\Entity\EntityTypeInterface::LABEL_COUNT, 5); + * $entity_type->entityTypeLabel(\Drupal\Core\Entity\EntityTypeInterface::LABEL_COUNT, 5); * @endcode * * @Annotation diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php index 1d1febb..8c2f72d 100644 --- a/core/lib/Drupal/Core/Entity/EntityType.php +++ b/core/lib/Drupal/Core/Entity/EntityType.php @@ -761,21 +761,22 @@ public function getLowercaseLabel() { /** * {@inheritdoc} */ - public function entityTypeLabel($label_form = self::LABEL_GENERIC, $count = NULL) { - switch ($label_form) { - case static::LABEL_SINGULAR: - return $this->label_singular; - - case static::LABEL_PLURAL: - return $this->label_plural; + public function getSingularLabel() { + return $this->label_singular; + } - case static::LABEL_COUNT: - return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural']); + /** + * {@inheritdoc} + */ + public function getPluralLabel() { + return $this->label_plural; + } - case static::LABEL_GENERIC: - default: - return $this->label; - } + /** + * {@inheritdoc} + */ + public function getCountLabel($count) { + return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural']); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php index ca8dca3..8794e32 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php @@ -653,18 +653,31 @@ public function getLabel(); public function getLowercaseLabel(); /** - * Gets the translated label of the entity type. + * Gets the singular label of the entity type. + * + * @return string + * The singular label. + */ + public function getSingularLabel(); + + /** + * Gets the plural label of the entity type. + * + * @return string + * The plural label. + */ + public function getPluralLabel(); + + /** + * Gets the count label of the entity type * - * @param int $label_form - * (optional) One of self::LABEL_GENERIC, self::LABEL_SINGULAR, - * self:LABEL_PLURAL or self::LABEL_COUNT. Defaults to the generic label. * @param int $count - * (optional) The item count to display if the plural form was requested. + * The item count to display if the plural form was requested. * * @return string - * The translated label. + * The count label. */ - public function entityTypeLabel($label_form = self::LABEL_GENERIC, $count = NULL); + public function getCountLabel($count); /** * Gets a callable that can be used to provide the entity URI. diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php index 6fa1f9c..54b6d4a 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Entity; +use Drupal\Component\Render\MarkupInterface; use Drupal\Core\Entity\EntityType; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; @@ -324,6 +325,37 @@ public function testGetGroupLabel() { } /** + * @covers ::getSingularLabel + */ + public function testGetSingularLabel() { + $translatable_label = new TranslatableMarkup('entity test singular', [], [], $this->getStringTranslationStub()); + $entity_type = $this->setUpEntityType(['label_singular' => $translatable_label]); + $entity_type->setStringTranslation($this->getStringTranslationStub()); + $this->assertEquals('entity test singular', $entity_type->getSingularLabel()); + } + + /** + * @covers ::getPluralLabel + */ + public function testGetPluralLabel() { + $translatable_label = new TranslatableMarkup('entity test plural', [], [], $this->getStringTranslationStub()); + $entity_type = $this->setUpEntityType(['label_plural' => $translatable_label]); + $entity_type->setStringTranslation($this->getStringTranslationStub()); + $this->assertEquals('entity test plural', $entity_type->getPluralLabel()); + } + + /** + * @covers ::getCountLabel + */ + public function testGetCountLabel() { + $entity_type = $this->setUpEntityType(['label_count' => ['singular' => 'one entity test', 'plural' => '@count entity test']]); + $entity_type->setStringTranslation($this->getStringTranslationStub()); + $this->assertEquals('one entity test', $entity_type->getCountLabel(1)); + $this->assertEquals('2 entity test', $entity_type->getCountLabel(2)); + $this->assertEquals('200 entity test', $entity_type->getCountLabel(200)); + } + + /** * Gets a mock controller class name. * * @return string