Chris@0: entityQueryFactory = $query_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Factory method for MigrationFormBase. Chris@0: * Chris@0: * @param \Symfony\Component\DependencyInjection\ContainerInterface $container Chris@0: * A container interface service. Chris@0: * Chris@0: * @return \Drupal\migrate_tools\Form\MigrationGroupFormBase Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static($container->get('entity.query')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides Drupal\Core\Entity\EntityFormController::form(). Chris@0: * Chris@0: * Builds the entity add/edit form. Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * An associative array containing the current state of the form. Chris@0: * Chris@0: * @return array Chris@0: * An associative array containing the migration add/edit form. Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state) { Chris@0: // Get anything we need from the base class. Chris@0: $form = parent::buildForm($form, $form_state); Chris@0: Chris@0: /** @var MigrationInterface $migration */ Chris@0: $migration = $this->entity; Chris@0: Chris@0: $form['warning'] = [ Chris@0: '#markup' => $this->t('Creating migrations is not yet supported. See :url', [ Chris@0: ':url' => 'https://www.drupal.org/node/2573241', Chris@0: ]) Chris@0: ]; Chris@0: Chris@0: // Build the form. Chris@0: $form['label'] = array( Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Label'), Chris@0: '#maxlength' => 255, Chris@0: '#default_value' => $migration->label(), Chris@0: '#required' => TRUE, Chris@0: ); Chris@0: $form['id'] = array( Chris@0: '#type' => 'machine_name', Chris@0: '#title' => $this->t('Machine name'), Chris@0: '#default_value' => $migration->id(), Chris@0: '#machine_name' => array( Chris@0: 'exists' => array($this, 'exists'), Chris@0: 'replace_pattern' => '([^a-z0-9_]+)|(^custom$)', Chris@0: '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: ), Chris@0: '#disabled' => !$migration->isNew(), Chris@0: ); Chris@0: Chris@0: $groups = MigrationGroup::loadMultiple(); Chris@0: $group_options = []; Chris@0: foreach ($groups as $group) { Chris@0: $group_options[$group->id()] = $group->label(); Chris@0: } Chris@0: if (!$migration->get('migration_group') && isset($group_options['default'])) { Chris@0: $migration->set('migration_group', 'default'); Chris@0: } Chris@0: Chris@0: $form['migration_group'] = array( Chris@0: '#type' => 'select', Chris@0: '#title' => $this->t('Migration Group'), Chris@0: '#empty_value' => '', Chris@0: '#default_value' => $migration->get('migration_group'), Chris@0: '#options' => $group_options, Chris@0: '#description' => $this->t('Assign this migration to an existing group.'), Chris@0: ); Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks for an existing migration group. Chris@0: * Chris@0: * @param string|int $entity_id Chris@0: * The entity ID. Chris@0: * @param array $element Chris@0: * The form element. Chris@0: * @param FormStateInterface $form_state Chris@0: * The form state. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if this format already exists, FALSE otherwise. Chris@0: */ Chris@0: public function exists($entity_id, array $element, FormStateInterface $form_state) { Chris@0: // Use the query factory to build a new migration entity query. Chris@0: $query = $this->entityQueryFactory->get('migration'); Chris@0: Chris@0: // Query the entity ID to see if its in use. Chris@0: $result = $query->condition('id', $element['#field_prefix'] . $entity_id) Chris@0: ->execute(); Chris@0: Chris@0: // We don't need to return the ID, only if it exists or not. Chris@0: return (bool) $result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides Drupal\Core\Entity\EntityFormController::actions(). Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * An associative array containing the current state of the form. Chris@0: * Chris@0: * @return array Chris@0: * An array of supported actions for the current entity form. Chris@0: */ Chris@0: protected function actions(array $form, FormStateInterface $form_state) { Chris@0: // Get the basic actins from the base class. Chris@0: $actions = parent::actions($form, $form_state); Chris@0: Chris@0: // Change the submit button text. Chris@0: $actions['submit']['#value'] = $this->t('Save'); Chris@0: Chris@0: // Return the result. Chris@0: return $actions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides Drupal\Core\Entity\EntityFormController::save(). Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * An associative array containing the current state of the form. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function save(array $form, FormStateInterface $form_state) { Chris@0: $migration = $this->getEntity(); Chris@0: $status = $migration->save(); Chris@0: Chris@0: if ($status == SAVED_UPDATED) { Chris@0: // If we edited an existing entity... Chris@0: drupal_set_message($this->t('Migration %label has been updated.', array('%label' => $migration->label()))); Chris@0: } Chris@0: else { Chris@0: // If we created a new entity... Chris@0: drupal_set_message($this->t('Migration %label has been added.', array('%label' => $migration->label()))); Chris@0: } Chris@0: Chris@0: // Redirect the user back to the listing route after the save operation. Chris@0: $form_state->setRedirect('entity.migration.list', Chris@0: array('migration_group' => $migration->get('migration_group'))); Chris@0: } Chris@0: Chris@0: }