diff --git a/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php b/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php index 51ff961dff..685e54ea27 100644 --- a/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php +++ b/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php @@ -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; @@ -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', ]; } @@ -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. * @@ -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); + } + } + } diff --git a/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/MigrateEventSubscriber.php b/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/MigrateEventSubscriber.php new file mode 100644 index 0000000000..219b92bd3c --- /dev/null +++ b/docroot/modules/custom/va_gov_workflow/src/EventSubscriber/MigrateEventSubscriber.php @@ -0,0 +1,63 @@ +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); + } + +} diff --git a/docroot/modules/custom/va_gov_workflow/src/Service/Flagger.php b/docroot/modules/custom/va_gov_workflow/src/Service/Flagger.php index d61e5c01dc..a518907de9 100644 --- a/docroot/modules/custom/va_gov_workflow/src/Service/Flagger.php +++ b/docroot/modules/custom/va_gov_workflow/src/Service/Flagger.php @@ -28,6 +28,13 @@ class Flagger { */ protected $flagService; + /** + * Whether a migration is currently running. + * + * @var bool + */ + protected $isMigrating = FALSE; + /** * Constructs the flagger object. * @@ -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 @@ -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() ?? ''; @@ -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; } @@ -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. * diff --git a/docroot/modules/custom/va_gov_workflow/va_gov_workflow.services.yml b/docroot/modules/custom/va_gov_workflow/va_gov_workflow.services.yml index 3444c3fe48..88266f28b2 100644 --- a/docroot/modules/custom/va_gov_workflow/va_gov_workflow.services.yml +++ b/docroot/modules/custom/va_gov_workflow/va_gov_workflow.services.yml @@ -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 }