You are here

class MigrationExecuteForm in Migrate Tools 8.4

Same name and namespace in other branches
  1. 8.5 src/Form/MigrationExecuteForm.php \Drupal\migrate_tools\Form\MigrationExecuteForm

This form is specifically for configuring process pipelines.

Hierarchy

Expanded class hierarchy of MigrationExecuteForm

1 string reference to 'MigrationExecuteForm'
migrate_tools.routing.yml in ./migrate_tools.routing.yml
migrate_tools.routing.yml

File

src/Form/MigrationExecuteForm.php, line 17

Namespace

Drupal\migrate_tools\Form
View source
class MigrationExecuteForm extends FormBase {

  /**
   * Plugin manager for migration plugins.
   *
   * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
   */
  protected $migrationPluginManager;

  /**
   * Constructs a new MigrationExecuteForm object.
   *
   * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
   *   The plugin manager for config entity-based migrations.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(MigrationPluginManagerInterface $migration_plugin_manager, RouteMatchInterface $route_match) {
    $this->migrationPluginManager = $migration_plugin_manager;
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.migration'), $container
      ->get('current_route_match'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'migration_execute_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = $form ?: [];

    /** @var \Drupal\migrate_plus\Entity\MigrationInterface $migration */
    $migration = $this
      ->getRouteMatch()
      ->getParameter('migration');
    $form['#title'] = $this
      ->t('Execute migration %label', [
      '%label' => $migration
        ->label(),
    ]);
    $form = $this
      ->buildFormOperations($form, $form_state);
    $form = $this
      ->buildFormOptions($form, $form_state);
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Execute'),
    ];
    return $form;
  }

  /**
   * Build the operation form field.
   *
   * @param array $form
   *   The execution form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The execution form updated with the operations.
   */
  protected function buildFormOperations(array $form, FormStateInterface $form_state) {

    // Build the migration execution form.
    $options = [
      'import' => $this
        ->t('Import'),
      'rollback' => $this
        ->t('Rollback'),
      'stop' => $this
        ->t('Stop'),
      'reset' => $this
        ->t('Reset'),
    ];
    $form['operation'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Operation'),
      '#description' => $this
        ->t('Choose an operation to run.'),
      '#options' => $options,
      '#default_value' => 'import',
      '#required' => TRUE,
      'import' => [
        '#description' => $this
          ->t('Imports all previously unprocessed records from the source, plus any records marked for update, into destination Drupal objects.'),
      ],
      'rollback' => [
        '#description' => $this
          ->t('Deletes all Drupal objects created by the import.'),
      ],
      'stop' => [
        '#description' => $this
          ->t('Cleanly interrupts any import or rollback processes that may currently be running.'),
      ],
      'reset' => [
        '#description' => $this
          ->t('Sometimes a process may fail to stop cleanly, and be left stuck in an Importing or Rolling Back status. Choose Reset to clear the status and permit other operations to proceed.'),
      ],
    ];
    return $form;
  }

  /**
   * Build the execution options form field.
   *
   * @param array $form
   *   The execution form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The execution form updated with the execution options.
   */
  protected function buildFormOptions(array $form, FormStateInterface $form_state) {
    $form['options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Additional execution options'),
      '#open' => FALSE,
    ];
    $form['options']['update'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Update'),
      '#description' => $this
        ->t('Check this box to update all previously-imported content in addition to importing new content. Leave unchecked to only import new content'),
    ];
    $form['options']['force'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Ignore dependencies'),
      '#description' => $this
        ->t('Check this box to ignore dependencies when running imports - all tasks will run whether or not their dependent tasks have completed.'),
    ];
    $form['options']['limit'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Limit to:'),
      '#size' => 10,
      '#description' => $this
        ->t('Set a limit of how many items to process for each migration task.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $operation = $form_state
      ->getValue('operation');
    if ($form_state
      ->getValue('limit')) {
      $limit = $form_state
        ->getValue('limit');
    }
    else {
      $limit = 0;
    }
    if ($form_state
      ->getValue('update')) {
      $update = $form_state
        ->getValue('update');
    }
    else {
      $update = 0;
    }
    if ($form_state
      ->getValue('force')) {
      $force = $form_state
        ->getValue('force');
    }
    else {
      $force = 0;
    }
    $migration = $this
      ->getRouteMatch()
      ->getParameter('migration');
    if ($migration) {

      /** @var \Drupal\migrate\Plugin\MigrationInterface $migration_plugin */
      $migration_plugin = $this->migrationPluginManager
        ->createInstance($migration
        ->id(), $migration
        ->toArray());
      $migrateMessage = new MigrateMessage();
      switch ($operation) {
        case 'import':
          $options = [
            'limit' => $limit,
            'update' => $update,
            'force' => $force,
          ];
          $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
          $executable
            ->batchImport();
          break;
        case 'rollback':
          $options = [
            'limit' => $limit,
            'update' => $update,
            'force' => $force,
          ];
          $executable = new MigrateBatchExecutable($migration_plugin, $migrateMessage, $options);
          $executable
            ->rollback();
          break;
        case 'stop':
          $migration_plugin
            ->interruptMigration(MigrationInterface::RESULT_STOPPED);
          break;
        case 'reset':
          $migration_plugin
            ->setStatus(MigrationInterface::STATUS_IDLE);
          break;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
MigrationExecuteForm::$migrationPluginManager protected property Plugin manager for migration plugins.
MigrationExecuteForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
MigrationExecuteForm::buildFormOperations protected function Build the operation form field.
MigrationExecuteForm::buildFormOptions protected function Build the execution options form field.
MigrationExecuteForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
MigrationExecuteForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MigrationExecuteForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
MigrationExecuteForm::__construct public function Constructs a new MigrationExecuteForm object.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.