Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Form / MigrationGroupFormBase.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 Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Class MigrationGroupFormBase.
12  *
13  * @package Drupal\migrate_tools\Form
14  *
15  * @ingroup migrate_tools
16  */
17 class MigrationGroupFormBase extends EntityForm {
18
19   /**
20    * The query factory service.
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
30    * group form base, however, requires an entity query factory to be injected
31    * into it from the container. We later use this query factory to build an
32    * entity 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    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static($container->get('entity.query'));
46   }
47
48   /**
49    * Overrides Drupal\Core\Entity\EntityFormController::form().
50    *
51    * Builds the entity add/edit form.
52    *
53    * @param array $form
54    *   An associative array containing the structure of the form.
55    * @param \Drupal\Core\Form\FormStateInterface $form_state
56    *   An associative array containing the current state of the form.
57    *
58    * @return array
59    *   An associative array containing the migration group add/edit form.
60    */
61   public function buildForm(array $form, FormStateInterface $form_state) {
62     // Get anything we need from the base class.
63     $form = parent::buildForm($form, $form_state);
64
65     /** @var \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group */
66     $migration_group = $this->entity;
67
68     // Build the form.
69     $form['label'] = [
70       '#type' => 'textfield',
71       '#title' => $this->t('Label'),
72       '#maxlength' => 255,
73       '#default_value' => $migration_group->label(),
74       '#required' => TRUE,
75     ];
76     $form['id'] = [
77       '#type' => 'machine_name',
78       '#title' => $this->t('Machine name'),
79       '#default_value' => $migration_group->id(),
80       '#machine_name' => [
81         'exists' => [$this, 'exists'],
82         'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
83         '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".',
84       ],
85       '#disabled' => !$migration_group->isNew(),
86     ];
87     $form['description'] = [
88       '#type' => 'textfield',
89       '#title' => $this->t('Description'),
90       '#maxlength' => 255,
91       '#default_value' => $migration_group->get('description'),
92     ];
93     $form['source_type'] = [
94       '#type' => 'textfield',
95       '#title' => $this->t('Source type'),
96       '#description' => $this->t('Type of source system the group is migrating from, for example "Drupal 6" or "WordPress 4".'),
97       '#maxlength' => 255,
98       '#default_value' => $migration_group->get('source_type'),
99     ];
100
101     // Return the form.
102     return $form;
103   }
104
105   /**
106    * Checks for an existing migration group.
107    *
108    * @param string|int $entity_id
109    *   The entity ID.
110    * @param array $element
111    *   The form element.
112    * @param \Drupal\Core\Form\FormStateInterface $form_state
113    *   The form state.
114    *
115    * @return bool
116    *   TRUE if this format already exists, FALSE otherwise.
117    */
118   public function exists($entity_id, array $element, FormStateInterface $form_state) {
119     // Use the query factory to build a new migration group entity query.
120     $query = $this->entityQueryFactory->get('migration_group');
121
122     // Query the entity ID to see if its in use.
123     $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
124       ->execute();
125
126     // We don't need to return the ID, only if it exists or not.
127     return (bool) $result;
128   }
129
130   /**
131    * Overrides Drupal\Core\Entity\EntityFormController::actions().
132    *
133    * @param array $form
134    *   An associative array containing the structure of the form.
135    * @param \Drupal\Core\Form\FormStateInterface $form_state
136    *   An associative array containing the current state of the form.
137    *
138    * @return array
139    *   An array of supported actions for the current entity form.
140    */
141   protected function actions(array $form, FormStateInterface $form_state) {
142     // Get the basic actins from the base class.
143     $actions = parent::actions($form, $form_state);
144
145     // Change the submit button text.
146     $actions['submit']['#value'] = $this->t('Save');
147
148     // Return the result.
149     return $actions;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function save(array $form, FormStateInterface $form_state) {
156     $migration_group = $this->getEntity();
157     $status = $migration_group->save();
158
159     if ($status == SAVED_UPDATED) {
160       // If we edited an existing entity...
161       drupal_set_message($this->t('Migration group %label has been updated.', ['%label' => $migration_group->label()]));
162     }
163     else {
164       // If we created a new entity...
165       drupal_set_message($this->t('Migration group %label has been added.', ['%label' => $migration_group->label()]));
166     }
167
168     // Redirect the user back to the listing route after the save operation.
169     $form_state->setRedirect('entity.migration_group.list');
170   }
171
172 }