Send email after Migrate Import in Drupal 8
Here’s an example of a barebones event subscriber to send an email when a Drupal migration finishes importing. The success vs failure logic isn’t working correctly, but you’ll get the gist. If you come up with a solution for this please contact me; I don’t have time to work on it at the moment.
The file structure looks like this:
mymodule.services.yml
1 2 3 4 5 |
services: mymodule_migrate.subscriber: class: Drupal\mymodule_migrate\EventSubscriber\MymoduleMigrateSubscriber tags: - { name: event_subscriber } |
mymodule.module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/** * Implements hook_mail(). */ function mymodule_migrate_mail($key, &$message, $params) { $options = array( 'langcode' => $message['langcode'], ); $message['from'] = \Drupal::config('system.site')->get('mail'); switch ($key) { case 'migrate_success': $message['subject'] = t('Migration @name finished successfully.', [ '@name' => $params['migration_name'], ], $options); $message['body'][] = t('All @source_count records have been processed.', [ '@source_count' => $params['source_count'], ], $options); break; case 'migrate_failure': $message['subject'] = t('Migration @name failed.', [ '@name' => $params['migration_name'], ], $options); $message['body'][] = t('@source_count records exist, but only @processed_count records have been processed.', [ '@source_count' => $params['source_count'], '@processed_count' => $params['processed_count'], ], $options); break; } } |
MymoduleMigrateSubscriber.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php namespace Drupal\mymodule_migrate\EventSubscriber; use Drupal\migrate\Event\MigrateEvents; use Drupal\migrate\Event\MigrateImportEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Class MigrationEvents. * * @package Drupal\mymodule_migrate */ class MymoduleMigrateSubscriber implements EventSubscriberInterface { /** * Constructs a new MigrationEvents object. */ public function __construct() { } /** * {@inheritdoc} */ public static function getSubscribedEvents() { return [ MigrateEvents::POST_IMPORT => 'onMigratePostImport', ]; } /** * Sends an email after a migration is finished. * * @param MigrateImportEvent $event * Import event. */ public function onMigratePostImport(MigrateImportEvent $event) { $mail_manager = \Drupal::service('plugin.manager.mail'); $to = 'you@youremail.com'; $langcode = \Drupal::currentUser()->getPreferredLangcode(); $params['migration_name'] = $event->getMigration()->label(); $params['migration_id'] = $event->getMigration()->id(); $params['source_count'] = $event->getMigration()->getSourcePlugin()->count(); $params['processed_count'] = $event->getMigration()->getIdMap()->processedCount(); // This is crude and may need improvement. $type = 'migrate_failure'; if ($params['source_count'] <= $params['processed_count']) { $type = 'migrate_success'; } $mail_manager->mail('mymodule_migrate', $type, $to, $langcode, $params, NULL, TRUE); } } |