You are here

public function AssetInjectorFormBase::validateForm in Asset Injector 8.2

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

1 call to AssetInjectorFormBase::validateForm()
AssetInjectorJsForm::validateForm in src/Form/AssetInjectorJsForm.php
Form validation handler.
1 method overrides AssetInjectorFormBase::validateForm()
AssetInjectorJsForm::validateForm in src/Form/AssetInjectorJsForm.php
Form validation handler.

File

src/Form/AssetInjectorFormBase.php, line 276

Class

AssetInjectorFormBase
Class AssetInjectorCsssForm.

Namespace

Drupal\asset_injector\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $conditions = $form_state
    ->getValue('conditions');

  // Validate conditions condition settings.
  foreach ($conditions as $condition_id => &$values) {

    // Since core theme condition doesn't support multiple theme choices, if
    // no themes are selected, we change it to an empty string so that the
    // plugin validation and submit will function as expected.
    if ($condition_id == 'current_theme' && empty($values['theme'])) {
      $values['theme'] = '';
      $form_state
        ->setValue('conditions', $conditions);
    }

    // All condition plugins use 'negate' as a Boolean in their schema.
    // However, certain form elements may return it as 0/1. Cast here to
    // ensure the data is in the expected type.
    if (array_key_exists('negate', $values)) {
      $form_state
        ->setValue([
        'conditions',
        $condition_id,
        'negate',
      ], (bool) $values['negate']);
    }

    // Allow the condition to validate the form.
    $condition = $form_state
      ->get([
      'conditions',
      $condition_id,
    ]);

    // Fix some issues with webform & context with their entity_bundle
    // conditions. Even with the array empty, the condition still saves
    // with values. This produces logic issues when resolving the conditions.
    // @see https://www.drupal.org/node/2857279
    $values = $form_state
      ->getValue([
      'conditions',
      $condition_id,
    ]);
    foreach ($values as &$value) {
      if (is_array($value)) {
        $value = array_filter($value);
      }
    }
    $form_state
      ->setValue([
      'conditions',
      $condition_id,
    ], $values);
    $condition
      ->validateConfigurationForm($form['conditions'][$condition_id], SubformState::createForSubform($form['conditions'][$condition_id], $form, $form_state));
  }
}