You are here

public function ContentImportSelectForm::submitForm in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 gathercontent_ui/src/Form/ContentImportSelectForm.php \Drupal\gathercontent_ui\Form\ContentImportSelectForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

gathercontent_ui/src/Form/ContentImportSelectForm.php, line 457

Class

ContentImportSelectForm
Class ContentImportSelectForm.

Namespace

Drupal\gathercontent_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] === 'edit-submit') {
    if ($this->step === 1) {
      $this->projectId = $form_state
        ->getValue('project');
      $selected_nodes = [];
      $selected_menus = [];
      $selected_statuses = [];
      foreach ($form_state
        ->getValue('items') as $item_id => $item) {
        if ($item['selected'] === "1") {
          $selected_nodes[] = $item_id;
          $selected_menus[$item_id] = $item['menu'];
          $selected_statuses[$item_id] = $item['drupal_status'];
        }
      }
      $this->nodes = $selected_nodes;
      $this->menu = $selected_menus;
      $this->drupalStatus = $selected_statuses;
      $this->step = 2;
      $form_state
        ->setRebuild(TRUE);
    }
    elseif ($this->step === 2) {
      $operation = Operation::create([
        'type' => 'import',
      ]);
      $operation
        ->save();
      $operations = [];
      $stack = [];
      $import_content = $this->nodes;
      foreach ($import_content as $k => $value) {
        if (isset($this->menu[$value]) && $this->menu[$value] != -1 || !isset($this->menu[$value])) {
          $parent_menu_item = isset($this->menu[$value]) ? $this->menu[$value] : NULL;
          $drupal_status = isset($this->drupalStatus[$value]) ? $this->drupalStatus[$value] : 0;
          $import_options = new ImportOptions($form_state
            ->getValue('node_update_method'), $drupal_status, $form_state
            ->getValue('node_create_new_revision'), $form_state
            ->getValue('status'), $parent_menu_item, $operation
            ->uuid());
          $operations[] = [
            'gathercontent_import_process',
            [
              $value,
              $import_options,
            ],
          ];
          $stack[$value] = $value;
          unset($import_content[$k]);
        }
      }
      if (!empty($import_content)) {

        // Load all by project_id.

        /** @var \Cheppers\GatherContent\DataTypes\Item[] $content */
        $content = $this->client
          ->itemsGet($this->projectId);
        while (!empty($import_content)) {
          $current = reset($import_content);
          if (isset($stack[$content[$current]->parentId])) {
            $parent_menu_item = 'node:' . $content[$current]->parentId;
            $drupal_status = isset($this->drupalStatus[$current]) ? $this->drupalStatus[$current] : 0;
            $import_options = new ImportOptions($form_state
              ->getValue('node_update_method'), $drupal_status, $form_state
              ->getValue('node_create_new_revision'), $form_state
              ->getValue('status'), $parent_menu_item, $operation
              ->uuid());
            $operations[] = [
              'gathercontent_import_process',
              [
                $current,
                $import_options,
              ],
            ];
            $stack[$current] = $current;
            array_shift($import_content);
          }
          else {
            array_shift($import_content);
            array_push($import_content, $current);
          }
        }
      }
      $batch = [
        'title' => t('Importing content ...'),
        'operations' => $operations,
        'finished' => 'gathercontent_import_finished',
        'file' => drupal_get_path('module', 'gathercontent') . '/gathercontent.module',
        'init_message' => t('Import is starting ...'),
        'progress_message' => t('Processed @current out of @total.'),
        'error_message' => t('An error occurred during processing'),
      ];
      batch_set($batch);
    }
  }
  elseif ($form_state
    ->getTriggeringElement()['#id'] === 'edit-back') {
    if ($this->step === 1) {
      return $form_state
        ->setRedirect('gathercontent_ui.import_select_form');
    }
    $this->step = 1;
    $form_state
      ->setRebuild(TRUE);
  }
  return TRUE;
}