diff --git a/core/modules/node/src/Form/NodePreviewForm.php b/core/modules/node/src/Form/NodePreviewForm.php index 1b9517d..4d68ce4 100644 --- a/core/modules/node/src/Form/NodePreviewForm.php +++ b/core/modules/node/src/Form/NodePreviewForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -33,7 +34,11 @@ class NodePreviewForm extends FormBase { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('entity.manager'), $container->get('config.factory')); + return new static( + $container->get('entity.manager'), + $container->get('config.factory'), + $container->get('redirect.destination') + ); } /** @@ -43,10 +48,15 @@ public static function create(ContainerInterface $container) { * The entity manager service. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The configuration factory. + * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination + * The redirect destination service. + * + * @todo Make $redirect_destination required in Drupal 9.0.x. */ - public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) { + public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, RedirectDestinationInterface $redirect_destination = NULL) { $this->entityManager = $entity_manager; $this->configFactory = $config_factory; + $this->redirectDestination = $redirect_destination; } /** @@ -76,10 +86,10 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt if ($node->isNew()) { $query_options['query']['uuid'] = $node->uuid(); } - if ($destination = $this->getRequest()->query->get('destination')) { - $query_options['query']['destination'] = $destination; + $query = $this->getRequest()->query; + if ($destination = $query->has('destination')) { + $query_options['query']['destination'] = $query->get('destination'); } - $form['backlink'] = array( '#type' => 'link', '#title' => $this->t('Back to content editing'), @@ -123,16 +133,12 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $route_params = [ + $route_parameters = [ 'node_preview' => $form_state->getValue('uuid'), 'view_mode_id' => $form_state->getValue('view_mode'), ]; - $options = $this->getRequest()->query->all(); - if ($destination = $this->getRequest()->query->get('destination')) { - $this->getRequest()->query->remove('destination'); - $options['query']['destination'] = $destination; - } - $form_state->setRedirect('entity.node.preview', $route_params, $options); + $options = ['query' => $this->redirectDestination->getAsArray()]; + $form_state->setRedirect('entity.node.preview', $route_parameters, $options); } } diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php index 11d38ad..5e15c3d 100644 --- a/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\user\PrivateTempStoreFactory; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -27,10 +28,15 @@ class NodeForm extends ContentEntityForm { * The entity manager. * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory * The factory for the temp store object. + * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination + * The redirect destination service. + * + * @todo Make $redirect_destination required in Drupal 9.0.x. */ - public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory) { + public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, RedirectDestinationInterface $redirect_destination = NULL) { parent::__construct($entity_manager); $this->tempStoreFactory = $temp_store_factory; + $this->redirectDestination = $redirect_destination; } /** @@ -39,7 +45,8 @@ public function __construct(EntityManagerInterface $entity_manager, PrivateTempS public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), - $container->get('user.private_tempstore') + $container->get('user.private_tempstore'), + $container->get('redirect.destination') ); } @@ -78,9 +85,7 @@ public function form(array $form, FormStateInterface $form_state) { } // Cache the form state for submission request. - $form_state->setRequestMethod('POST'); - $form_state->setCached(); - + $form_state->setRequestMethod('POST')->setCached(); $this->entity = $preview->getFormObject()->getEntity(); $this->entity->in_preview = NULL; @@ -339,17 +344,18 @@ public function preview(array $form, FormStateInterface $form_state) { $this->entity->in_preview = TRUE; $store->set($this->entity->uuid(), $form_state); - $route_params = [ + $route_parameters = [ 'node_preview' => $this->entity->uuid(), 'view_mode_id' => 'default', ]; $options = []; - if ($destination = $this->getRequest()->query->get('destination')) { - $this->getRequest()->query->remove('destination'); - $options['query']['destination'] = $destination; + $query = $this->getRequest()->query; + if ($query->has('destination')) { + $options['query'] = $this->redirectDestination->getAsArray(); + $query->remove('destination'); } - $form_state->setRedirect('entity.node.preview', $route_params, $options); + $form_state->setRedirect('entity.node.preview', $route_parameters, $options); } /** diff --git a/core/modules/node/src/Tests/PagePreviewTest.php b/core/modules/node/src/Tests/PagePreviewTest.php index b5269d9..dab150d 100644 --- a/core/modules/node/src/Tests/PagePreviewTest.php +++ b/core/modules/node/src/Tests/PagePreviewTest.php @@ -6,6 +6,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Component\Utility\Unicode; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Url; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; @@ -261,14 +262,16 @@ function testPagePreview() { $this->clickLink(t('Back to content editing')); $this->assertRaw('edit-submit'); - // Check that destination is remembered when clicking on preview. When - // going back to the edit form and clicking save, we should go back to - // the original destination, if set. + // Check that destination is remembered when clicking on preview. When going + // back to the edit form and clicking save, we should go back to the + // original destination, if set. $destination = 'node'; - $this->drupalPostForm('node/' . $node->id() . '/edit', array(), t('Preview'), array('query' => array('destination' => $destination))); - $this->assertEqual($this->getUrl(), \Drupal::url('entity.node.preview', array('node_preview' => $node->uuid(), 'view_mode_id' => 'default'), array('absolute' => TRUE, 'query' => array('destination' => $destination)))); + $this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'), ['query' => ['destination' => $destination]]); + $parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'default']; + $options = ['absolute' => TRUE, 'query' => ['destination' => $destination]]; + $this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, $options)); $this->clickLink(t('Back to content editing')); - $this->drupalPostForm(NULL, array(), t('Save')); + $this->drupalPostForm(NULL, [], t('Save')); $this->assertUrl($destination); }