annotate modules/contrib/migrate_tools/src/Form/MigrationFormBase.php @ 7:848c88cfe644

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