Skip to content

Instantly share code, notes, and snippets.

@weitzman
Created April 20, 2017 14:12
Show Gist options
  • Save weitzman/9b2f395e6bd9f266e9f9c2df1c8f287b to your computer and use it in GitHub Desktop.
Save weitzman/9b2f395e6bd9f266e9f9c2df1c8f287b to your computer and use it in GitHub Desktop.
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