You are here

public function Table::validateOptionsForm in Views Aggregator Plus 8

Validate the options form.

Overrides StylePluginBase::validateOptionsForm

File

src/Plugin/views/style/Table.php, line 267

Class

Table
Style plugin to render each item as a row in a table.

Namespace

Drupal\views_aggregator\Plugin\views\style

Code

public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  parent::validateOptionsForm($form, $form_state);
  $allowed_tags = [
    'b',
    'br',
    'em',
    'i',
    'p',
    'strong',
    'u',
  ];
  $tag_msg = $this
    ->t('<strong>Parameter</strong> field contains an illegal character or illegal HTML tag. Allowed tags are: %tags', [
    '%tags' => implode(', ', $allowed_tags),
  ]);

  // Count the number of occurrences of the grouping and other aggregation
  // functions.
  $num_grouped = 0;
  $num_aggregation_functions = 0;
  foreach ($form_state
    ->getValue([
    'style_options',
    'info',
  ]) as $field_name => $options) {
    if (!empty($options['has_aggr'])) {
      if (in_array('views_aggregator_group_and_compress', $options['aggr'])) {
        $num_grouped++;
      }
      elseif (!in_array('views_aggregator_row_filter', $options['aggr'])) {
        $num_aggregation_functions += count($options['aggr']);
      }
    }
    $filtered = Xss::filter($options['aggr_par'], $allowed_tags);
    if ($options['aggr_par'] != $filtered) {
      $form_state
        ->setError($form['info'][$field_name]['aggr_par'], $tag_msg);
    }
    $filtered = Xss::filter($options['aggr_par_column'], $allowed_tags);
    if ($options['aggr_par_column'] != $filtered) {
      $form_state
        ->setError($form['info'][$field_name]['aggr_par_column'], $tag_msg);
    }
  }

  // When we have no aggregation functions, we must have 0 or 1 grouping
  // function. When we have aggregation functions, there must be 1 grouping.
  $ok = $num_aggregation_functions == 0 ? $num_grouped <= 1 : $num_grouped == 1;
  if (!$ok) {
    $msg = $this
      ->t('When applying group aggregation functions, you must also select <em>"Group and compress"</em> on exactly one field.');
    foreach ($form_state
      ->getValue([
      'style_options',
      'info',
    ]) as $field_name => $options) {
      $form_state
        ->setError($form['info'][$field_name]['aggr'], $msg);
      $msg = '';
    }
  }
}