Maybe someone can help me. I have created a custom module to start a migration of a csv file based on the code from the migration source ui module. Is it possible to update a field default value with data from my form submit ? I tried $migration->setProcessOfProperty($fieldname, $config) but cant get it to work. My Code looks like this:
class ExampleForm extends FormBase { /** * The migration plugin manager. * * @var DrupalmigratePluginMigrationPluginManager */ protected $pluginManagerMigration; /** * The migration definitions. * * @var array */ protected $definitions; /** * Config object for migrate_source_ui.settings. * * @var DrupalCoreConfigImmutableConfig */ protected $config; private $addFieldMapping; /** * MigrateSourceUiForm constructor. * * @param DrupalmigratePluginMigrationPluginManager $plugin_manager_migration * The migration plugin manager. * @param DrupalCoreConfigConfigFactoryInterface $config_factory * The config factory service. */ public function __construct(MigrationPluginManager $plugin_manager_migration, ConfigFactoryInterface $config_factory) { $this->pluginManagerMigration = $plugin_manager_migration; $this->definitions = $this->pluginManagerMigration->getDefinitions(); $this->config = $config_factory->get('migrate_source_ui.settings'); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('plugin.manager.migration'), $container->get('config.factory') ); } /** * {@inheritdoc} */ public function getFormId() { return 'confidence_migrate_example'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $options_verfahren = []; $options_verwalter = []; $verwalter_ids = Drupal::entityQuery('user') ->condition('status', 1) ->condition('roles', 'kunde') ->execute(); $verfahren = Drupal::entityTypeManager() ->getStorage('node') ->loadByProperties(['type' => 'verfahren']); $verwalter = Drupal::entityTypeManager() ->getStorage('user') ->loadMultiple($verwalter_ids); foreach ($verfahren as $node) { $options_verfahren[$node->id()] = $node->label(); } foreach ($verwalter as $user) { $options_verwalter[$user->id()] = $user->label(); } $options = []; foreach ($this->definitions as $definition) { $migrationInstance = $this->pluginManagerMigration->createStubMigration($definition); $aa3 = $migrationInstance; if ($migrationInstance->getSourcePlugin() instanceof CSV || $migrationInstance->getSourcePlugin() instanceof Json || $migrationInstance->getSourcePlugin() instanceof Xml) { $id = $definition['id']; $options[$id] = $this->t('%id (supports %file_type)', [ '%id' => $definition['label'] ?? $id, '%file_type' => $this->getFileExtensionSupported($migrationInstance), ]); } } $form['migrations'] = [ '#type' => 'select', '#title' => $this->t('Migrations'), '#options' => $options, ]; $form['verfahren'] = [ '#type' => 'select', '#title' => t('Verfahren'), '#options' => $options_verfahren, ]; $form['verwalter'] = [ '#type' => 'select', '#title' => t('Verwalter'), '#options' => $options_verwalter, ]; $form['source_file'] = [ '#type' => 'file', '#title' => $this->t('Upload the source file'), ]; $form['update_existing_records'] = [ '#type' => 'checkbox', '#title' => $this->t('Update existing records'), '#default_value' => 1, ]; $form['import'] = [ '#type' => 'submit', '#value' => $this->t('Importieren'), ]; return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); $migration_id = $form_state->getValue('migrations'); $definition = $this->pluginManagerMigration->getDefinition($migration_id); $migrationInstance = $this->pluginManagerMigration->createStubMigration($definition); $extension = $this->getFileExtensionSupported($migrationInstance); $validators = ['file_validate_extensions' => [$extension]]; // Check to see if a specific file temp directory is configured. If not, // default the value to FALSE, which will instruct file_save_upload() to // use Drupal's temporary files scheme. $file_destination = $this->config->get('file_temp_directory'); if (is_null($file_destination)) { $file_destination = FALSE; } $file = file_save_upload('source_file', $validators, $file_destination, 0, FileSystemInterface::EXISTS_REPLACE); $nid = $form_state->getValue('verfahren'); $uid = $form_state->getValue('verwalter'); if (isset($file)) { // File upload was attempted. if ($file) { $form_state->setValue('file_path', $file->getFileUri()); } // File upload failed. else { $form_state->setErrorByName('source_file', $this->t('The file could not be uploaded.')); } } else { $form_state->setErrorByName('source_file', $this->t('You have to upload a source file.')); } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $nid = $form_state->getValue('verfahren'); $uid = $form_state->getValue('verwalter'); $verwalter2 = [ 'plugin' => 'default_value', 'default_value' => $uid, ]; $migration_id = $form_state->getValue('migrations'); /** @var DrupalmigratePluginMigration $migration */ $migration = $this->pluginManagerMigration->createInstance($migration_id); $migration->setProcessOfProperty('field_akten_verwalter', $verwalter2); // Reset status. $status = $migration->getStatus(); if ($status !== MigrationInterface::STATUS_IDLE) { $migration->setStatus(MigrationInterface::STATUS_IDLE); $this->messenger() ->addWarning($this->t('Migration @id reset to Idle', ['@id' => $migration_id])); } $options = [ 'file_path' => $form_state->getValue('file_path'), ]; // Force updates or not. if ($form_state->getValue('update_existing_records')) { $options['update'] = TRUE; } $plugin_defination = $migration->getPluginDefinition(); $executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $options); $executable->batchImport(); } /** * The allowed file extension for the migration. * * @param DrupalmigratePluginMigration $migrationInstance * The migration instance. * * @return string * The file extension. */ public function getFileExtensionSupported(Migration $migrationInstance) { $extension = 'csv'; if ($migrationInstance->getSourcePlugin() instanceof CSV) { $extension = 'csv'; } elseif ($migrationInstance->getSourcePlugin() instanceof Json) { $extension = 'json'; } elseif ($migrationInstance->getSourcePlugin() instanceof Xml) { $extension = 'xml'; } return $extension; } }
Code from my .yml file:
id: confidence_migrate migration_tags: - CSV label: 'Daten der Akten importieren' source: plugin: csv ids: [Lfd. Nr.] delimiter: ";" header_offset: 0 process: title: Bezeichnung field_akten_halle: Halle field_akten_gang: Gang field_akten_jahrgang: Jahrgang field_akten_objekttyp: Objekttyp field_akten_regalboden: Regalboden field_akten_regalteil: Regalteil field_akten_beschreibung: Inhalt field_akten_lagerung_bis: plugin: format_date from_format: d.m.Y to_format: Y-m-d source: Lagerung field_akten_verwalter: plugin: default_value default_value: { } field_akten_auswahl_verfahren: plugin: default_value default_value: { } destination: plugin: 'entity:node' default_bundle: akten migration_dependencies: required: { } optional: { }
My CSV file looks like this:
Lfd. Nr.;Halle;Gang;Regalteil;Regalboden;Objekttyp;Jahrgang;Bezeichnung;Inhalt;Lagerung bis (Datum);nid;uid
I would appreciate any help thanks.