3 namespace Drupal\migrate_tools\Form;
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;
12 * Class MigrationGroupFormBase.
14 * @package Drupal\migrate_tools\Form
16 * @ingroup migrate_tools
18 class MigrationGroupFormBase extends EntityForm {
21 * @var \Drupal\Core\Entity\Query\QueryFactory
23 protected $entityQueryFactory;
26 * Construct the MigrationGroupFormBase.
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.
33 * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
34 * An entity query factory for the migration group entity type.
36 public function __construct(QueryFactory $query_factory) {
37 $this->entityQueryFactory = $query_factory;
41 * Factory method for MigrationGroupFormBase.
43 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
44 * A container interface service.
46 * @return \Drupal\migrate_tools\Form\MigrationFormBase
49 public static function create(ContainerInterface $container) {
50 return new static($container->get('entity.query'));
54 * Overrides Drupal\Core\Entity\EntityFormController::form().
56 * Builds the entity add/edit 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.
64 * An associative array containing the migration group add/edit form.
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);
70 /** @var MigrationGroupInterface $migration_group */
71 $migration_group = $this->entity;
74 $form['label'] = array(
75 '#type' => 'textfield',
76 '#title' => $this->t('Label'),
78 '#default_value' => $migration_group->label(),
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".',
90 '#disabled' => !$migration_group->isNew(),
92 $form['description'] = array(
93 '#type' => 'textfield',
94 '#title' => $this->t('Description'),
96 '#default_value' => $migration_group->get('description'),
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".'),
103 '#default_value' => $migration_group->get('source_type'),
111 * Checks for an existing migration group.
113 * @param string|int $entity_id
115 * @param array $element
117 * @param FormStateInterface $form_state
121 * TRUE if this format already exists, FALSE otherwise.
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');
127 // Query the entity ID to see if its in use.
128 $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
131 // We don't need to return the ID, only if it exists or not.
132 return (bool) $result;
136 * Overrides Drupal\Core\Entity\EntityFormController::actions().
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.
144 * An array of supported actions for the current entity form.
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);
150 // Change the submit button text.
151 $actions['submit']['#value'] = $this->t('Save');
153 // Return the result.
158 * Overrides Drupal\Core\Entity\EntityFormController::save().
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.
167 public function save(array $form, FormStateInterface $form_state) {
168 $migration_group = $this->getEntity();
169 $status = $migration_group->save();
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())));
176 // If we created a new entity...
177 drupal_set_message($this->t('Migration group %label has been added.', array('%label' => $migration_group->label())));
180 // Redirect the user back to the listing route after the save operation.
181 $form_state->setRedirect('entity.migration_group.list');