Skip to content

Commit

Permalink
Use labels from entity definition in content-entity generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-teck committed Jun 29, 2020
1 parent f90a6d2 commit 9f3b63b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
20 changes: 13 additions & 7 deletions templates/content-entity/src/Entity/Example.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ use Drupal\Core\Entity\EntityChangedTrait;
* id = "{{ entity_type_id }}",
* label = @Translation("{{ entity_type_label }}"),
* label_collection = @Translation("{{ entity_type_label|pluralize }}"),
* label_singular = @Translation("{{ entity_type_label|lower }}"),
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }}"),
* label_count = @PluralTranslation(
* singular = "@count {{ entity_type_label|pluralize|lower }}",
* plural = "@count {{ entity_type_label|pluralize|lower }}",
* ),
{% if bundle %}
* bundle_label = @Translation("{{ entity_type_label }} type"),
{% endif %}
Expand All @@ -44,7 +50,7 @@ use Drupal\Core\Entity\EntityChangedTrait;
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "edit" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}Form",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "route_provider" = {
{% if canonical %}
Expand Down Expand Up @@ -89,9 +95,9 @@ use Drupal\Core\Entity\EntityChangedTrait;
* "label" = "{{ label_base_field ? 'label' : 'id' }}",
{% if author_base_field %}
* "uuid" = "uuid",
* "owner" = "uid"
* "owner" = "uid",
{% else %}
* "uuid" = "uuid"
* "uuid" = "uuid",
{% endif %}
* },
{% if revisionable %}
Expand All @@ -102,7 +108,7 @@ use Drupal\Core\Entity\EntityChangedTrait;
{% if created_base_field %}
* "revision_created" = "revision_timestamp",
{% endif %}
* "revision_log_message" = "revision_log"
* "revision_log_message" = "revision_log",
* },
{% endif %}
* links = {
Expand All @@ -119,13 +125,13 @@ use Drupal\Core\Entity\EntityChangedTrait;
{% else %}
* "edit-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}",
{% endif %}
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete"
* "delete-form" = "{{ entity_base_path }}/{{ '{' }}{{ entity_type_id }}{{ '}' }}/delete",
* },
{% if bundle %}
* bundle_entity_type = "{{ entity_type_id }}_type",
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form"
* field_ui_base_route = "entity.{{ entity_type_id }}_type.edit_form",
{% elseif fieldable %}
* field_ui_base_route = "entity.{{ entity_type_id }}.settings"
* field_ui_base_route = "entity.{{ entity_type_id }}.settings",
{% endif %}
* )
*/
Expand Down
7 changes: 7 additions & 0 deletions templates/content-entity/src/Entity/ExampleType.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
* @ConfigEntityType(
* id = "{{ entity_type_id }}_type",
* label = @Translation("{{ entity_type_label }} type"),
* label_collection = @Translation("{{ entity_type_label }} types"),
* label_singular = @Translation("{{ entity_type_label|lower }} type"),
* label_plural = @Translation("{{ entity_type_label|pluralize|lower }} types"),
* label_count = @PluralTranslation(
* singular = "@count {{ entity_type_label|pluralize|lower }} type",
* plural = "@count {{ entity_type_label|pluralize|lower }} types",
* ),
* handlers = {
* "form" = {
* "add" = "Drupal\{{ machine_name }}\Form\{{ class_prefix }}TypeForm",
Expand Down
10 changes: 2 additions & 8 deletions templates/content-entity/src/Form/ExampleTypeForm.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ class {{ class_prefix }}TypeForm extends BundleEntityFormBase {
$form = parent::form($form, $form_state);

$entity_type = $this->entity;
if ($this->operation == 'add') {
$form['#title'] = $this->t('Add {{ entity_type_label|lower }} type');
}
else {
$form['#title'] = $this->t(
'Edit %label {{ entity_type_label|lower }} type',
['%label' => $entity_type->label()]
);
if ($this->operation == 'edit') {
$form['#title'] = $this->t('Edit %label {{ entity_type_label|lower }} type', ['%label' => $entity_type->label()]);
}

$form['label'] = [
Expand Down
16 changes: 11 additions & 5 deletions tests/dcg/Generator/_content_entity/src/Entity/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
* id = "foo_example",
* label = @Translation("Example"),
* label_collection = @Translation("Examples"),
* label_singular = @Translation("example"),
* label_plural = @Translation("examples"),
* label_count = @PluralTranslation(
* singular = "@count examples",
* plural = "@count examples",
* ),
* handlers = {
* "list_builder" = "Drupal\foo\FooExampleListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\foo\Form\FooExampleForm",
* "edit" = "Drupal\foo\Form\FooExampleForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
Expand All @@ -38,21 +44,21 @@
* "revision" = "revision_id",
* "label" = "label",
* "uuid" = "uuid",
* "owner" = "uid"
* "owner" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_uid",
* "revision_created" = "revision_timestamp",
* "revision_log_message" = "revision_log"
* "revision_log_message" = "revision_log",
* },
* links = {
* "collection" = "/admin/content/foo-example",
* "add-form" = "/example/add",
* "canonical" = "/example/{foo_example}",
* "edit-form" = "/example/{foo_example}/edit",
* "delete-form" = "/example/{foo_example}/delete"
* "delete-form" = "/example/{foo_example}/delete",
* },
* field_ui_base_route = "entity.foo_example.settings"
* field_ui_base_route = "entity.foo_example.settings",
* )
*/
class FooExample extends RevisionableContentEntityBase implements FooExampleInterface {
Expand Down
20 changes: 10 additions & 10 deletions tests/sut/nigma/tests/src/Functional/ContentEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\Tests\nigma\Functional;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\FormattableMarkup as FM;
use Drupal\dcg_test\TestTrait;
use Drupal\nigma\Entity\Example;
use Drupal\Tests\BrowserTestBase;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function testEntityTypeUi(): void {

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

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

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

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

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

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

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

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

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

Expand Down
14 changes: 7 additions & 7 deletions tests/sut/sigma/tests/src/Functional/ContentEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\Tests\sigma\Functional;

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

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

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

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

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

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

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

Expand Down

0 comments on commit 9f3b63b

Please sign in to comment.