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;
11 * Class MigrationGroupFormBase.
13 * @package Drupal\migrate_tools\Form
15 * @ingroup migrate_tools
17 class MigrationGroupFormBase extends EntityForm {
20 * The query factory service.
22 * @var \Drupal\Core\Entity\Query\QueryFactory
24 protected $entityQueryFactory;
27 * Construct the MigrationGroupFormBase.
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.
34 * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
35 * An entity query factory for the migration group entity type.
37 public function __construct(QueryFactory $query_factory) {
38 $this->entityQueryFactory = $query_factory;
44 public static function create(ContainerInterface $container) {
45 return new static($container->get('entity.query'));
49 * Overrides Drupal\Core\Entity\EntityFormController::form().
51 * Builds the entity add/edit 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.
59 * An associative array containing the migration group add/edit form.
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);
65 /** @var \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group */
66 $migration_group = $this->entity;
70 '#type' => 'textfield',
71 '#title' => $this->t('Label'),
73 '#default_value' => $migration_group->label(),
77 '#type' => 'machine_name',
78 '#title' => $this->t('Machine name'),
79 '#default_value' => $migration_group->id(),
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".',
85 '#disabled' => !$migration_group->isNew(),
87 $form['description'] = [
88 '#type' => 'textfield',
89 '#title' => $this->t('Description'),
91 '#default_value' => $migration_group->get('description'),
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".'),
98 '#default_value' => $migration_group->get('source_type'),
106 * Checks for an existing migration group.
108 * @param string|int $entity_id
110 * @param array $element
112 * @param \Drupal\Core\Form\FormStateInterface $form_state
116 * TRUE if this format already exists, FALSE otherwise.
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');
122 // Query the entity ID to see if its in use.
123 $result = $query->condition('id', $element['#field_prefix'] . $entity_id)
126 // We don't need to return the ID, only if it exists or not.
127 return (bool) $result;
131 * Overrides Drupal\Core\Entity\EntityFormController::actions().
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.
139 * An array of supported actions for the current entity form.
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);
145 // Change the submit button text.
146 $actions['submit']['#value'] = $this->t('Save');
148 // Return the result.
155 public function save(array $form, FormStateInterface $form_state) {
156 $migration_group = $this->getEntity();
157 $status = $migration_group->save();
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()]));
164 // If we created a new entity...
165 drupal_set_message($this->t('Migration group %label has been added.', ['%label' => $migration_group->label()]));
168 // Redirect the user back to the listing route after the save operation.
169 $form_state->setRedirect('entity.migration_group.list');