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