Skip to content

Commit

Permalink
Issue #3109795 by alexpott, Berdir: Entity plural label context is no…
Browse files Browse the repository at this point in the history
…t set as expected

(cherry picked from commit 5002f8f786f6d93a8db35a36f9c221438fa83c8c)
  • Loading branch information
catch committed Jun 1, 2020
1 parent f0ea15c commit 553d63f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/Drupal/Core/Entity/EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,11 @@ public function getCountLabel($count) {
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);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Drupal/Tests/Core/Entity/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ public function testGetCountLabel() {
$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'));
}

/**
Expand All @@ -408,6 +414,7 @@ public function testGetCountLabelDefault() {
$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'));
}

/**
Expand Down

0 comments on commit 553d63f

Please sign in to comment.