Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Determine if a migration is running
<?php
namespace Drupal\ras_comments;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateImportEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class CommentEventSubscriber.
*
* Sets a temporary variable to identify that this process is a migration
* process. Used during the comment mail hook to disable emails.
*
* @package Drupal\ras_comments
*/
class CommentEventSubscriber implements EventSubscriberInterface {
const DISABLE_VARIABLE = 'ras_comments_email_disable';
/**
* The drupal_static cache.
*
* @var array
*/
protected $staticCache;
/**
* CommentEventSubscriber constructor.
*/
public function __construct() {
$this->staticCache = &drupal_static(self::DISABLE_VARIABLE);
}
/**
* Get subscribed events.
*
* @inheritdoc
*/
public static function getSubscribedEvents() {
return [
MigrateEvents::PRE_IMPORT => 'onMigratePreImport',
MigrateEvents::POST_IMPORT => 'onMigratePostImport',
];
}
/**
* Listener onMigratePreImport.
*
* Set the blocking variable.
*
* @param \Drupal\migrate\Event\MigrateImportEvent $importEvent
* The import event object.
*/
public function onMigratePreImport(MigrateImportEvent $importEvent) {
if ($importEvent->getMigration()->getBaseId() == 'typo3_comment') {
$this->staticCache = TRUE;
}
}
/**
* Listener onMigratePostSave.
*
* Set the blocking variable to FALSE, in case anything else needs to create
* a comment in this process.
*
* @param \Drupal\migrate\Event\MigrateImportEvent $importEvent
* The import event object.
*/
public function onMigratePostImport(MigrateImportEvent $importEvent) {
if ($importEvent->getMigration()->getBaseId() == 'typo3_comment') {
$this->staticCache = FALSE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment