From 0540da60d6e30a99585a04bb362423c09b9dde4b Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Wed, 21 Feb 2024 17:02:37 -0800 Subject: [PATCH] Fix CSV upload importer form submission cached path --- src/Form/StanfordMigrateCsvImportForm.php | 56 ++++++++++------------- src/StanfordMigrateBatchExecutable.php | 7 ++- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Form/StanfordMigrateCsvImportForm.php b/src/Form/StanfordMigrateCsvImportForm.php index 4e31e05..7bd9108 100644 --- a/src/Form/StanfordMigrateCsvImportForm.php +++ b/src/Form/StanfordMigrateCsvImportForm.php @@ -5,6 +5,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityForm; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; use Drupal\Core\Session\AccountInterface; @@ -24,13 +25,6 @@ */ class StanfordMigrateCsvImportForm extends EntityForm { - /** - * Migration plugin manager service. - * - * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface - */ - protected $migrationManager; - /** * Migration plugin instance that matches the migration entity. * @@ -38,20 +32,6 @@ class StanfordMigrateCsvImportForm extends EntityForm { */ protected $migrationPlugin; - /** - * Core state service. - * - * @var \Drupal\Core\State\StateInterface - */ - protected $state; - - /** - * File module usage service. - * - * @var \Drupal\file\FileUsage\FileUsageInterface - */ - protected $fileUsage; - /** * {@inheritDoc} */ @@ -59,24 +39,23 @@ public static function create(ContainerInterface $container) { return new static( $container->get('plugin.manager.migration'), $container->get('state'), - $container->get('file.usage') + $container->get('file.usage'), + $container->get('entity_type.manager') ); } /** * StanfordMigrateCsvImportForm constructor. * - * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_manager + * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migrationManager * Migration plugin manager service. * @param \Drupal\Core\State\StateInterface $state * Core state service. - * @param \Drupal\file\FileUsage\FileUsageInterface $file_usage + * @param \Drupal\file\FileUsage\FileUsageInterface $fileUsage * File module usage service. */ - public function __construct(MigrationPluginManagerInterface $migration_manager, StateInterface $state, FileUsageInterface $file_usage) { - $this->migrationManager = $migration_manager; - $this->state = $state; - $this->fileUsage = $file_usage; + public function __construct(protected MigrationPluginManagerInterface $migrationManager, protected StateInterface $state, protected FileUsageInterface $fileUsage, EntityTypeManagerInterface $entityTypeManager) { + $this->entityTypeManager = $entityTypeManager; /** @var \Drupal\migrate_plus\Entity\MigrationInterface $migration */ $migration = $this->getRequest()->attributes->get('migration'); @@ -175,6 +154,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); + // When removing the original file, don't go through validating. if ( $form_state->getTriggeringElement()['#name'] == 'csv_remove_button' || @@ -232,8 +212,7 @@ public function save(array $form, FormStateInterface $form_state) { if ($form_state::hasAnyErrors()) { return; } - // Invalidate the migration cache since the file is changing. - Cache::invalidateTags(['migration_plugins']); + $this->migrationPlugin->getIdMap()->prepareUpdate(); $migration_id = $this->entity->id(); @@ -304,13 +283,28 @@ protected function fixLineBreaks($csv_path) { * Submitted form state. */ public function import(array $form, FormStateInterface $form_state) { + // Invalidate the migration cache since the file is changing. + $this->migrationManager->clearCachedDefinitions(); + Cache::invalidateTags(['migration_plugins']); + $migration_id = $this->entity->id(); $state = $this->state->get("stanford_migrate.csv.$migration_id", []); + if (!empty($state)) { + try { + /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */ $migration = $this->migrationManager->createInstance($migration_id); + $definition = $migration->getPluginDefinition(); + + $fid = $form_state->getValue(['csv', 0]); + $file = $this->entityTypeManager->getStorage('file')->load($fid); + $definition['source']['path'] = $file->getFileUri(); + + $options = ['configuration' => $definition]; + $migrateMessage = new MigrateMessage(); - $executable = new StanfordMigrateBatchExecutable($migration, $migrateMessage); + $executable = new StanfordMigrateBatchExecutable($migration, $migrateMessage, $options); $executable->batchImport(); } catch (\Exception $e) { diff --git a/src/StanfordMigrateBatchExecutable.php b/src/StanfordMigrateBatchExecutable.php index 3490a4d..e23a029 100644 --- a/src/StanfordMigrateBatchExecutable.php +++ b/src/StanfordMigrateBatchExecutable.php @@ -94,7 +94,12 @@ public static function batchProcessImport($migration_id, array $options, &$conte /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */ $migration = \Drupal::getContainer() ->get('plugin.manager.migration') - ->createInstance($migration_id); + ->createInstance($migration_id, $options); + + // Make sure the migration plugin has the passed configuration settings. + foreach ($options['configuration'] as $key => $value) { + $migration->set($key, $value); + } $executable = new StanfordMigrateBatchExecutable($migration, $message, $options);