Skip to content

Commit

Permalink
Merge pull request #829 from UN-OCHA/berliner/HPC-9306
Browse files Browse the repository at this point in the history
HPC-9306: Update articles and documents during migration
  • Loading branch information
berliner authored Nov 24, 2023
2 parents 10546c9 + 6b58850 commit f6824bd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
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

0 comments on commit f6824bd

Please sign in to comment.