Skip to content
Snippets Groups Projects
Commit c7da19c9 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2325463 by claudiu.cristea, swentel, GroovyCarrot, bircher,...

Issue #2325463 by claudiu.cristea, swentel, GroovyCarrot, bircher, aloknarwaria, lokapujya, Berdir, mohit_aghera, yanniboi, mgifford, BarisW, Yogesh Pawar: Destination URL breaks preview
parent 83018b75
No related branches found
No related tags found
No related merge requests found
...@@ -73,6 +73,11 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt ...@@ -73,6 +73,11 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt
$view_mode = $node->preview_view_mode; $view_mode = $node->preview_view_mode;
$query_options = array('query' => array('uuid' => $node->uuid())); $query_options = array('query' => array('uuid' => $node->uuid()));
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$query_options['query']['destination'] = $query->get('destination');
}
$form['backlink'] = array( $form['backlink'] = array(
'#type' => 'link', '#type' => 'link',
'#title' => $this->t('Back to content editing'), '#title' => $this->t('Back to content editing'),
...@@ -116,10 +121,18 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt ...@@ -116,10 +121,18 @@ public function buildForm(array $form, FormStateInterface $form_state, EntityInt
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRedirect('entity.node.preview', array( $route_parameters = [
'node_preview' => $form_state->getValue('uuid'), 'node_preview' => $form_state->getValue('uuid'),
'view_mode_id' => $form_state->getValue('view_mode'), 'view_mode_id' => $form_state->getValue('view_mode'),
)); ];
$options = [];
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options['query']['destination'] = $query->get('destination');
$query->remove('destination');
}
$form_state->setRedirect('entity.node.preview', $route_parameters, $options);
} }
} }
...@@ -22,11 +22,6 @@ class NodeForm extends ContentEntityForm { ...@@ -22,11 +22,6 @@ class NodeForm extends ContentEntityForm {
*/ */
protected $tempStoreFactory; protected $tempStoreFactory;
/**
* Whether this node has been previewed or not.
*/
protected $hasBeenPreviewed = FALSE;
/** /**
* Constructs a NodeForm object. * Constructs a NodeForm object.
* *
...@@ -84,7 +79,7 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -84,7 +79,7 @@ public function form(array $form, FormStateInterface $form_state) {
$this->entity = $preview->getFormObject()->getEntity(); $this->entity = $preview->getFormObject()->getEntity();
$this->entity->in_preview = NULL; $this->entity->in_preview = NULL;
$this->hasBeenPreviewed = TRUE; $form_state->set('has_been_previewed', TRUE);
} }
/** @var \Drupal\node\NodeInterface $node */ /** @var \Drupal\node\NodeInterface $node */
...@@ -186,7 +181,7 @@ protected function actions(array $form, FormStateInterface $form_state) { ...@@ -186,7 +181,7 @@ protected function actions(array $form, FormStateInterface $form_state) {
$node = $this->entity; $node = $this->entity;
$preview_mode = $node->type->entity->getPreviewMode(); $preview_mode = $node->type->entity->getPreviewMode();
$element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $this->hasBeenPreviewed; $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
// If saving is an option, privileged users get dedicated form submit // If saving is an option, privileged users get dedicated form submit
// buttons to adjust the publishing status while saving in one go. // buttons to adjust the publishing status while saving in one go.
...@@ -267,10 +262,19 @@ public function preview(array $form, FormStateInterface $form_state) { ...@@ -267,10 +262,19 @@ public function preview(array $form, FormStateInterface $form_state) {
$store = $this->tempStoreFactory->get('node_preview'); $store = $this->tempStoreFactory->get('node_preview');
$this->entity->in_preview = TRUE; $this->entity->in_preview = TRUE;
$store->set($this->entity->uuid(), $form_state); $store->set($this->entity->uuid(), $form_state);
$form_state->setRedirect('entity.node.preview', array(
$route_parameters = [
'node_preview' => $this->entity->uuid(), 'node_preview' => $this->entity->uuid(),
'view_mode_id' => 'default', 'view_mode_id' => 'default',
)); ];
$options = [];
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options['query']['destination'] = $query->get('destination');
$query->remove('destination');
}
$form_state->setRedirect('entity.node.preview', $route_parameters, $options);
} }
/** /**
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
...@@ -292,6 +293,29 @@ function testPagePreview() { ...@@ -292,6 +293,29 @@ function testPagePreview() {
$this->clickLink(t('Back to content editing')); $this->clickLink(t('Back to content editing'));
$this->assertRaw('edit-submit'); $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.
$destination = 'node';
$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->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch'));
$this->clickLink(t('Back to content editing'));
$this->drupalPostForm(NULL, [], t('Save'));
$this->assertUrl($destination);
// Check that preview page works as expected without a destination set.
$this->drupalPostForm($node->toUrl('edit-form'), [], t('Preview'));
$parameters = ['node_preview' => $node->uuid(), 'view_mode_id' => 'default'];
$this->assertUrl(Url::fromRoute('entity.node.preview', $parameters, ['absolute' => TRUE]));
$this->drupalPostForm(NULL, ['view_mode' => 'teaser'], t('Switch'));
$this->clickLink(t('Back to content editing'));
$this->drupalPostForm(NULL, [], t('Save'));
$this->assertUrl($node->toUrl());
$this->assertResponse(200);
// Assert multiple items can be added and are not lost when previewing. // Assert multiple items can be added and are not lost when previewing.
$test_image_1 = current($this->drupalGetTestFiles('image', 39325)); $test_image_1 = current($this->drupalGetTestFiles('image', 39325));
$edit_image_1['files[field_image_0][]'] = drupal_realpath($test_image_1->uri); $edit_image_1['files[field_image_0][]'] = drupal_realpath($test_image_1->uri);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment