Version 1
[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 use Drupal\migrate\Plugin\MigrationInterface;
11
12 /**
13  * Class MigrationFormBase.
14  *
15  * @package Drupal\migrate_tools\Form
16  *
17  * @ingroup migrate_tools
18  */
19 class MigrationFormBase extends EntityForm {
20
21   /**
22    * @var \Drupal\Core\Entity\Query\QueryFactory
23    */
24   protected $entityQueryFactory;
25
26   /**
27    * Construct the MigrationGroupFormBase.
28    *
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.
33    *
34    * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
35    *   An entity query factory for the migration group entity type.
36    */
37   public function __construct(QueryFactory $query_factory) {
38     $this->entityQueryFactory = $query_factory;
39   }
40
41   /**
42    * Factory method for MigrationFormBase.
43    *
44    * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
45    *   A container interface service.
46    *
47    * @return \Drupal\migrate_tools\Form\MigrationGroupFormBase
48    */
49   public static function create(ContainerInterface $container) {
50     return new static($container->get('entity.query'));
51   }
52
53   /**
54    * Overrides Drupal\Core\Entity\EntityFormController::form().
55    *
56    * Builds the entity add/edit form.
57    *
58    * @param array $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.
62    *
63    * @return array
64    *   An associative array containing the migration add/edit form.
65    */
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);
69
70     /** @var MigrationInterface $migration */
71     $migration = $this->entity;
72
73     $form['warning'] = [
74       '#markup' => $this->t('Creating migrations is not yet supported. See <a href=":url">:url</a>', [
75         ':url' => 'https://www.drupal.org/node/2573241',
76       ])
77     ];
78
79     // Build the form.
80     $form['label'] = array(
81       '#type' => 'textfield',
82       '#title' => $this->t('Label'),
83       '#maxlength' => 255,
84       '#default_value' => $migration->label(),
85       '#required' => TRUE,
86     );
87     $form['id'] = array(
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".',
95       ),
96       '#disabled' => !$migration->isNew(),
97     );
98
99     $groups = MigrationGroup::loadMultiple();
100     $group_options = [];
101     foreach ($groups as $group) {
102       $group_options[$group->id()] = $group->label();
103     }
104     if (!$migration->get('migration_group') && isset($group_options['default'])) {
105       $migration->set('migration_group', 'default');
106     }
107
108     $form['migration_group'] = array(
109       '#type' => 'select',
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.'),
115     );
116
117     return $form;
118   }
119
120   /**
121    * Checks for an existing migration group.
122    *
123    * @param string|int $entity_id
124    *   The entity ID.
125    * @param array $element
126    *   The form element.
127    * @param FormStateInterface $form_state
128    *   The form state.
129    *
130    * @return bool
131    *   TRUE if this format already exists, FALSE otherwise.
132    */
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');
136
137     // Query the entity ID to see if its in use.
138     $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
139       ->execute();
140
141     // We don't need to return the ID, only if it exists or not.
142     return (bool) $result;
143   }
144
145   /**
146    * Overrides Drupal\Core\Entity\EntityFormController::actions().
147    *
148    * @param array $form
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.
152    *
153    * @return array
154    *   An array of supported actions for the current entity form.
155    */
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);
159
160     // Change the submit button text.
161     $actions['submit']['#value'] = $this->t('Save');
162
163     // Return the result.
164     return $actions;
165   }
166
167   /**
168    * Overrides Drupal\Core\Entity\EntityFormController::save().
169    *
170    * @param array $form
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.
174    *
175    * @return $this
176    */
177   public function save(array $form, FormStateInterface $form_state) {
178     $migration = $this->getEntity();
179     $status = $migration->save();
180
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())));
184     }
185     else {
186       // If we created a new entity...
187       drupal_set_message($this->t('Migration %label has been added.', array('%label' => $migration->label())));
188     }
189
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')));
193   }
194
195 }