Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Form / MigrationFormBase.php
1 <?php
2
3 namespace Drupal\migrate_tools\Form;
4
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
11 /**
12  * Class MigrationFormBase.
13  *
14  * @package Drupal\migrate_tools\Form
15  *
16  * @ingroup migrate_tools
17  */
18 class MigrationFormBase extends EntityForm {
19
20   /**
21    * The entity query factory.
22    *
23    * @var \Drupal\Core\Entity\Query\QueryFactory
24    */
25   protected $entityQueryFactory;
26
27   /**
28    * Construct the MigrationGroupFormBase.
29    *
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.
34    *
35    * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
36    *   An entity query factory for the migration group entity type.
37    */
38   public function __construct(QueryFactory $query_factory) {
39     $this->entityQueryFactory = $query_factory;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container) {
46     return new static($container->get('entity.query'));
47   }
48
49   /**
50    * Overrides Drupal\Core\Entity\EntityFormController::form().
51    *
52    * Builds the entity add/edit form.
53    *
54    * @param array $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.
58    *
59    * @return array
60    *   An associative array containing the migration add/edit form.
61    */
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);
65
66     /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
67     $migration = $this->entity;
68
69     $form['warning'] = [
70       '#markup' => $this->t('Creating migrations is not yet supported. See <a href=":url">:url</a>', [
71         ':url' => 'https://www.drupal.org/node/2573241',
72       ]),
73     ];
74
75     // Build the form.
76     $form['label'] = [
77       '#type' => 'textfield',
78       '#title' => $this->t('Label'),
79       '#maxlength' => 255,
80       '#default_value' => $migration->label(),
81       '#required' => TRUE,
82     ];
83     $form['id'] = [
84       '#type' => 'machine_name',
85       '#title' => $this->t('Machine name'),
86       '#default_value' => $migration->id(),
87       '#machine_name' => [
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".',
91       ],
92       '#disabled' => !$migration->isNew(),
93     ];
94
95     $groups = MigrationGroup::loadMultiple();
96     $group_options = [];
97     foreach ($groups as $group) {
98       $group_options[$group->id()] = $group->label();
99     }
100     if (!$migration->get('migration_group') && isset($group_options['default'])) {
101       $migration->set('migration_group', 'default');
102     }
103
104     $form['migration_group'] = [
105       '#type' => 'select',
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.'),
111     ];
112
113     return $form;
114   }
115
116   /**
117    * Checks for an existing migration group.
118    *
119    * @param string|int $entity_id
120    *   The entity ID.
121    * @param array $element
122    *   The form element.
123    * @param \Drupal\Core\Form\FormStateInterface $form_state
124    *   The form state.
125    *
126    * @return bool
127    *   TRUE if this format already exists, FALSE otherwise.
128    */
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');
132
133     // Query the entity ID to see if its in use.
134     $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
135       ->execute();
136
137     // We don't need to return the ID, only if it exists or not.
138     return (bool) $result;
139   }
140
141   /**
142    * Overrides Drupal\Core\Entity\EntityFormController::actions().
143    *
144    * @param array $form
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.
148    *
149    * @return array
150    *   An array of supported actions for the current entity form.
151    */
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);
155
156     // Change the submit button text.
157     $actions['submit']['#value'] = $this->t('Save');
158
159     // Return the result.
160     return $actions;
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function save(array $form, FormStateInterface $form_state) {
167     $migration = $this->getEntity();
168     $status = $migration->save();
169
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()]));
173     }
174     else {
175       // If we created a new entity...
176       drupal_set_message($this->t('Migration %label has been added.', ['%label' => $migration->label()]));
177     }
178
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')]);
182   }
183
184 }