- πΊπΈUnited States firewaller
FYI I was able to override this the core revert forms to enforce draft on new revisions by:
- 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"
- Creating a new file to extend the first class from the routes above here: /web/modules/custom/MY_MODULE/src/Form/NodeRevisionRevertForm.php
- Creating a new file to extend the second class from the routes above here: /web/modules/custom/MY_MODULE/src/Form/NodeRevisionRevertTranslationForm.php
- Overriding the route with the above new classes here: /web/modules/custom/MY_MODULE/src/Routing/RouteSubscriber.php
- 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'); } } }