You are here

public function Table::buildOptionsForm in Views Aggregator Plus 8

Render the given style.

Overrides Table::buildOptionsForm

File

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

Class

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

Namespace

Drupal\views_aggregator\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {

  // Note: bulk of form is provided by superclass Table.
  parent::buildOptionsForm($form, $form_state);
  $handlers = $this->displayHandler
    ->getHandlers('field');
  $columns = $this
    ->sanitizeColumns($this->options['columns']);
  foreach ($columns as $field => $column) {
    if ($field == $column) {

      // Make all columns potentially sortable, excluding "Global: Counter".
      // Views Field View is not click sortable by default
      // (module plugin definition). To enable it, set its
      // "public function clickSortable()" to return TRUE.
      $plugin_id = $handlers[$field]
        ->getPluginId();
      if ($plugin_id === 'counter') {
        $handlers[$field]->definition['click sortable'] = FALSE;
        $form['info'][$field]['sortable'] = NULL;
        $form['default'][$field] = NULL;
      }
      else {
        $handlers[$field]->definition['click sortable'] = TRUE;
      }
    }
  }

  // See function views_aggregator_theme().
  $form['#theme'] = 'views_aggregator_plugin_style_table';

  // Views style of grouping (splitting table into many) interferes,
  // so get rid of the form.
  unset($form['grouping']);
  $form['description_markup'] = [
    '#prefix' => '<div class="description form-item">',
    '#markup' => $this
      ->t('Column aggregation functions may be enabled independently of group aggregation functions. Every group aggregation function, except <em>Filter rows (by regexp)</em>, requires exactly <strong>one</strong> field to be assigned the <em>Group and compress</em> function. With that done, select any of the other aggregation functions for some or all of the fields. Functions marked with an asterisk take an optional parameter. For the aggregation functions <em>Enumerate, Range</em> and <em>Tally</em> the optional parameter is a delimiter to separate items. <br/>If you rewrite the results of a field, the aggregation functions will still use the original value. You can use a Global:Custom text field and Twig syntax instead to rewrite the results and apply aggregation functions on it. The Twig number_format() filter can be applied as last filter, its settings for decimal and thousand separator will be respected by the aggregation functions.<br/>You may combine multiple fields into the same render column. If you do, the separator specified will be used to separate the fields. You can control column order and field labels in the Fields section of the main configuration page.'),
    '#suffix' => '</div>',
  ];
  foreach ($columns as $field => $column) {
    $form['info'][$field]['has_aggr'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Apply group function'),
      '#default_value' => isset($this->options['info'][$field]['has_aggr']) ? $this->options['info'][$field]['has_aggr'] : FALSE,
    ];
    $group_options = [];
    $column_options = [];
    foreach (views_aggregator_get_aggregation_functions_info() as $function => $display_names) {
      if (!empty($display_names['group'])) {
        $group_options[$function] = $display_names['group'];
      }
      if (!empty($display_names['column'])) {
        $column_options[$function] = $display_names['column'];
      }
    }
    $form['info'][$field]['aggr'] = [
      '#type' => 'select',
      '#options' => $group_options,
      '#multiple' => TRUE,
      '#default_value' => empty($this->options['info'][$field]['aggr']) ? [
        'views_aggregator_first',
      ] : $this->options['info'][$field]['aggr'],
      '#states' => [
        'visible' => [
          'input[name="style_options[info][' . $field . '][has_aggr]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Optional parameter for the selected aggregation function.
    $parameter_label = $this
      ->t('Parameter');
    $form['info'][$field]['aggr_par'] = [
      '#type' => 'textfield',
      '#size' => 23,
      '#title' => $parameter_label,
      '#default_value' => isset($this->options['info'][$field]['aggr_par']) ? $this->options['info'][$field]['aggr_par'] : '',
      '#states' => [
        'visible' => [
          'input[name="style_options[info][' . $field . '][has_aggr]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['info'][$field]['has_aggr_column'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Apply column function'),
      '#default_value' => isset($this->options['info'][$field]['has_aggr_column']) ? $this->options['info'][$field]['has_aggr_column'] : FALSE,
    ];
    $form['info'][$field]['aggr_column'] = [
      '#type' => 'select',
      '#options' => $column_options,
      '#multiple' => FALSE,
      '#default_value' => empty($this->options['info'][$field]['aggr_column']) ? 'views_aggregator_sum' : $this->options['info'][$field]['aggr_column'],
      '#states' => [
        'visible' => [
          'input[name="style_options[info][' . $field . '][has_aggr_column]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Optional parameter for the selected column aggregation function.
    $form['info'][$field]['aggr_par_column'] = [
      '#type' => 'textfield',
      '#size' => 24,
      '#title' => $parameter_label,
      '#default_value' => isset($this->options['info'][$field]['aggr_par_column']) ? $this->options['info'][$field]['aggr_par_column'] : '',
      '#states' => [
        'visible' => [
          'input[name="style_options[info][' . $field . '][has_aggr_column]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  if (isset($this->options['group_aggregation']['group_aggregation_results'])) {
    $group_aggregation_results = $this->options['group_aggregation']['group_aggregation_results'];
  }
  else {
    $group_aggregation_results = 0;
  }
  $form['group_aggregation'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Group aggregation options'),
    '#weight' => -3,
  ];
  $form['group_aggregation']['group_aggregation_results'] = [
    '#title' => $this
      ->t('Aggregation results per group'),
    '#type' => 'radios',
    '#options' => [
      0 => $this
        ->t('results will be aggregated with one row per group'),
      1 => $this
        ->t('no aggregation, results will be shown after each group (like subtotals)'),
    ],
    '#description' => $this
      ->t('Select the second option, if you want to show subtotals'),
    '#default_value' => $group_aggregation_results,
    '#weight' => -2,
  ];
  $form['group_aggregation']['grouping_field_class'] = [
    '#title' => $this
      ->t('Grouping field cell class'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The CSS class to provide on each cell of the column/field that is being <em>Grouped and compressed</em>.'),
    '#default_value' => $this->options['group_aggregation']['grouping_field_class'],
  ];
  $form['group_aggregation']['result_label_prefix'] = [
    '#title' => $this
      ->t('Label prefix'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('<em>If no aggregation selected: </em>Attach this <em>before</em> the label in the column/field that is being <em>Grouped and compressed</em>.'),
    '#default_value' => $this->options['group_aggregation']['result_label_prefix'],
  ];
  $form['group_aggregation']['result_label_suffix'] = [
    '#title' => $this
      ->t('Label suffix'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('<em>If no aggregation selected: </em>Attach this <em>after</em> the label in the column/field that is being <em>Grouped and compressed</em>.'),
    '#default_value' => $this->options['group_aggregation']['result_label_suffix'],
  ];
  $form['group_aggregation']['grouping_row_class'] = [
    '#title' => $this
      ->t('Grouping row class'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('<em>If no aggregation selected: </em>The CSS class to provide on each subtotals result row.'),
    '#default_value' => $this->options['group_aggregation']['grouping_row_class'],
  ];
  $form['column_aggregation'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Column aggregation options'),
    '#weight' => -1,
  ];
  $form['column_aggregation']['totals_row_position'] = [
    '#title' => $this
      ->t('Column aggregation position'),
    '#description' => $this
      ->t('The <em>caption</em> option simply strings all column aggregations together with a space in between. So in order to add a comment or label you probably want to assign a <em>Label</em> column aggregation function to one of the first Views fields, before displaying any numeric aggregations.'),
    '#type' => 'checkboxes',
    '#options' => [
      1 => $this
        ->t('in the table header'),
      2 => $this
        ->t('in the table footer'),
      3 => $this
        ->t('in the table caption'),
    ],
    '#default_value' => $this->options['column_aggregation']['totals_row_position'],
  ];
  $form['column_aggregation']['totals_per_page'] = [
    '#title' => $this
      ->t('Column aggregation row applies to'),
    '#type' => 'radios',
    '#options' => [
      1 => $this
        ->t('the page shown, if a pager is enabled'),
      0 => $this
        ->t('the entire result set (CAUTION: To enable this, create a copy of this view display, disable the pager there, set the <i>machine name</i> to <i>DISPLAY_NAME_no_pager</i> (e.g. page_1_no_pager) and give it a different path.)'),
    ],
    '#description' => $this
      ->t('If your view does not have a pager, then the two options are equivalent.'),
    '#default_value' => $this->options['column_aggregation']['totals_per_page'],
    '#weight' => 1,
  ];
  $form['column_aggregation']['precision'] = [
    '#title' => $this
      ->t('Column aggregation row default numeric precision'),
    '#type' => 'textfield',
    '#size' => 3,
    '#description' => $this
      ->t('The number of decimals to use for column aggregations whose precisions are not defined elsewhere.'),
    '#default_value' => $this->options['column_aggregation']['precision'],
    '#weight' => 2,
  ];
  $form['column_aggregation']['totals_row_class'] = [
    '#title' => $this
      ->t('Column aggregation row class'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The CSS class to provide on the row containing the column aggregations.'),
    '#default_value' => $this->options['column_aggregation']['totals_row_class'],
    '#weight' => 3,
  ];
}