Skip to content

Commit 9f3b63b

Browse files
committed
Use labels from entity definition in content-entity generator
1 parent f90a6d2 commit 9f3b63b

File tree

6 files changed

+50
-37
lines changed

6 files changed

+50
-37
lines changed

templates/content-entity/src/Entity/Example.php.twig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ use Drupal\Core\Entity\EntityChangedTrait;
3232
* id = "{{ entity_type_id }}",
3333
* label = @Translation("{{ entity_type_label }}"),
3434
* label_collection = @Translation("{{ entity_type_label|pluralize }}"),
35+
* label_singular = @Translation("{{ entity_type_label|lower }}"),
36+
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }}"),
37+
* label_count = @PluralTranslation(
38+
* singular = "@count {{ entity_type_label|pluralize|lower }}",
39+
* plural = "@count {{ entity_type_label|pluralize|lower }}",
40+
* ),
3541
{% if bundle %}
3642
* bundle_label = @Translation("{{ entity_type_label }} type"),
3743
{% endif %}
@@ -44,7 +50,7 @@ use Drupal\Core\Entity\EntityChangedTrait;
4450
* "form" = {
4551
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
4652
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
47-
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
53+
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
4854
* },
4955
* "route_provider" = {
5056
{% if canonical %}
@@ -89,9 +95,9 @@ use Drupal\Core\Entity\EntityChangedTrait;
8995
* "label" = "{{ label_base_field ? 'label' : 'id' }}",
9096
{% if author_base_field %}
9197
* "uuid" = "uuid",
92-
* "owner" = "uid"
98+
* "owner" = "uid",
9399
{% else %}
94-
* "uuid" = "uuid"
100+
* "uuid" = "uuid",
95101
{% endif %}
96102
* },
97103
{% if revisionable %}
@@ -102,7 +108,7 @@ use Drupal\Core\Entity\EntityChangedTrait;
102108
{% if created_base_field %}
103109
* "revision_created" = "revision_timestamp",
104110
{% endif %}
105-
* "revision_log_message" = "revision_log"
111+
* "revision_log_message" = "revision_log",
106112
* },
107113
{% endif %}
108114
* links = {
@@ -119,13 +125,13 @@ use Drupal\Core\Entity\EntityChangedTrait;
119125
{% else %}
120126
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
121127
{% endif %}
122-
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete"
128+
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
123129
* },
124130
{% if bundle %}
125131
* bundle_entity_type = "{{ entity_type_id }}_type",
126-
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form"
132+
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form",
127133
{% elseif fieldable %}
128-
* field_ui_base_route = "entity.{{ entity_type_id }}.settings"
134+
* field_ui_base_route = "entity.{{ entity_type_id }}.settings",
129135
{% endif %}
130136
* )
131137
*/

templates/content-entity/src/Entity/ExampleType.php.twig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
1010
* @ConfigEntityType(
1111
* id = "{{ entity_type_id }}_type",
1212
* label = @Translation("{{ entity_type_label }} type"),
13+
* label_collection = @Translation("{{ entity_type_label }} types"),
14+
* label_singular = @Translation("{{ entity_type_label|lower }} type"),
15+
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }} types"),
16+
* label_count = @PluralTranslation(
17+
* singular = "@count {{ entity_type_label|pluralize|lower }} type",
18+
* plural = "@count {{ entity_type_label|pluralize|lower }} types",
19+
* ),
1320
* handlers = {
1421
* "form" = {
1522
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}TypeForm",

templates/content-entity/src/Form/ExampleTypeForm.php.twig

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@ class {{ class_prefix }}TypeForm extends BundleEntityFormBase {
1818
$form = parent::form($form, $form_state);
1919
2020
$entity_type = $this->entity;
21-
if ($this->operation == 'add') {
22-
$form['#title'] = $this->t('Add {{ entity_type_label|lower }} type');
23-
}
24-
else {
25-
$form['#title'] = $this->t(
26-
'Edit %label {{ entity_type_label|lower }} type',
27-
['%label' => $entity_type->label()]
28-
);
21+
if ($this->operation == 'edit') {
22+
$form['#title'] = $this->t('Edit %label {{ entity_type_label|lower }} type', ['%label' => $entity_type->label()]);
2923
}
3024
3125
$form['label'] = [

tests/dcg/Generator/_content_entity/src/Entity/Example.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717
* id = "foo_example",
1818
* label = @Translation("Example"),
1919
* label_collection = @Translation("Examples"),
20+
* label_singular = @Translation("example"),
21+
* label_plural = @Translation("examples"),
22+
* label_count = @PluralTranslation(
23+
* singular = "@count examples",
24+
* plural = "@count examples",
25+
* ),
2026
* handlers = {
2127
* "list_builder" = "Drupal\foo\FooExampleListBuilder",
2228
* "views_data" = "Drupal\views\EntityViewsData",
2329
* "form" = {
2430
* "add" = "Drupal\foo\Form\FooExampleForm",
2531
* "edit" = "Drupal\foo\Form\FooExampleForm",
26-
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
32+
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
2733
* },
2834
* "route_provider" = {
2935
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
@@ -38,21 +44,21 @@
3844
* "revision" = "revision_id",
3945
* "label" = "label",
4046
* "uuid" = "uuid",
41-
* "owner" = "uid"
47+
* "owner" = "uid",
4248
* },
4349
* revision_metadata_keys = {
4450
* "revision_user" = "revision_uid",
4551
* "revision_created" = "revision_timestamp",
46-
* "revision_log_message" = "revision_log"
52+
* "revision_log_message" = "revision_log",
4753
* },
4854
* links = {
4955
* "collection" = "/admin/content/foo-example",
5056
* "add-form" = "/example/add",
5157
* "canonical" = "/example/{foo_example}",
5258
* "edit-form" = "/example/{foo_example}/edit",
53-
* "delete-form" = "/example/{foo_example}/delete"
59+
* "delete-form" = "/example/{foo_example}/delete",
5460
* },
55-
* field_ui_base_route = "entity.foo_example.settings"
61+
* field_ui_base_route = "entity.foo_example.settings",
5662
* )
5763
*/
5864
class FooExample extends RevisionableContentEntityBase implements FooExampleInterface {

tests/sut/nigma/tests/src/Functional/ContentEntityTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\Tests\nigma\Functional;
44

5-
use Drupal\Component\Render\FormattableMarkup;
5+
use Drupal\Component\Render\FormattableMarkup as FM;
66
use Drupal\dcg_test\TestTrait;
77
use Drupal\nigma\Entity\Example;
88
use Drupal\Tests\BrowserTestBase;
@@ -44,7 +44,7 @@ public function testEntityTypeUi(): void {
4444

4545
// -- Create a new bundle.
4646
$this->drupalGet('admin/structure/example_types');
47-
$this->assertPageTitle('Example type entities');
47+
$this->assertPageTitle('Example types');
4848
$this->assertXpath('//td[@class = "empty message" and contains(text(), "No example types available.")]');
4949

5050
$this->clickLink('Add example type');
@@ -56,18 +56,18 @@ public function testEntityTypeUi(): void {
5656
'id' => 'foo',
5757
];
5858
$this->drupalPostForm(NULL, $edit, 'Save example type');
59-
$this->assertStatusMessage(new FormattableMarkup('The example type %label has been added.', ['%label' => 'Foo']));
59+
$this->assertStatusMessage(new FM('The example type %label has been added.', ['%label' => 'Foo']));
6060

6161
$this->getSession()->getDriver()->click('//td[text() = "Foo"]/following-sibling::td//a[text() = "Edit"]');
62-
$this->assertPageTitle(new FormattableMarkup('Edit %label example type', ['%label' => 'Foo']));
62+
$this->assertPageTitle(new FM('Edit %label example type', ['%label' => 'Foo']));
6363
$this->assertXpath('//label[text() = "Label"]/following-sibling::input[@name = "label" and @value="Foo"]');
6464

6565
$edit = [
6666
'label' => 'Bar',
6767
'id' => 'bar',
6868
];
6969
$this->drupalPostForm(NULL, $edit, 'Save example type');
70-
$this->assertStatusMessage(new FormattableMarkup('The example type %label has been updated.', ['%label' => 'Bar']));
70+
$this->assertStatusMessage(new FM('The example type %label has been updated.', ['%label' => 'Bar']));
7171

7272
// Make sure the entity type is fieldable.
7373
$this->getSession()->getDriver()->click('//td[text() = "Bar"]/following-sibling::td//a[text() = "Manage fields"]');
@@ -140,7 +140,7 @@ public function testEntityTypeUi(): void {
140140
$this->drupalPostForm(NULL, $edit, 'Save');
141141

142142
// -- Test entity view builder.
143-
$this->assertStatusMessage(new FormattableMarkup('New example %label has been created.', ['%label' => 'Beer']));
143+
$this->assertStatusMessage(new FM('New example %label has been created.', ['%label' => 'Beer']));
144144
$this->assertPageTitle('Beer');
145145
$this->assertXpath('//div[text() = "Status"]/following-sibling::div[text() = "Enabled"]');
146146
$this->assertXpath('//div[text() = "Description"]/following-sibling::div/p[text() = "Dark"]');
@@ -171,7 +171,7 @@ public function testEntityTypeUi(): void {
171171
'description[0][value]' => 'White',
172172
];
173173
$this->drupalPostForm(NULL, $edit, 'Save');
174-
$this->assertStatusMessage(new FormattableMarkup('The example %label has been updated.', ['%label' => 'Wine']));
174+
$this->assertStatusMessage(new FM('The example %label has been updated.', ['%label' => 'Wine']));
175175
$this->assertPageTitle('Wine');
176176

177177
// -- Test entity list builder.
@@ -199,12 +199,12 @@ public function testEntityTypeUi(): void {
199199

200200
// -- Test entity deletion.
201201
$this->getSession()->getDriver()->click('//td[text() = "1"]/following-sibling::td//a[text() = "Delete"]');
202-
$this->assertPageTitle(new FormattableMarkup('Are you sure you want to delete the example %label?', ['%label' => 'Wine']));
202+
$this->assertPageTitle(new FM('Are you sure you want to delete the example %label?', ['%label' => 'Wine']));
203203
$this->assertSession()->pageTextContains('This action cannot be undone');
204204

205205
$this->drupalPostForm(NULL, [], 'Delete');
206-
$this->assertStatusMessage(new FormattableMarkup('The example %label has been deleted.', ['%label' => 'Wine']));
207-
$this->assertSession()->pageTextContains('There are no example entities yet.');
206+
$this->assertStatusMessage(new FM('The example %label has been deleted.', ['%label' => 'Wine']));
207+
$this->assertSession()->pageTextContains('There are no examples yet.');
208208
$this->assertSession()->pageTextContains('Total examples: 0');
209209
}
210210

tests/sut/sigma/tests/src/Functional/ContentEntityTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\Tests\sigma\Functional;
44

5-
use Drupal\Component\Render\FormattableMarkup;
5+
use Drupal\Component\Render\FormattableMarkup as FM;
66
use Drupal\dcg_test\TestTrait;
77
use Drupal\sigma\Entity\Example;
88
use Drupal\Tests\BrowserTestBase;
@@ -55,7 +55,7 @@ public function testEntityTypeUi(): void {
5555
$this->drupalPostForm('admin/structure/example/fields/add-field', $edit, 'Save');
5656
$this->drupalPostForm(NULL, [], 'Save field settings');
5757
$this->drupalPostForm(NULL, [], 'Save settings');
58-
$this->assertStatusMessage(new FormattableMarkup('Saved %label configuration.', ['%label' => 'Foo']));
58+
$this->assertStatusMessage(new FM('Saved %label configuration.', ['%label' => 'Foo']));
5959

6060
/** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
6161
$entity_type = \Drupal::entityTypeManager()->getDefinition('example');
@@ -121,7 +121,7 @@ public function testEntityTypeUi(): void {
121121
$this->drupalPostForm(NULL, $edit, 'Save');
122122

123123
// -- Test entity view builder.
124-
$this->assertStatusMessage(new FormattableMarkup('New example %label has been created.', ['%label' => 'Beer']));
124+
$this->assertStatusMessage(new FM('New example %label has been created.', ['%label' => 'Beer']));
125125
$this->assertPageTitle('Beer');
126126
$this->assertXpath('//div[text() = "Foo"]/following-sibling::div[text() = "Dark"]');
127127

@@ -143,7 +143,7 @@ public function testEntityTypeUi(): void {
143143
'field_foo[0][value]' => 'White',
144144
];
145145
$this->drupalPostForm(NULL, $edit, 'Save');
146-
$this->assertStatusMessage(new FormattableMarkup('The example %label has been updated.', ['%label' => 'Wine']));
146+
$this->assertStatusMessage(new FM('The example %label has been updated.', ['%label' => 'Wine']));
147147
$this->assertPageTitle('Wine');
148148

149149
// -- Test entity list builder.
@@ -165,12 +165,12 @@ public function testEntityTypeUi(): void {
165165

166166
// -- Test entity deletion.
167167
$this->getSession()->getDriver()->click('//td[text() = "1"]/following-sibling::td//a[text() = "Delete"]');
168-
$this->assertPageTitle(new FormattableMarkup('Are you sure you want to delete the example %label?', ['%label' => 'Wine']));
168+
$this->assertPageTitle(new FM('Are you sure you want to delete the example %label?', ['%label' => 'Wine']));
169169
$this->assertSession()->pageTextContains('This action cannot be undone');
170170

171171
$this->drupalPostForm(NULL, [], 'Delete');
172-
$this->assertStatusMessage(new FormattableMarkup('The example %label has been deleted.', ['%label' => 'Wine']));
173-
$this->assertSession()->pageTextContains('There are no example entities yet.');
172+
$this->assertStatusMessage(new FM('The example %label has been deleted.', ['%label' => 'Wine']));
173+
$this->assertSession()->pageTextContains('There are no examples yet.');
174174
$this->assertSession()->pageTextContains('Total examples: 0');
175175
}
176176

0 commit comments

Comments
 (0)