comparison modules/contrib/migrate_tools/src/Form/MigrationGroupFormBase.php @ 0:4c8ae668cc8c

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