You are here

public function ContentImportSelectForm::validateForm in GatherContent 8.3

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

src/Form/ContentImportSelectForm.php, line 364

Class

ContentImportSelectForm
Class ContentImportSelectForm.

Namespace

Drupal\gathercontent\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.
        $content_obj = new Content();
        $contents_source = $content_obj
          ->getContents($form_state
          ->getValue('project'));
        $content = [];
        foreach ($contents_source as $value) {
          $content[$value->id] = $value;
        }
        $num_of_repeats = 0;
        $size = count($import_content);
        while (!empty($import_content)) {
          $current = reset($import_content);
          if (isset($stack[$content[$current]->parent_id])) {
            $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 = [];
            }
          }
        }
      }
    }
  }
}