Update migrate data from form submit value

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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Update migrate data from form submit value

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.