How to 'revert' revision into 'draft' state

Created on 10 March 2021, almost 4 years ago
Updated 16 May 2024, 9 months ago

Drupal 9.1.4

My problem is that when a revision is reverted that it is published immediately instead of going through the workflow. So when an item is reverted, we want to set it back into the draft state. We had an issue when we reverted an item on a live site and it published immediately. What hook can I use to do this? Is it possible?

Been looking to add a patch to: Drupal\node\Form\NodeRevisionRevertForm.php

Can we use these hooks:
hook_entity_presave
hook_entity_revision_create

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
Node systemΒ  β†’

Last updated 9 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States oktboy

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States firewaller

    +1

  • πŸ‡ΊπŸ‡ΈUnited States firewaller

    FYI I was able to override this the core revert forms to enforce draft on new revisions by:

    1. Copying and renaming the "node.revision_revert_confirm" and "node.revision_revert_translation_confirm" routes from core's node.routing.yml to "MY_MODULE.node.revision_revert_confirm" and "MY_MODULE.node.revision_revert_translation_confirm"
    2. Creating a new file to extend the first class from the routes above here: /web/modules/custom/MY_MODULE/src/Form/NodeRevisionRevertForm.php
    3. Creating a new file to extend the second class from the routes above here: /web/modules/custom/MY_MODULE/src/Form/NodeRevisionRevertTranslationForm.php
    4. Overriding the route with the above new classes here: /web/modules/custom/MY_MODULE/src/Routing/RouteSubscriber.php
    5. Clearing caches

    NodeRevisionArchiveForm.php:

    <?php
    
    namespace Drupal\MY_MODULE\Form;
    
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\node\NodeInterface;
    use Drupal\node\Form\NodeRevisionRevertForm as NodeRevisionRevertCoreForm;
    
    /**
     * Provides a form for reverting a node revision.
     *
     * @internal
     */
    class NodeRevisionRevertForm extends NodeRevisionRevertCoreForm {
    
      /**
       * Prepares a revision to be reverted.
       *
       * @param \Drupal\node\NodeInterface $revision
       *   The revision to be reverted.
       * @param \Drupal\Core\Form\FormStateInterface $form_state
       *   The current state of the form.
       *
       * @return \Drupal\node\NodeInterface
       *   The prepared revision ready to be stored.
       */
      protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
        $revision = $this->enforceModerationState($revision, $form_state);
        return parent::prepareRevertedRevision($revision, $form_state);
      }
    
      /**
       * Enforce moderation state for a revision.
       *
       * @param \Drupal\node\NodeInterface $revision
       *   The revision to be reverted.
       * @param \Drupal\Core\Form\FormStateInterface $form_state
       *   The current state of the form.
       * @param string $state
       *   The moderation state to enforce.
       *
       * @return \Drupal\node\NodeInterface
       *   The prepared revision ready to be stored.
       */
      public function enforceModerationState(NodeInterface $revision, FormStateInterface $form_state, string $state = 'draft'): NodeInterface {
        if ($revision->hasField('moderation_state')) {
          $revision->set('moderation_state', $state);
        }
        return $revision;
      }
    
    }

    NodeRevisionRevertTranslationForm.php:

    <?php
    
    namespace Drupal\MY_MODULE\Form;
    
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\node\NodeInterface;
    use Drupal\node\Form\NodeRevisionRevertTranslationForm as NodeRevisionRevertTranslationCoreForm;
    
    /**
     * Provides a form for reverting a node revision for a single translation.
     *
     * @internal
     */
    class NodeRevisionRevertTranslationForm extends NodeRevisionRevertTranslationCoreForm {
    
      /**
       * {@inheritdoc}
       */
      protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
        $revision = $this->enforceModerationState($revision, $form_state);
        return parent::prepareRevertedRevision($revision, $form_state);
      }
    
      /**
       * Enforce moderation state for a revision.
       *
       * @param \Drupal\node\NodeInterface $revision
       *   The revision to be reverted.
       * @param \Drupal\Core\Form\FormStateInterface $form_state
       *   The current state of the form.
       * @param string $state
       *   The moderation state to enforce.
       *
       * @return \Drupal\node\NodeInterface
       *   The prepared revision ready to be stored.
       */
      public function enforceModerationState(NodeInterface $revision, FormStateInterface $form_state, string $state = 'draft'): NodeInterface {
        if ($revision->hasField('moderation_state')) {
          $revision->set('moderation_state', $state);
        }
        return $revision;
      }
    
    }

    RouteSubscriber.php:

    <?php
    
    namespace Drupal\MY_MODULE\Routing;
    
    use Drupal\Core\Routing\RouteSubscriberBase;
    use Symfony\Component\Routing\RouteCollection;
    
    /**
     * Route subscriber to subscribe to route event.
     */
    class RouteSubscriber extends RouteSubscriberBase {
    
      /**
       * {@inheritDoc}
       */
      protected function alterRoutes(RouteCollection $collection) {
        // Altering the node revert form.
        if ($route = $collection->get('node.revision_revert_confirm')) {
          $route->setDefault('_form', '\Drupal\MY_MODULE\Form\NodeRevisionRevertForm');
        }
    
        // Altering the node translation revert form.
        if ($route = $collection->get('node.revision_revert_translation_confirm')) {
          $route->setDefault('_form', '\Drupal\MY_MODULE\Form\NodeRevisionRevertTranslationForm');
        }
      }
    
    }
Production build 0.71.5 2024