Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VACMS-17587: Fixes revision squashing during migrate #17637

Merged
merged 8 commits into from
Mar 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\core_event_dispatcher\EntityHookEvents;
use Drupal\core_event_dispatcher\Event\Entity\EntityDeleteEvent;
use Drupal\core_event_dispatcher\Event\Entity\EntityInsertEvent;
use Drupal\core_event_dispatcher\Event\Entity\EntityPresaveEvent;
use Drupal\core_event_dispatcher\Event\Entity\EntityUpdateEvent;
use Drupal\core_event_dispatcher\Event\Form\FormBaseAlterEvent;
use Drupal\node\NodeInterface;
Expand Down Expand Up @@ -71,6 +72,7 @@ public static function getSubscribedEvents(): array {
EntityHookEvents::ENTITY_DELETE => 'entityDelete',
EntityHookEvents::ENTITY_INSERT => 'entityInsert',
EntityHookEvents::ENTITY_UPDATE => 'entityUpdate',
EntityHookEvents::ENTITY_PRE_SAVE => 'entityPresave',
];
}

Expand Down Expand Up @@ -113,6 +115,19 @@ public function entityUpdate(EntityUpdateEvent $event): void {
$this->flagVaFormChanges($entity);
}

/**
* Entity Presave Event call.
*
* @param \Drupal\core_event_dispatcher\Event\Entity\EntityPresaveEvent $event
* The event.
*/
public function entityPresave(EntityPresaveEvent $event): void {
$entity = $event->getEntity();
if ($entity instanceof NodeInterface) {
$this->setNewRevisionDuringMigrate($entity);
}
}

/**
* Alters to be applied to all content types.
*
Expand Down Expand Up @@ -376,4 +391,21 @@ protected function isNodeIef(NodeInterface $node, $field_name): bool {
return (in_array($fieldType, $field_types_for_ief)) && ($target_type === "node");
}

/**
* Sets flags on an entity to create a new default revision.
*
* This is needed for migration to be able to set a new revision.
* {@see
* https://www.drupal.org/project/drupal/issues/3052115#comment-15476028}
*
* @param \Drupal\node\NodeInterface $entity
* The entity to set a revision for.
*/
protected function setNewRevisionDuringMigrate($entity) :void {
if ($entity->isSyncing() && !$entity->isNew() && $this->flagger->isMigrating()) {
$entity->setNewRevision(TRUE);
$entity->isDefaultRevision(TRUE);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Drupal\va_gov_workflow\EventSubscriber;

use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Drupal\migrate\Event\MigratePreRowSaveEvent;
use Drupal\va_gov_workflow\Service\Flagger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* VA.gov Workflow Migrate Event Subscriber.
*/
class MigrateEventSubscriber implements EventSubscriberInterface {

/**
* The flagger service.
*
* @var \Drupal\va_gov_workflow\Service\Flagger
*/
protected $flagger;

/**
* {@inheritdoc}
*/
public function __construct(Flagger $flagger) {
$this->flagger = $flagger;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[MigrateEvents::PRE_ROW_SAVE] = 'preRowSave';
$events[MigrateEvents::POST_ROW_SAVE] = 'postRowSave';
return $events;
}

/**
* Pre row save event call.
*
* @param \Drupal\migrate\Event\MigratePreRowSaveEvent $event
* The migrate import event.
*/
public function preRowSave(MigratePreRowSaveEvent $event) {
// Flag is already set.
if ($this->flagger->isMigrating()) {
return;
}
$this->flagger->setMigrating(TRUE);
}

/**
* Post row save event call.
*
* @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
* The migrate import event.
*/
public function postRowSave(MigratePostRowSaveEvent $event) {
$this->flagger->setMigrating(FALSE);
}

}
36 changes: 34 additions & 2 deletions docroot/modules/custom/va_gov_workflow/src/Service/Flagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class Flagger {
*/
protected $flagService;

/**
* Whether a migration is currently running.
*
* @var bool
*/
protected $isMigrating = FALSE;

/**
* Constructs the flagger object.
*
Expand Down Expand Up @@ -57,7 +64,7 @@ public function __construct(FlagService $flag_service, EntityTypeManagerInterfac
public function setFlag($flag_id, NodeInterface $node, $log_message = '', array $vars = []) {
// Don't set flags on a syncing operation, like updating a revision log
// after the fact, or we end up with recursion.
if (!$node->isSyncing()) {
if (!$node->isSyncing() || $this->isMigrating) {
$flag = $this->flagService->getFlagById($flag_id);
if ($flag && !$this->isTestData()) {
// NOTE: This setting of the flag only works in non-form based node
Expand Down Expand Up @@ -88,6 +95,11 @@ public function setFlag($flag_id, NodeInterface $node, $log_message = '', array
*/
public function updateRevisonLog(int $nid, $message, array $msg_vars = [], $prepend = FALSE) {
if (!empty($message) && !$this->isTestData()) {
// Toggle migrating flag off to prevent new revisions. (It will
// be turned back on with the next row).
if ($this->isMigrating()) {
$this->setMigrating(FALSE);
}
$revision = $this->getLatestRevision($nid);
if ($revision) {
$existing_message = $revision->getRevisionLogMessage() ?? '';
Expand Down Expand Up @@ -168,7 +180,7 @@ public function flagNew($flag_id, NodeInterface $node, $log_message = '') {
*/
public function flagFieldChanged($fieldname, $flag_id, NodeInterface $node, $log_message) {
$original = $this->getPreviousRevision($node);
if (!$original || $node->isSyncing()) {
if (!$original || ($node->isSyncing() && !$this->isMigrating)) {
// There is nothing to compare, so bail out.
return FALSE;
}
Expand Down Expand Up @@ -217,6 +229,26 @@ public function logFlagOperation(Flagging $flagging, $operation) {
}
}

/**
* Gets the status of the isMigrating flag.
*
* @return bool
* The isMigrating status.
*/
public function isMigrating(): bool {
return $this->isMigrating;
}

/**
* Sets the status of the isMigrating flag.
*
* @param bool $migrating
* The value to set the flag to.
*/
public function setMigrating(bool $migrating): void {
$this->isMigrating = $migrating;
}

/**
* Checks to see if this flag is loggable.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ services:
va_gov_workflow.workflow_content_control:
class: Drupal\va_gov_workflow\Service\WorkflowContentControl
arguments: ['@va_gov_user.user_perms']
va_gov_workflow.migrate_event_subscriber:
class: Drupal\va_gov_workflow\EventSubscriber\MigrateEventSubscriber
arguments: ['@va_gov_workflow.flagger']
tags:
- { name: event_subscriber }
Loading