Skip to content
Snippets Groups Projects
Commit 2cda1fb4 authored by Jesus Manuel Olivas's avatar Jesus Manuel Olivas
Browse files

:sparkles: Issue #3450267: Add support for Preview Draft Content

parent a251b608
No related branches found
No related tags found
1 merge request!16✨ Issue #3450267: Add support for Preview Draft Content
Pipeline #183818 passed with warnings
...@@ -25,13 +25,15 @@ class NodeEditForm extends NodeForm { ...@@ -25,13 +25,15 @@ class NodeEditForm extends NodeForm {
* Overridden to store the root parent entity. * Overridden to store the root parent entity.
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
// Calculate if preview | latest published revision.
$this->calculateNodeEntity($form, $form_state);
// Build the form.
$form = parent::buildForm($form, $form_state); $form = parent::buildForm($form, $form_state);
$node = $this->calculateNodeEntity($form, $form_state);
// Hide Preview button.
$form['actions']['preview']['#access'] = $this->isLatestRevision();
// Hide Delete button. // Hide Delete button.
$form['actions']['delete']['#access'] = FALSE; $form['actions']['delete']['#access'] = FALSE;
// Hide advanced tab.
$form['advanced']['#attributes']['class'][] = 'visually-hidden';
// Add ajax callback to submit button. // Add ajax callback to submit button.
$form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit'; $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
// @todo static::ajaxSubmit() requires data-drupal-selector to be the same // @todo static::ajaxSubmit() requires data-drupal-selector to be the same
......
...@@ -9,6 +9,23 @@ namespace Drupal\visual_editor; ...@@ -9,6 +9,23 @@ namespace Drupal\visual_editor;
*/ */
trait FormEditTrait { trait FormEditTrait {
/**
* Stores the isLatestRevision status.
*
* @var bool
*/
protected $isLatestRevision = FALSE;
/**
* Get the isLatestRevision status.
*
* @return bool
* The isLatestRevision status.
*/
public function isLatestRevision() {
return $this->isLatestRevision;
}
/** /**
* Sets the entity for sidebar edit form. * Sets the entity for sidebar edit form.
* *
...@@ -17,19 +34,43 @@ trait FormEditTrait { ...@@ -17,19 +34,43 @@ trait FormEditTrait {
* @param \Drupal\Core\Form\FormStateInterface $form_state * @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object. * The form state object.
* *
* @return \Drupal\Core\Entity\ContentEntityInterface * @return void
* The node object to return. * Return nothing.
*/ */
public function calculateNodeEntity(&$form, &$form_state) { public function calculateNodeEntity(&$form, &$form_state) {
// Check if the entity is not the latest published revision.
$entity = $form_state->getFormObject()->getEntity();
// Set the isLatestRevision status.
$this->isLatestRevision = $entity->isLatestRevision();
// If not the latest revision, load the latest revision.
if (!$entity->isLatestRevision()) {
$revisionId = $this->entityTypeManager
->getStorage('node')
->getLatestRevisionId($entity->id());
$node = $this->entityTypeManager
->getStorage('node')
->loadRevision($revisionId);
$form_state->getFormObject()->setEntity($node);
$form_state->setRebuild();
$this->entity = $node;
return;
}
// Check if the form is in preview mode.
$query = \Drupal::request()->query; $query = \Drupal::request()->query;
$is_preview = $query->has('preview') && $query->get('preview') == 'true'; $isPreview = $query->has('preview') && $query->get('preview') == 'true';
// Try to restore from temp store, this must be done before calling // Try to restore from temp store, this must be done before calling
// parent::form(). // parent::form().
$store = $this->tempStoreFactory->get('node_preview'); $store = $this->tempStoreFactory->get('node_preview');
$node_uuid = $form_state->getFormObject()->getEntity()->uuid(); $uuid = $form_state->getFormObject()->getEntity()->uuid();
$preview = $store->get($uuid);
if ($is_preview && !$form_state->isRebuilding() && $node_uuid && $preview = $store->get($node_uuid)) { if ($isPreview && !$form_state->isRebuilding() && $uuid && $preview) {
/** @var \Drupal\Core\Form\FormStateInterface $preview */ /** @var \Drupal\Core\Form\FormStateInterface $preview */
$form_state->setStorage($preview->getStorage()); $form_state->setStorage($preview->getStorage());
$form_state->setUserInput($preview->getUserInput()); $form_state->setUserInput($preview->getUserInput());
...@@ -46,14 +87,10 @@ trait FormEditTrait { ...@@ -46,14 +87,10 @@ trait FormEditTrait {
$this->entity->in_preview = NULL; $this->entity->in_preview = NULL;
$form_state->set('has_been_previewed', TRUE); $form_state->set('has_been_previewed', TRUE);
return;
return $this->entity;
}
else {
$this->entity = $form_state->getFormObject()->getEntity();
return $form_state->getFormObject()->getEntity();
} }
$this->entity = $form_state->getFormObject()->getEntity();
} }
} }
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