Skip to content
Permalink
Browse files

[generate:entity:content] Add coding standards for Entity classes (#…

…4031)

* REL #3985: Correct entity link templates

* REL #3985: Correct Controller CS

* REL #3985: Correct hook docblock

* REL #3985: Correct entity forms CS

* REL #3985: Correct StorageInterface CS

* REL #3985: Correct Entity Interface CS

* FIX #3985: Remove unnecessary use statement from the entity
  • Loading branch information...
harold20 authored and enzolutions committed May 11, 2019
1 parent 5a05088 commit b0a15c981e2723bb2bf38bdac936a8427d79560d
@@ -3,4 +3,3 @@ entity.{{ entity_name }}.add_form:
title: 'Add {{ label }}'
appears_on:
- entity.{{ entity_name }}.collection

@@ -7,4 +7,3 @@ entity.{{ entity_name }}.collection:
description: 'List {{ label }} (bundles)'
parent: system.admin_structure
weight: 99

@@ -29,4 +29,3 @@ entity.{{ entity_name }}.delete_form:
base_route: entity.{{ entity_name }}.canonical
title: Delete
weight: 10

@@ -16,7 +16,9 @@ function {{machine_name}}_help($route_name, RouteMatchInterface $route_match) {
case 'help.page.{{ machine_name }}':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
{% if description %}
$output .= '<p>' . t('{{ description|escape }}') . '</p>';
{% endif %}
return $output;

default:
@@ -11,9 +11,12 @@ namespace Drupal\{{ module }}\Controller;
{% block use_class %}
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Url;
use Drupal\{{ module }}\Entity\{{ entity_class }}Interface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endblock %}
{% block class_declaration %}
/**
@@ -22,50 +25,92 @@ use Drupal\{{ module }}\Entity\{{ entity_class }}Interface;
* Returns responses for {{ label }} routes.
*/
class {{ entity_class }}Controller extends ControllerBase implements ContainerInjectionInterface {% endblock %}

{% block class_methods %}

/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;

/**
* The renderer.
*
* @var \Drupal\Core\Render\Renderer
*/
protected $renderer;

/**
* Constructs a new {{ entity_class }}Controller.
*
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter.
* @param \Drupal\Core\Render\Renderer $renderer
* The renderer.
*/
public function __construct(DateFormatter $date_formatter, Renderer $renderer) {
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('date.formatter'),
$container->get('renderer')
);
}

/**
* Displays a {{ label }} revision.
* Displays a {{ label }} revision.
*
* @param int ${{ entity_name }}_revision
* The {{ label }} revision ID.
* The {{ label }} revision ID.
*
* @return array
* An array suitable for drupal_render().
*/
public function revisionShow(${{ entity_name }}_revision) {
${{ entity_name }} = $this->entityManager()->getStorage('{{ entity_name }}')->loadRevision(${{ entity_name }}_revision);
$view_builder = $this->entityManager()->getViewBuilder('{{ entity_name }}');
${{ entity_name }} = $this->entityTypeManager()->getStorage('{{ entity_name }}')
->loadRevision(${{ entity_name }}_revision);
$view_builder = $this->entityTypeManager()->getViewBuilder('{{ entity_name }}');

return $view_builder->view(${{ entity_name }});
}

/**
* Page title callback for a {{ label }} revision.
* Page title callback for a {{ label }} revision.
*
* @param int ${{ entity_name }}_revision
* The {{ label }} revision ID.
* The {{ label }} revision ID.
*
* @return string
* The page title.
*/
public function revisionPageTitle(${{ entity_name }}_revision) {
${{ entity_name }} = $this->entityManager()->getStorage('{{ entity_name }}')->loadRevision(${{ entity_name }}_revision);
return $this->t('Revision of %title from %date', ['%title' => ${{ entity_name }}->label(), '%date' => format_date(${{ entity_name }}->getRevisionCreationTime())]);
${{ entity_name }} = $this->entityTypeManager()->getStorage('{{ entity_name }}')
->loadRevision(${{ entity_name }}_revision);
return $this->t('Revision of %title from %date', [
'%title' => ${{ entity_name }}->label(),
'%date' => $this->dateFormatter->format(${{ entity_name }}->getRevisionCreationTime()),
]);
}

/**
* Generates an overview table of older revisions of a {{ label }} .
* Generates an overview table of older revisions of a {{ label }}.
*
* @param \Drupal\{{ module }}\Entity\{{ entity_class }}Interface ${{ entity_name }}
* A {{ label }} object.
* A {{ label }} object.
*
* @return array
* An array as expected by drupal_render().
*/
public function revisionOverview({{ entity_class }}Interface ${{ entity_name }}) {
$account = $this->currentUser();
${{ entity_name }}_storage = $this->entityManager()->getStorage('{{ entity_name }}');
${{ entity_name }}_storage = $this->entityTypeManager()->getStorage('{{ entity_name }}');

{% if is_translatable %}
$langcode = ${{ entity_name }}->language()->getId();
@@ -101,9 +146,12 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn
];

// Use revision link to link to revisions that are not active.
$date = \Drupal::service('date.formatter')->format($revision->getRevisionCreationTime(), 'short');
$date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
if ($vid != ${{ entity_name }}->getRevisionId()) {
$link = $this->l($date, new Url('entity.{{ entity_name }}.revision', ['{{ entity_name }}' => ${{ entity_name }}->id(), '{{ entity_name }}_revision' => $vid]));
$link = $this->l($date, new Url('entity.{{ entity_name }}.revision', [
'{{ entity_name }}' => ${{ entity_name }}->id(),
'{{ entity_name }}_revision' => $vid,
]));
}
else {
$link = ${{ entity_name }}->link($date);
@@ -116,8 +164,11 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn
'#template' => '{{ '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}' }}',
'#context' => [
'date' => $link,
'username' => \Drupal::service('renderer')->renderPlain($username),
'message' => ['#markup' => $revision->getRevisionLogMessage(), '#allowed_tags' => Xss::getHtmlTagList()],
'username' => $this->renderer->renderPlain($username),
'message' => [
'#markup' => $revision->getRevisionLogMessage(),
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
],
];
@@ -143,18 +194,31 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn
'title' => $this->t('Revert'),
{% if is_translatable %}
'url' => $has_translations ?
Url::fromRoute('entity.{{ entity_name }}.translation_revert', ['{{ entity_name }}' => ${{ entity_name }}->id(), '{{ entity_name }}_revision' => $vid, 'langcode' => $langcode]) :
Url::fromRoute('entity.{{ entity_name }}.revision_revert', ['{{ entity_name }}' => ${{ entity_name }}->id(), '{{ entity_name }}_revision' => $vid]),
Url::fromRoute('entity.{{ entity_name }}.translation_revert', [
'{{ entity_name }}' => ${{ entity_name }}->id(),
'{{ entity_name }}_revision' => $vid,
'langcode' => $langcode,
]) :
Url::fromRoute('entity.{{ entity_name }}.revision_revert', [
'{{ entity_name }}' => ${{ entity_name }}->id(),
'{{ entity_name }}_revision' => $vid,
]),
{% else %}
'url' => Url::fromRoute('entity.{{ entity_name }}.revision_revert', ['{{ entity_name }}' => ${{ entity_name }}->id(), '{{ entity_name }}_revision' => $vid]),
'url' => Url::fromRoute('entity.{{ entity_name }}.revision_revert', [
'{{ entity_name }}' => ${{ entity_name }}->id(),
'{{ entity_name }}_revision' => $vid,
]),
{% endif %}
];
}

if ($delete_permission) {
$links['delete'] = [
'title' => $this->t('Delete'),
'url' => Url::fromRoute('entity.{{ entity_name }}.revision_delete', ['{{ entity_name }}' => ${{ entity_name }}->id(), '{{ entity_name }}_revision' => $vid]),
'url' => Url::fromRoute('entity.{{ entity_name }}.revision_delete', [
'{{ entity_name }}' => ${{ entity_name }}->id(),
'{{ entity_name }}_revision' => $vid,
]),
];
}

@@ -64,9 +64,9 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('entity.manager');
$entity_type_manager = $container->get('entity_type.manager');
return new static(
$entity_manager->getStorage('{{ entity_name }}'),
$entity_type_manager->getStorage('{{ entity_name }}'),
$container->get('database')
);
}
@@ -82,7 +82,9 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => format_date($this->revision->getRevisionCreationTime())]);
return $this->t('Are you sure you want to delete the revision from %revision-date?', [
'%revision-date' => format_date($this->revision->getRevisionCreationTime()),
]);
}

/**
@@ -96,7 +98,7 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
return $this->t('Delete');
}

/**
@@ -116,7 +118,7 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
$this->{{ entity_class }}Storage->deleteRevision($this->revision->getRevisionId());

$this->logger('content')->notice('{{ label }}: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
\Drupal::messenger()->addMessage(t('Revision from %revision-date of {{ label }} %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()]));
$this->messenger()->addMessage(t('Revision from %revision-date of {{ label }} %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()]));
$form_state->setRedirect(
'entity.{{ entity_name }}.canonical',
['{{ entity_name }}' => $this->revision->id()]
@@ -19,7 +19,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;

{% block class_declaration %}
/**
* Provides a form for reverting a {{ label }} revision for a single translation.
* Provides a form for reverting a {{ label }} revision for a single trans.
*
* @ingroup {{module}}
*/
@@ -60,7 +60,7 @@ class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('{{ entity_name }}'),
$container->get('entity_type.manager')->getStorage('{{ entity_name }}'),
$container->get('date.formatter'),
$container->get('language_manager')
);
@@ -77,7 +77,10 @@ class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to revert @language translation to the revision from %revision-date?', ['@language' => $this->languageManager->getLanguageName($this->langcode), '%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
return $this->t('Are you sure you want to revert @language translation to the revision from %revision-date?', [
'@language' => $this->languageManager->getLanguageName($this->langcode),
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
]);
}

/**
@@ -66,7 +66,7 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('{{ entity_name }}'),
$container->get('entity_type.manager')->getStorage('{{ entity_name }}'),
$container->get('date.formatter')
);
}
@@ -82,7 +82,9 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
return $this->t('Are you sure you want to revert to the revision from %revision-date?', [
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
]);
}

/**
@@ -96,7 +98,7 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Revert');
return $this->t('Revert');
}

/**
@@ -125,11 +127,13 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
$original_revision_timestamp = $this->revision->getRevisionCreationTime();

$this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
$this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
$this->revision->revision_log = $this->t('Copy of the revision from %date.', [
'%date' => $this->dateFormatter->format($original_revision_timestamp),
]);
$this->revision->save();

$this->logger('content')->notice('{{ label }}: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
\Drupal::messenger()->addMessage(t('{{ label }} %title has been reverted to the revision from %revision-date.', ['%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
$this->messenger()->addMessage(t('{{ label }} %title has been reverted to the revision from %revision-date.', ['%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
$form_state->setRedirect(
'entity.{{ entity_name }}.version_history',
['{{ entity_name }}' => $this->revision->id()]

0 comments on commit b0a15c9

Please sign in to comment.
You can’t perform that action at this time.