diff --git a/core/modules/node/src/Form/NodePreviewForm.php b/core/modules/node/src/Form/NodePreviewForm.php index ee98221..784d36f 100644 --- a/core/modules/node/src/Form/NodePreviewForm.php +++ b/core/modules/node/src/Form/NodePreviewForm.php @@ -10,6 +10,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Routing\RedirectDestinationInterface; /** * Contains a form for switching the view mode of a node during preview. @@ -31,10 +32,21 @@ class NodePreviewForm extends FormBase implements ContainerInjectionInterface { protected $configFactory; /** + * The redirect destination. + * + * @var \Drupal\Core\Routing\RedirectDestinationInterface + */ + protected $redirectDestination; + + /** * {@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') + ); } /** @@ -45,9 +57,10 @@ public static function create(ContainerInterface $container) { * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The configuration factory. */ - public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) { + public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, RedirectDestinationInterface $redirect_destination) { $this->entityManager = $entity_manager; $this->configFactory = $config_factory; + $this->redirectDestination = $redirect_destination; } /** @@ -77,7 +90,7 @@ 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')) { + if ($destination = $this->redirectDestination->get()) { $query_options['query']['destination'] = $destination; } @@ -129,7 +142,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { 'view_mode_id' => $form_state->getValue('view_mode'), ]; $options = $this->getRequest()->query->all(); - if ($destination = $this->getRequest()->query->get('destination')) { + if ($destination = $this->redirectDestination->get()) { $this->getRequest()->query->remove('destination'); $options['query']['destination'] = $destination; } diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php index 00d7b06..f82f266 100644 --- a/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\user\PrivateTempStoreFactory; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Routing\RedirectDestinationInterface; /** * Form handler for the node edit forms. @@ -21,6 +22,13 @@ class NodeForm extends ContentEntityForm { protected $tempStoreFactory; /** + * The redirect destination. + * + * @var \Drupal\Core\Routing\RedirectDestinationInterface + */ + protected $redirectDestination; + + /** * Constructs a ContentEntityForm object. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager @@ -28,9 +36,10 @@ class NodeForm extends ContentEntityForm { * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory * The factory for the temp store object. */ - public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory) { + public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, RedirectDestinationInterface $redirect_destination) { parent::__construct($entity_manager); $this->tempStoreFactory = $temp_store_factory; + $this->redirectDestination = $redirect_destination; } /** @@ -39,7 +48,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') ); } @@ -345,7 +355,7 @@ public function preview(array $form, FormStateInterface $form_state) { ]; $options = []; - if ($destination = $this->getRequest()->query->get('destination')) { + if ($destination = $this->redirectDestination->get()) { $this->getRequest()->query->remove('destination'); $options['query']['destination'] = $destination; } diff --git a/core/modules/node/src/Tests/PagePreviewTest.php b/core/modules/node/src/Tests/PagePreviewTest.php index b5269d9..e69035a 100644 --- a/core/modules/node/src/Tests/PagePreviewTest.php +++ b/core/modules/node/src/Tests/PagePreviewTest.php @@ -180,7 +180,7 @@ function testPagePreview() { $this->assertFieldByName('field_image[0][alt]', 'Picture of llamas'); // Return to page preview to check everything is as expected. - $this->drupalPostForm(NULL, array(), t('Preview')); + $this->drupalPostForm(NULL, [], t('Preview')); $this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.'); $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.'); $this->assertText($edit[$body_key], 'Body displayed.'); @@ -257,7 +257,7 @@ function testPagePreview() { $node_type->save(); $this->drupalGet('node/add/page'); $this->assertNoRaw('edit-submit'); - $this->drupalPostForm('node/add/page', array($title_key => 'Preview'), t('Preview')); + $this->drupalPostForm('node/add/page', [$title_key => 'Preview'], t('Preview')); $this->clickLink(t('Back to content editing')); $this->assertRaw('edit-submit'); @@ -265,10 +265,10 @@ function testPagePreview() { // 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/' . $node->id() . '/edit', [], t('Preview'), array('query' => array('destination' => $destination))); + $this->assertUrl($this->getUrl(), ['node_preview' => $node->uuid(), 'view_mode_id' => 'default'], ['absolute' => TRUE, 'query' => array('destination' => $destination)]); $this->clickLink(t('Back to content editing')); - $this->drupalPostForm(NULL, array(), t('Save')); + $this->drupalPostForm(NULL, [], t('Save')); $this->assertUrl($destination); }