public function SourceCsvForm::buildForm in Migrate Tools 8.4
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ SourceCsvForm.php, line 201
Class
- SourceCsvForm
- Provides an edit form for CSV source plugin column_names configuration.
Namespace
Drupal\migrate_tools\FormCode
public function buildForm(array $form, FormStateInterface $form_state, MigrationInterface $migration = NULL) {
try {
// @TODO: remove this horrible config work around after
// https://www.drupal.org/project/drupal/issues/2986665 is fixed.
$this->migration = $this->migrationPluginManager
->createInstance($migration
->id(), $migration
->toArray());
/** @var \Drupal\migrate_source_csv\Plugin\migrate\source\CSV $source */
$source = $this->migration
->getSourcePlugin();
$source
->setConfiguration($migration
->toArray()['source']);
} catch (PluginException $e) {
return AccessResult::forbidden();
}
// Get the source file after the properties are initialized.
$source
->initializeIterator();
$this->file = $source
->getFile();
// Set the input field options to the header row values or, if there are
// no such values, use an indexed array.
if ($this->file
->getHeaderRowCount() > 0) {
$this->options = $this
->getHeaderColumnNames();
}
else {
for ($i = 0; $i < $this
->getFileColumnCount(); $i++) {
$this->options[$i] = $i;
}
}
// Set the store key to the migration id.
$this->id = $this->migration
->getPluginId();
// Get the column names from the file or from the store, if updated
// values are in the store.
$this->sourceConfiguration = $this->store
->get($this->id);
if (isset($this->sourceConfiguration['changed'])) {
if ($config = $this->sourceConfiguration['changed']) {
$this->columnNames = $config;
}
}
else {
// Get the calculated column names. This is either the header rows or
// the configuration column_name value.
$this->columnNames = $this->file
->getColumnNames();
if (!isset($this->sourceConfiguration['original'])) {
// Save as the original values.
$this->sourceConfiguration['original'] = $this->columnNames;
$this->store
->set($this->id, $this->sourceConfiguration);
}
}
$form['#title'] = $this
->t('Column Aliases');
$form['heading'] = [
'#type' => 'item',
'#title' => $this
->t(':label', [
':label' => $this->migration
->label(),
]),
'#description' => '<p>' . $this
->t('You can change the columns to be used by this migration for each source property.') . '</p>',
];
// Create a form field for each column in this migration.
foreach ($this->columnNames as $index => $data) {
$property_name = key($data);
$default_value = $index;
$label = $this
->getLabel($this->sourceConfiguration['original'], $property_name);
$description = $this
->t('Select the column where the data for <em>:label</em>, property <em>:property</em>, will be found.', [
':label' => $label,
':property' => $property_name,
]);
$form['aliases'][$property_name] = [
'#type' => 'select',
'#title' => $label,
'#description' => $description,
'#options' => $this->options,
'#default_value' => $default_value,
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Submit'),
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#submit' => [
'::cancel',
],
'#limit_validation_errors' => [],
];
return $form;
}