3 namespace Drupal\migrate\Plugin;
5 use Drupal\migrate\Event\ImportAwareInterface;
6 use Drupal\migrate\Event\MigrateEvents;
7 use Drupal\migrate\Event\MigrateImportEvent;
8 use Drupal\migrate\Event\MigrateRollbackEvent;
9 use Drupal\migrate\Event\RollbackAwareInterface;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13 * Event subscriber to forward Migrate events to source and destination plugins.
15 class PluginEventSubscriber implements EventSubscriberInterface {
18 * Tries to invoke event handling methods on source and destination plugins.
20 * @param string $method
21 * The method to invoke.
22 * @param \Drupal\migrate\Event\MigrateImportEvent|\Drupal\migrate\Event\MigrateRollbackEvent $event
23 * The event that has triggered the invocation.
24 * @param string $plugin_interface
25 * The interface which plugins must implement in order to be invoked.
27 protected function invoke($method, $event, $plugin_interface) {
28 $migration = $event->getMigration();
30 $source = $migration->getSourcePlugin();
31 if ($source instanceof $plugin_interface) {
32 call_user_func([$source, $method], $event);
35 $destination = $migration->getDestinationPlugin();
36 if ($destination instanceof $plugin_interface) {
37 call_user_func([$destination, $method], $event);
42 * Forwards pre-import events to the source and destination plugins.
44 * @param \Drupal\migrate\Event\MigrateImportEvent $event
47 public function preImport(MigrateImportEvent $event) {
48 $this->invoke('preImport', $event, ImportAwareInterface::class);
52 * Forwards post-import events to the source and destination plugins.
54 * @param \Drupal\migrate\Event\MigrateImportEvent $event
57 public function postImport(MigrateImportEvent $event) {
58 $this->invoke('postImport', $event, ImportAwareInterface::class);
62 * Forwards pre-rollback events to the source and destination plugins.
64 * @param \Drupal\migrate\Event\MigrateRollbackEvent $event
67 public function preRollback(MigrateRollbackEvent $event) {
68 $this->invoke('preRollback', $event, RollbackAwareInterface::class);
72 * Forwards post-rollback events to the source and destination plugins.
74 * @param \Drupal\migrate\Event\MigrateRollbackEvent $event
77 public function postRollback(MigrateRollbackEvent $event) {
78 $this->invoke('postRollback', $event, RollbackAwareInterface::class);
84 public static function getSubscribedEvents() {
86 $events[MigrateEvents::PRE_IMPORT][] = ['preImport'];
87 $events[MigrateEvents::POST_IMPORT][] = ['postImport'];
88 $events[MigrateEvents::PRE_ROLLBACK][] = ['preRollback'];
89 $events[MigrateEvents::POST_ROLLBACK][] = ['postRollback'];