3 namespace Drupal\migrate_tools\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\Query\QueryFactory;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\migrate_plus\Entity\MigrationGroup;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Class MigrationFormBase.
14 * @package Drupal\migrate_tools\Form
16 * @ingroup migrate_tools
18 class MigrationFormBase extends EntityForm {
21 * The entity query factory.
23 * @var \Drupal\Core\Entity\Query\QueryFactory
25 protected $entityQueryFactory;
28 * Construct the MigrationGroupFormBase.
30 * For simple entity forms, there's no need for a constructor. Our migration
31 * form base, however, requires an entity query factory to be injected into it
32 * from the container. We later use this query factory to build an entity
33 * query for the exists() method.
35 * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
36 * An entity query factory for the migration group entity type.
38 public function __construct(QueryFactory $query_factory) {
39 $this->entityQueryFactory = $query_factory;
45 public static function create(ContainerInterface $container) {
46 return new static($container->get('entity.query'));
50 * Overrides Drupal\Core\Entity\EntityFormController::form().
52 * Builds the entity add/edit form.
55 * An associative array containing the structure of the form.
56 * @param \Drupal\Core\Form\FormStateInterface $form_state
57 * An associative array containing the current state of the form.
60 * An associative array containing the migration add/edit form.
62 public function buildForm(array $form, FormStateInterface $form_state) {
63 // Get anything we need from the base class.
64 $form = parent::buildForm($form, $form_state);
66 /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
67 $migration = $this->entity;
70 '#markup' => $this->t('Creating migrations is not yet supported. See <a href=":url">:url</a>', [
71 ':url' => 'https://www.drupal.org/node/2573241',
77 '#type' => 'textfield',
78 '#title' => $this->t('Label'),
80 '#default_value' => $migration->label(),
84 '#type' => 'machine_name',
85 '#title' => $this->t('Machine name'),
86 '#default_value' => $migration->id(),
88 'exists' => [$this, 'exists'],
89 'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
90 '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".',
92 '#disabled' => !$migration->isNew(),
95 $groups = MigrationGroup::loadMultiple();
97 foreach ($groups as $group) {
98 $group_options[$group->id()] = $group->label();
100 if (!$migration->get('migration_group') && isset($group_options['default'])) {
101 $migration->set('migration_group', 'default');
104 $form['migration_group'] = [
106 '#title' => $this->t('Migration Group'),
107 '#empty_value' => '',
108 '#default_value' => $migration->get('migration_group'),
109 '#options' => $group_options,
110 '#description' => $this->t('Assign this migration to an existing group.'),
117 * Checks for an existing migration group.
119 * @param string|int $entity_id
121 * @param array $element
123 * @param \Drupal\Core\Form\FormStateInterface $form_state
127 * TRUE if this format already exists, FALSE otherwise.
129 public function exists($entity_id, array $element, FormStateInterface $form_state) {
130 // Use the query factory to build a new migration entity query.
131 $query = $this->entityQueryFactory->get('migration');
133 // Query the entity ID to see if its in use.
134 $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
137 // We don't need to return the ID, only if it exists or not.
138 return (bool) $result;
142 * Overrides Drupal\Core\Entity\EntityFormController::actions().
145 * An associative array containing the structure of the form.
146 * @param \Drupal\Core\Form\FormStateInterface $form_state
147 * An associative array containing the current state of the form.
150 * An array of supported actions for the current entity form.
152 protected function actions(array $form, FormStateInterface $form_state) {
153 // Get the basic actins from the base class.
154 $actions = parent::actions($form, $form_state);
156 // Change the submit button text.
157 $actions['submit']['#value'] = $this->t('Save');
159 // Return the result.
166 public function save(array $form, FormStateInterface $form_state) {
167 $migration = $this->getEntity();
168 $status = $migration->save();
170 if ($status == SAVED_UPDATED) {
171 // If we edited an existing entity...
172 drupal_set_message($this->t('Migration %label has been updated.', ['%label' => $migration->label()]));
175 // If we created a new entity...
176 drupal_set_message($this->t('Migration %label has been added.', ['%label' => $migration->label()]));
179 // Redirect the user back to the listing route after the save operation.
180 $form_state->setRedirect('entity.migration.list',
181 ['migration_group' => $migration->get('migration_group')]);