View source
<?php
namespace Drupal\migrate_tools\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\migrate_plus\Entity\MigrationGroup;
class MigrationFormBase extends EntityForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$migration = $this->entity;
$form['warning'] = [
'#markup' => $this
->t('Creating migrations is not yet supported. See <a href=":url">:url</a>', [
':url' => 'https://www.drupal.org/node/2573241',
]),
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $migration
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Machine name'),
'#default_value' => $migration
->id(),
'#machine_name' => [
'exists' => [
$this,
'exists',
],
'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
],
'#disabled' => !$migration
->isNew(),
];
$groups = MigrationGroup::loadMultiple();
$group_options = [];
foreach ($groups as $group) {
$group_options[$group
->id()] = $group
->label();
}
if (!$migration->migration_group && isset($group_options['default'])) {
$migration
->set('migration_group', 'default');
}
$form['migration_group'] = [
'#type' => 'select',
'#title' => $this
->t('Migration Group'),
'#empty_value' => '',
'#default_value' => $migration->migration_group,
'#options' => $group_options,
'#description' => $this
->t('Assign this migration to an existing group.'),
];
return $form;
}
public function exists($entity_id, array $element, FormStateInterface $form_state) {
$query = $this->entityTypeManager
->getStorage('migration')
->getQuery();
$result = $query
->condition('id', $element['#field_prefix'] . $entity_id)
->execute();
return (bool) $result;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save');
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$migration = $this
->getEntity();
$status = $migration
->save();
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addStatus($this
->t('Migration %label has been updated.', [
'%label' => $migration
->label(),
]));
}
else {
$this
->messenger()
->addStatus($this
->t('Migration %label has been added.', [
'%label' => $migration
->label(),
]));
}
$form_state
->setRedirect('entity.migration.list', [
'migration_group' => $migration
->get('migration_group'),
]);
}
}