You are here

public function AdvancedRouting::validateOptionsForm in Views Advanced Routing 8

Same name and namespace in other branches
  1. 2.x src/Plugin/views/display_extender/AdvancedRouting.php \Drupal\views_advanced_routing\Plugin\views\display_extender\AdvancedRouting::validateOptionsForm()

@inheritDoc

Overrides DisplayExtenderPluginBase::validateOptionsForm

File

src/Plugin/views/display_extender/AdvancedRouting.php, line 119
Contains \Drupal\views_test_data\Plugin\views\display_extender\DisplayExtenderTest.

Class

AdvancedRouting
Advanced route editor.

Namespace

Drupal\views_advanced_routing\Plugin\views\display_extender

Code

public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  $section = $form_state
    ->get('section');
  if ($section == 'views_advanced_routing_route') {
    $route_info = [
      'defaults' => [],
      'requirements' => [],
      'options' => [],
    ];

    // $key: defaults, requirements, options.
    $route = $form_state
      ->getValue('route');
    foreach ($route as $key => $value) {
      try {
        $route_data = Yaml::decode($route[$key]) ?: [];
        if (is_array($route_data)) {
          $route_info[$key] = $route_data;
        }
        else {
          $form_state
            ->setError($form['route'][$key], $this
            ->t('Value must be an array.'));
        }
      } catch (InvalidDataTypeException $e) {
        $form_state
          ->setError($form['route'][$key], $this
          ->t('YAML does not validate: @exception', [
          '@exception' => $e
            ->getMessage(),
        ]));
      }
    }
    try {
      new Route('<none>', $route_info['defaults'], $route_info['requirements'], $route_info['options']);
    } catch (\Exception $e) {

      // Creating the route can throw exceptions.
      $form_state
        ->setError($form['route'], $e
        ->getMessage());
    }
    $form_state
      ->set('route_info', $route_info);
  }
}