From cb14f8badfdb6d8342eba7663ae4ca3ff1f435f3 Mon Sep 17 00:00:00 2001 From: catch Date: Mon, 1 Jun 2020 12:23:12 +0100 Subject: [PATCH] Issue #3109795 by alexpott, Berdir: Entity plural label context is not set as expected (cherry picked from commit 5002f8f786f6d93a8db35a36f9c221438fa83c8c) --- core/lib/Drupal/Core/Entity/EntityType.php | 7 +++++-- core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php index 8155ff250cd..fdaab2e6cd3 100644 --- a/core/lib/Drupal/Core/Entity/EntityType.php +++ b/core/lib/Drupal/Core/Entity/EntityType.php @@ -821,8 +821,11 @@ class EntityType extends PluginDefinition implements EntityTypeInterface { if (empty($this->label_count)) { return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getSingularLabel()], ['context' => 'Entity type label']); } - $context = isset($this->label_count['context']) ? $this->label_count['context'] : 'Entity type label'; - return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], ['context' => $context]); + $options = []; + if (isset($this->label_count['context'])) { + $options['context'] = $this->label_count['context']; + } + return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], [], $options); } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php index 6d5812ac2e5..28bff19c11e 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php @@ -397,6 +397,12 @@ class EntityTypeTest extends UnitTestCase { $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)); + $this->assertArrayNotHasKey('context', $entity_type->getCountLabel(1)->getOptions()); + + // Test a custom context. + $entity_type = $this->setUpEntityType(['label_count' => ['singular' => 'one entity test', 'plural' => '@count entity test', 'context' => 'custom context']]); + $entity_type->setStringTranslation($this->getStringTranslationStub()); + $this->assertSame('custom context', $entity_type->getCountLabel(1)->getOption('context')); } /** @@ -408,6 +414,7 @@ class EntityTypeTest extends UnitTestCase { $this->assertEquals('1 entity test plural', $entity_type->getCountLabel(1)); $this->assertEquals('2 entity test plural entities', $entity_type->getCountLabel(2)); $this->assertEquals('200 entity test plural entities', $entity_type->getCountLabel(200)); + $this->assertSame('Entity type label', $entity_type->getCountLabel(1)->getOption('context')); } /**