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

HPC-9306: Update articles and documents during migration #829

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions html/modules/custom/ghi_content/ghi_content.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ services:
class: Drupal\ghi_content\EventSubscriber\RouteSubscriber
tags:
- { name: event_subscriber }
ghi_content.post_row_save_subscriber:
class: '\Drupal\ghi_content\EventSubscriber\PostRowSaveEventSubscriber'
arguments: ['@entity_type.manager', '@ghi_content.manager.factory']
tags:
- { name: 'event_subscriber' }
ghi_content.manager.factory:
class: Drupal\ghi_content\ContentManager\ManagerFactory
arguments: ['@ghi_content.manager.article', '@ghi_content.manager.document']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Drupal\ghi_content\EventSubscriber;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\ghi_content\ContentManager\ManagerFactory;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Reacting to content nodes beeing imported/updated.
*
* @package Drupal\ghi_content\EventSubscriber
*/
class PostRowSaveEventSubscriber implements EventSubscriberInterface {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The remote source manager.
*
* @var \Drupal\ghi_content\ContentManager\ManagerFactory
*/
protected $managerFactory;

/**
* Create an instance of the class.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Core's entity query.
* @param \Drupal\ghi_content\ContentManager\ManagerFactory $manager_factory
* Core's entity query.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ManagerFactory $manager_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->managerFactory = $manager_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('ghi_content.manager.factory'),
);
}

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

/**
* React to an entity having been saved during a migration run.
*
* Check update imported or updated article or document nodes with their
* version from the remote source system.
*
* @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
* The event object.
*/
public function onMigratePostRowSave(MigratePostRowSaveEvent $event) {
$migration_ids = [
'articles_hpc_content_module',
'documents_hpc_content_module',
];
if (!in_array($event->getMigration()->id(), $migration_ids)) {
return;
}
$ids = $event->getDestinationIdValues();
$entities = $this->entityTypeManager->getStorage('node')->loadMultiple($ids);
foreach ($entities as $entity) {
$content_manager = $this->managerFactory->getContentManager($entity);
if (!$content_manager) {
continue;
}
$content_manager->updateNodeFromRemote($entity);
$content_manager->saveContentNode($entity);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public function query($payload) {
'body' => $body,
'headers' => $headers,
'cookies' => $jar,
'timeout' => 60000,
];

// See if we have a cached version already for this request.
Expand Down
Loading