You are here

public function ContentImportSelectForm::validateForm in GatherContent 8.4

Form validation 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 FormBase::validateForm

File

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

Class

ContentImportSelectForm
Class ContentImportSelectForm.

Namespace

Drupal\gathercontent_ui\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] === 'edit-submit') {
    if ($this->step === 1) {
      $stack = [];
      $import_content = [];
      $selected_menus = [];
      foreach ($form_state
        ->getValue('items') as $item_id => $item) {
        if ($item['selected'] === "1") {
          $import_content[] = $item_id;
          $selected_menus[$item_id] = $item['menu'];
        }
      }
      foreach ($import_content as $k => $value) {
        if (isset($selected_menus[$value]) && $selected_menus[$value] != -1 || !isset($selected_menus[$value])) {
          $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($form_state
          ->getValue('project'));
        $num_of_repeats = 0;
        $size = count($import_content);
        while (!empty($import_content)) {
          $current = reset($import_content);
          if (isset($stack[$content[$current]->parentId])) {
            $stack[$current] = $current;
            array_shift($import_content);
          }
          else {
            array_shift($import_content);
            array_push($import_content, $current);
            $num_of_repeats++;
            if ($num_of_repeats >= $size * $size) {
              $form_state
                ->setErrorByName('form', t("Please check your menu selection, some of items don't have parent in import."));
              $import_content = [];
            }
          }
        }
      }
    }
  }
}