You are here

public function View::validateConfigurationForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/View.php \Drupal\webform\Plugin\WebformElement\View::validateConfigurationForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides WebformElementBase::validateConfigurationForm

File

src/Plugin/WebformElement/View.php, line 139

Class

View
Provides a hidden 'view' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  $properties = $this
    ->getConfigurationFormProperties($form, $form_state);

  // Check view name.

  /** @var \Drupal\views\ViewEntityInterface $view */
  $view = ViewEntity::load($properties['#name']);
  if (!$view) {
    $form_state
      ->setErrorByName('name', $this
      ->t('View %name does not exist.', [
      '%name' => $properties['#name'],
    ]));
    return;
  }

  // Check display id.
  $display = $view
    ->getDisplay($properties['#display_id']);
  if (!$display) {
    $form_state
      ->setErrorByName('display_id', $this
      ->t('View display %display_id does not exist.', [
      '%display_id' => $properties['#display_id'],
    ]));
    return;
  }

  // Check exposed filters is display on a form.
  $display_on = !empty($properties['#display_on']) ? $properties['#display_on'] : $this
    ->getDefaultProperty('display_on');
  if (in_array($display_on, [
    WebformElementDisplayOnInterface::DISPLAY_ON_BOTH,
    WebformElementDisplayOnInterface::DISPLAY_ON_FORM,
  ])) {
    if (isset($display['display_options']['filters'])) {
      $filters = $display['display_options']['filters'];
    }
    else {
      $default_display = $view
        ->getDisplay('default');
      $filters = $default_display['display_options']['filters'];
    }
    foreach ($filters as $filter) {
      if (!empty($filter['exposed'])) {
        $form_state
          ->setErrorByName('display_id', $this
          ->t('View display %display_id has exposed filters which will break the webform.', [
          '%display_id' => $properties['#display_id'],
        ]));
        break;
      }
    }
  }
}