Skip to content

Commit

Permalink
Fix CSV upload importer form submission cached path
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Feb 22, 2024
1 parent d1fd555 commit 0540da6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
56 changes: 25 additions & 31 deletions src/Form/StanfordMigrateCsvImportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,59 +25,37 @@
*/
class StanfordMigrateCsvImportForm extends EntityForm {

/**
* Migration plugin manager service.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migrationManager;

/**
* Migration plugin instance that matches the migration entity.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migrationPlugin;

/**
* Core state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;

/**
* File module usage service.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;

/**
* {@inheritDoc}
*/
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');
Expand Down Expand Up @@ -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' ||
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion src/StanfordMigrateBatchExecutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 0540da6

Please sign in to comment.