You are here

public function FacetForm::validateForm in Facets 8

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/FacetForm.php, line 615

Class

FacetForm
Provides a form for configuring the processors of a facet.

Namespace

Drupal\facets\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  /** @var \Drupal\facets\FacetInterface $facet */
  $facet = $this->entity;
  $values = $form_state
    ->getValues();

  /** @var \Drupal\facets\Processor\ProcessorInterface[] $processors */
  $processors = $facet
    ->getProcessors(FALSE);

  // Iterate over all processors that have a form and are enabled.
  foreach ($form['facet_settings'] as $processor_id => $processor_form) {
    if (!empty($values['processors'][$processor_id])) {
      $processor_form_state = SubformState::createForSubform($form['facet_settings'][$processor_id]['settings'], $form, $form_state);
      $processors[$processor_id]
        ->validateConfigurationForm($form['facet_settings'][$processor_id], $processor_form_state, $facet);
    }
  }

  // Iterate over all sorting processors that have a form and are enabled.
  foreach ($form['facet_sorting'] as $processor_id => $processor_form) {
    if (!empty($values['processors'][$processor_id])) {
      $processor_form_state = SubformState::createForSubform($form['facet_sorting'][$processor_id]['settings'], $form, $form_state);
      $processors[$processor_id]
        ->validateConfigurationForm($form['facet_sorting'][$processor_id], $processor_form_state, $facet);
    }
  }

  // Only widgets that return an array can work with rest facet sources, so if
  // the user has selected another widget, we should point them to their
  // misconfiguration.
  if ($facet_source = $facet
    ->getFacetSource()) {
    if ($facet_source instanceof SearchApiFacetSourceInterface) {
      if ($facet_source
        ->getDisplay() instanceof ViewsRest) {
        if (strpos($values['widget'], 'array') === FALSE) {
          $form_state
            ->setErrorByName('widget', $this
            ->t('The Facet source is a Rest export. Please select a raw widget.'));
        }
      }
    }
  }

  // Validate url alias.
  $url_alias = $form_state
    ->getValue([
    'facet_settings',
    'url_alias',
  ]);
  if ($url_alias == 'page') {
    $form_state
      ->setErrorByName('url_alias', $this
      ->t('This URL alias is not allowed.'));
  }
  elseif (preg_match('/[^a-zA-Z0-9_~\\.\\-]/', $url_alias)) {
    $form_state
      ->setErrorByName('url_alias', $this
      ->t('The URL alias contains characters that are not allowed.'));
  }
}