From c6adb7c717ae5be9c4eccae25015419515a5a139 Mon Sep 17 00:00:00 2001 From: catch Date: Tue, 11 May 2021 21:35:27 +0100 Subject: [PATCH] Issue #2730631 by jibran, benjifisher, claudiu.cristea, pdenooijer, merauluka, Deepak Goyal, mpp, gnuget, Spokje, sanjayk, idimopoulos, nikitagupta, ayushmishra206, ridhimaabrol24, mrinalini9, KapilV, harings_rob, Berdir, alexpott, catch, dawehner, tstoeckler, dpi, lexbritvin, mlncn, jhedstrom, kevin.dutra, amateescu, johnwebdev, Steven Spasbo: Upcast node and node_revision parameters of node revision routes --- core/modules/node/node.post_update.php | 7 ++++++ core/modules/node/node.routing.yml | 24 +++++++++++++++++++ .../src/ContextProvider/NodeRouteContext.php | 6 ++--- .../node/src/Controller/NodeController.php | 24 +++++++++---------- .../node/src/Form/NodeRevisionDeleteForm.php | 5 ++-- .../node/src/Form/NodeRevisionRevertForm.php | 4 ++-- 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/core/modules/node/node.post_update.php b/core/modules/node/node.post_update.php index cb5c5fc6c9c..350332edc17 100644 --- a/core/modules/node/node.post_update.php +++ b/core/modules/node/node.post_update.php @@ -47,3 +47,10 @@ function node_post_update_glossary_view_published() { } } } + +/** + * Rebuild the node revision routes. + */ +function node_post_update_rebuild_node_revision_routes() { + // Empty update to rebuild routes. +} diff --git a/core/modules/node/node.routing.yml b/core/modules/node/node.routing.yml index ac958ea242b..962f21c0ccd 100644 --- a/core/modules/node/node.routing.yml +++ b/core/modules/node/node.routing.yml @@ -60,6 +60,9 @@ entity.node.version_history: node: \d+ options: _node_operation_route: TRUE + parameters: + node: + type: entity:node entity.node.revision: path: '/node/{node}/revisions/{node_revision}/view' @@ -69,6 +72,12 @@ entity.node.revision: requirements: _access_node_revision: 'view' node: \d+ + options: + parameters: + node: + type: entity:node + node_revision: + type: entity_revision:node node.revision_revert_confirm: path: '/node/{node}/revisions/{node_revision}/revert' @@ -80,6 +89,11 @@ node.revision_revert_confirm: node: \d+ options: _node_operation_route: TRUE + parameters: + node: + type: entity:node + node_revision: + type: entity_revision:node node.revision_revert_translation_confirm: path: '/node/{node}/revisions/{node_revision}/revert/{langcode}' @@ -91,6 +105,11 @@ node.revision_revert_translation_confirm: node: \d+ options: _node_operation_route: TRUE + parameters: + node: + type: entity:node + node_revision: + type: entity_revision:node node.revision_delete_confirm: path: '/node/{node}/revisions/{node_revision}/delete' @@ -102,6 +121,11 @@ node.revision_delete_confirm: node: \d+ options: _node_operation_route: TRUE + parameters: + node: + type: entity:node + node_revision: + type: entity_revision:node entity.node_type.collection: path: '/admin/structure/types' diff --git a/core/modules/node/src/ContextProvider/NodeRouteContext.php b/core/modules/node/src/ContextProvider/NodeRouteContext.php index 2a241cb4817..e738cf6cb06 100644 --- a/core/modules/node/src/ContextProvider/NodeRouteContext.php +++ b/core/modules/node/src/ContextProvider/NodeRouteContext.php @@ -45,10 +45,8 @@ class NodeRouteContext implements ContextProviderInterface { if (($route_object = $this->routeMatch->getRouteObject())) { $route_contexts = $route_object->getOption('parameters'); // Check for a node revision parameter first. - // @todo https://www.drupal.org/i/2730631 will allow to use the upcasted - // node revision object. - if ($revision_id = $this->routeMatch->getRawParameter('node_revision')) { - $value = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id); + if (isset($route_contexts['node_revision']) && $revision = $this->routeMatch->getParameter('node_revision')) { + $value = $revision; } elseif (isset($route_contexts['node']) && $node = $this->routeMatch->getParameter('node')) { $value = $node; diff --git a/core/modules/node/src/Controller/NodeController.php b/core/modules/node/src/Controller/NodeController.php index 965cbcb59a1..eb39f42a78c 100644 --- a/core/modules/node/src/Controller/NodeController.php +++ b/core/modules/node/src/Controller/NodeController.php @@ -115,33 +115,33 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa /** * Displays a node revision. * - * @param int $node_revision - * The node revision ID. + * @param \Drupal\node\NodeInterface $node_revision + * The node revision. * * @return array * An array suitable for \Drupal\Core\Render\RendererInterface::render(). */ - public function revisionShow($node_revision) { - $node = $this->entityTypeManager()->getStorage('node')->loadRevision($node_revision); - $node = $this->entityRepository->getTranslationFromContext($node); + public function revisionShow(NodeInterface $node_revision) { $node_view_controller = new NodeViewController($this->entityTypeManager(), $this->renderer, $this->currentUser(), $this->entityRepository); - $page = $node_view_controller->view($node); - unset($page['nodes'][$node->id()]['#cache']); + $page = $node_view_controller->view($node_revision); + unset($page['nodes'][$node_revision->id()]['#cache']); return $page; } /** * Page title callback for a node revision. * - * @param int $node_revision - * The node revision ID. + * @param \Drupal\node\NodeInterface $node_revision + * The node revision. * * @return string * The page title. */ - public function revisionPageTitle($node_revision) { - $node = $this->entityTypeManager()->getStorage('node')->loadRevision($node_revision); - return $this->t('Revision of %title from %date', ['%title' => $node->label(), '%date' => $this->dateFormatter->format($node->getRevisionCreationTime())]); + public function revisionPageTitle(NodeInterface $node_revision) { + return $this->t('Revision of %title from %date', [ + '%title' => $node_revision->label(), + '%date' => $this->dateFormatter->format($node_revision->getRevisionCreationTime()), + ]); } /** diff --git a/core/modules/node/src/Form/NodeRevisionDeleteForm.php b/core/modules/node/src/Form/NodeRevisionDeleteForm.php index cd5afca2971..be63883481e 100644 --- a/core/modules/node/src/Form/NodeRevisionDeleteForm.php +++ b/core/modules/node/src/Form/NodeRevisionDeleteForm.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Drupal\node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -117,8 +118,8 @@ class NodeRevisionDeleteForm extends ConfirmFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) { - $this->revision = $this->nodeStorage->loadRevision($node_revision); + public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node_revision = NULL) { + $this->revision = $node_revision; $form = parent::buildForm($form, $form_state); return $form; diff --git a/core/modules/node/src/Form/NodeRevisionRevertForm.php b/core/modules/node/src/Form/NodeRevisionRevertForm.php index 220e81532ba..b223e30f40d 100644 --- a/core/modules/node/src/Form/NodeRevisionRevertForm.php +++ b/core/modules/node/src/Form/NodeRevisionRevertForm.php @@ -111,8 +111,8 @@ class NodeRevisionRevertForm extends ConfirmFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL) { - $this->revision = $this->nodeStorage->loadRevision($node_revision); + public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node_revision = NULL) { + $this->revision = $node_revision; $form = parent::buildForm($form, $form_state); return $form;