Skip to content

Commit

Permalink
Fix drupal 9 deprecations in generateentitycontent] (#4214)
Browse files Browse the repository at this point in the history
* Update entity-controller.php.twig

* Update entity-content-revision-delete.php.twig

* Update entity-content-revision-revert.php.twig

Co-authored-by: Nikolay Lobachev <[email protected]>
  • Loading branch information
Holo96 and LOBsTerr authored Sep 16, 2022
1 parent b0fea30 commit f0cf5c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions templates/module/src/Controller/entity-controller.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Drupal\{{ module }}\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\{{ module }}\Entity\{{ entity_class }}Interface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -132,13 +133,13 @@ class {{ entity_class }}Controller extends ControllerBase implements ContainerIn
// Use revision link to link to revisions that are not active.
$date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
if ($vid != ${{ entity_name }}->getRevisionId()) {
$link = $this->l($date, new Url('entity.{{ entity_name }}.revision', [
$link = Link::fromTextAndUrl($date, new Url('entity.{{ entity_name }}.revision', [
'{{ entity_name }}' => ${{ entity_name }}->id(),
'{{ entity_name }}_revision' => $vid,
]));
}
else {
$link = ${{ entity_name }}->link($date);
$link = ${{ entity_name }}->toLink($date)->toString();
}

$row = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %}
{% block class_methods %}

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

/**
* The {{ label }} revision.
*
Expand All @@ -49,6 +57,7 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->dateFormatter = $container->get('date.formatter');
$instance->{{ entity_class[:1]|lower ~ entity_class[1:] }}Storage = $container->get('entity_type.manager')->getStorage('{{ entity_name }}');
$instance->connection = $container->get('database');
return $instance;
Expand All @@ -66,7 +75,7 @@ class {{ entity_class }}RevisionDeleteForm extends ConfirmFormBase {% endblock %
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the revision from %revision-date?', [
'%revision-date' => \Drupal::service('date.formatter')->format($this->revision->getRevisionCreationTime()),
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
]);
}

Expand Down Expand Up @@ -101,7 +110,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()]);
$this->messenger()->addMessage(t('Revision from %revision-date of {{ label }} %title has been deleted.', ['%revision-date' => \Drupal::service('date.formatter')->format($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()]));
$this->messenger()->addMessage(t('Revision from %revision-date of {{ label }} %title has been deleted.', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()]));
$form_state->setRedirect(
'entity.{{ entity_name }}.canonical',
['{{ entity_name }}' => $this->revision->id()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;

/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;

/**
* {@inheritdoc}
Expand All @@ -52,6 +59,7 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
$instance = parent::create($container);
$instance->{{ entity_class[:1]|lower ~ entity_class[1:] }}Storage = $container->get('entity_type.manager')->getStorage('{{ entity_name }}');
$instance->dateFormatter = $container->get('date.formatter');
$instance->time = $container->get('datetime.time');
return $instance;
}

Expand Down Expand Up @@ -138,7 +146,7 @@ class {{ entity_class }}RevisionRevertForm extends ConfirmFormBase {% endblock %
protected function prepareRevertedRevision({{ entity_class }}Interface $revision, FormStateInterface $form_state) {
$revision->setNewRevision();
$revision->isDefaultRevision(TRUE);
$revision->setRevisionCreationTime(\Drupal::time()->getRequestTime());
$revision->setRevisionCreationTime($this->time->getRequestTime());

return $revision;
}
Expand Down

0 comments on commit f0cf5c2

Please sign in to comment.