Chris@0: entityQueryFactory = $query_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Factory method for MigrationGroupFormBase. 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\MigrationFormBase Chris@0: * 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 group 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 MigrationGroupInterface $migration_group */ Chris@0: $migration_group = $this->entity; 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_group->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_group->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_group->isNew(), Chris@0: ); Chris@0: $form['description'] = array( Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Description'), Chris@0: '#maxlength' => 255, Chris@0: '#default_value' => $migration_group->get('description'), Chris@0: ); Chris@0: $form['source_type'] = array( Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Source type'), Chris@0: '#description' => $this->t('Type of source system the group is migrating from, for example "Drupal 6" or "WordPress 4".'), Chris@0: '#maxlength' => 255, Chris@0: '#default_value' => $migration_group->get('source_type'), Chris@0: ); Chris@0: Chris@0: // Return the form. 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 group entity query. Chris@0: $query = $this->entityQueryFactory->get('migration_group'); 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_group = $this->getEntity(); Chris@0: $status = $migration_group->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 group %label has been updated.', array('%label' => $migration_group->label()))); Chris@0: } Chris@0: else { Chris@0: // If we created a new entity... Chris@0: drupal_set_message($this->t('Migration group %label has been added.', array('%label' => $migration_group->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_group.list'); Chris@0: } Chris@0: Chris@0: }