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;
10 use Drupal\migrate\Plugin\MigrationInterface;
13 * Class MigrationFormBase.
15 * @package Drupal\migrate_tools\Form
17 * @ingroup migrate_tools
19 class MigrationFormBase extends EntityForm {
22 * @var \Drupal\Core\Entity\Query\QueryFactory
24 protected $entityQueryFactory;
27 * Construct the MigrationGroupFormBase.
29 * For simple entity forms, there's no need for a constructor. Our migration form
30 * base, however, requires an entity query factory to be injected into it
31 * from the container. We later use this query factory to build an entity
32 * query for the exists() method.
34 * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
35 * An entity query factory for the migration group entity type.
37 public function __construct(QueryFactory $query_factory) {
38 $this->entityQueryFactory = $query_factory;
42 * Factory method for MigrationFormBase.
44 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
45 * A container interface service.
47 * @return \Drupal\migrate_tools\Form\MigrationGroupFormBase
49 public static function create(ContainerInterface $container) {
50 return new static($container->get('entity.query'));
54 * Overrides Drupal\Core\Entity\EntityFormController::form().
56 * Builds the entity add/edit form.
59 * An associative array containing the structure of the form.
60 * @param \Drupal\Core\Form\FormStateInterface $form_state
61 * An associative array containing the current state of the form.
64 * An associative array containing the migration add/edit form.
66 public function buildForm(array $form, FormStateInterface $form_state) {
67 // Get anything we need from the base class.
68 $form = parent::buildForm($form, $form_state);
70 /** @var MigrationInterface $migration */
71 $migration = $this->entity;
74 '#markup' => $this->t('Creating migrations is not yet supported. See <a href=":url">:url</a>', [
75 ':url' => 'https://www.drupal.org/node/2573241',
80 $form['label'] = array(
81 '#type' => 'textfield',
82 '#title' => $this->t('Label'),
84 '#default_value' => $migration->label(),
88 '#type' => 'machine_name',
89 '#title' => $this->t('Machine name'),
90 '#default_value' => $migration->id(),
91 '#machine_name' => array(
92 'exists' => array($this, 'exists'),
93 'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
94 '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".',
96 '#disabled' => !$migration->isNew(),
99 $groups = MigrationGroup::loadMultiple();
101 foreach ($groups as $group) {
102 $group_options[$group->id()] = $group->label();
104 if (!$migration->get('migration_group') && isset($group_options['default'])) {
105 $migration->set('migration_group', 'default');
108 $form['migration_group'] = array(
110 '#title' => $this->t('Migration Group'),
111 '#empty_value' => '',
112 '#default_value' => $migration->get('migration_group'),
113 '#options' => $group_options,
114 '#description' => $this->t('Assign this migration to an existing group.'),
121 * Checks for an existing migration group.
123 * @param string|int $entity_id
125 * @param array $element
127 * @param FormStateInterface $form_state
131 * TRUE if this format already exists, FALSE otherwise.
133 public function exists($entity_id, array $element, FormStateInterface $form_state) {
134 // Use the query factory to build a new migration entity query.
135 $query = $this->entityQueryFactory->get('migration');
137 // Query the entity ID to see if its in use.
138 $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
141 // We don't need to return the ID, only if it exists or not.
142 return (bool) $result;
146 * Overrides Drupal\Core\Entity\EntityFormController::actions().
149 * An associative array containing the structure of the form.
150 * @param \Drupal\Core\Form\FormStateInterface $form_state
151 * An associative array containing the current state of the form.
154 * An array of supported actions for the current entity form.
156 protected function actions(array $form, FormStateInterface $form_state) {
157 // Get the basic actins from the base class.
158 $actions = parent::actions($form, $form_state);
160 // Change the submit button text.
161 $actions['submit']['#value'] = $this->t('Save');
163 // Return the result.
168 * Overrides Drupal\Core\Entity\EntityFormController::save().
171 * An associative array containing the structure of the form.
172 * @param \Drupal\Core\Form\FormStateInterface $form_state
173 * An associative array containing the current state of the form.
177 public function save(array $form, FormStateInterface $form_state) {
178 $migration = $this->getEntity();
179 $status = $migration->save();
181 if ($status == SAVED_UPDATED) {
182 // If we edited an existing entity...
183 drupal_set_message($this->t('Migration %label has been updated.', array('%label' => $migration->label())));
186 // If we created a new entity...
187 drupal_set_message($this->t('Migration %label has been added.', array('%label' => $migration->label())));
190 // Redirect the user back to the listing route after the save operation.
191 $form_state->setRedirect('entity.migration.list',
192 array('migration_group' => $migration->get('migration_group')));