You are here

protected function Views_Merge_RowsDisplayExtenderPlugin::views_merge_rows_options_form_build in Views Merge Rows 8

Provide a form to edit options for this plugin.

1 call to Views_Merge_RowsDisplayExtenderPlugin::views_merge_rows_options_form_build()
Views_Merge_RowsDisplayExtenderPlugin::buildOptionsForm in src/Plugin/views/display_extender/views_merge_rowsDisplayExtenderPlugin.php
Provide the form to set the rows merge options.

File

src/Plugin/views/display_extender/views_merge_rowsDisplayExtenderPlugin.php, line 113
Contains the class to extend views display with rows merge functionality.

Class

Views_Merge_RowsDisplayExtenderPlugin
Plugin annotation @ViewsDisplayExtender( id = "views_merge_rows", title = @Translation("Merge rows"), help = @Translation("Merges rows with the same values in the specified fields."), no_ui = FALSE )

Namespace

Drupal\views_merge_rows\Plugin\views\display_extender

Code

protected function views_merge_rows_options_form_build(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->get('section') == 'views_merge_rows') {
    $options = $this
      ->get_options();
    if ($this->displayHandler
      ->usesPager()) {
      $form['warning_markup'] = [
        '#markup' => '<div class="warning messages">' . t('It is highly recommended to disable pager if you merge rows.') . '</div>',
      ];
    }
    else {
      $form['warning_markup'] = [];
    }
    $form['#tree'] = TRUE;
    $form['#theme'] = 'merge_rows_theme';
    $form['#title'] .= t('Merge rows with the same content.');
    $form['merge_rows'] = [
      '#type' => 'checkbox',
      '#title' => t('Merge rows with the same content in the specified fields'),
      '#default_value' => $options['merge_rows'],
    ];
    $form['use_grouping'] = [
      '#type' => 'checkbox',
      '#title' => t('Merge rows using the grouping defined in the base view'),
      '#default_value' => $options['use_grouping'],
    ];

    // Create an array of allowed columns from the data we know:
    $field_names = $this->displayHandler
      ->getFieldLabels();
    foreach ($field_names as $field => $name) {
      $safe = str_replace([
        '][',
        '_',
        ' ',
      ], '-', $field);

      // Markup for the field name.
      $form['field_config'][$field]['name'] = [
        '#markup' => $name,
      ];

      // Select for merge options.
      $form['field_config'][$field]['merge_option'] = [
        '#type' => 'select',
        '#options' => [
          'filter' => t('Use values of this field as a filter'),
          'merge' => t('Merge values of this field'),
          'merge_unique' => t('Merge unique values of this field'),
          'first_value' => t('Use the first value of this field'),
          'highest_value' => t('Use the highest value of this field'),
          'lowest_value' => t('Use the lowest value of this field'),
          'average' => t('Use the average value of this field'),
          'std_deviation' => t('Use the sample standard deviation of this field'),
          'sum' => t('Sum values of this field'),
          'count' => t('Count merged values of this field'),
          'count_unique' => t('Count merged unique values of this field'),
        ],
        '#default_value' => $options['field_config'][$field]['merge_option'],
      ];
      $form['field_config'][$field]['exclude_first'] = [
        '#title' => t(''),
        '#type' => 'checkbox',
        '#default_value' => $options['field_config'][$field]['exclude_first'],
      ];
      $form['field_config'][$field]['prefix'] = [
        '#id' => 'views-merge-rows-prefix',
        '#title' => t(''),
        '#type' => 'textfield',
        '#size' => 10,
        '#default_value' => $options['field_config'][$field]['prefix'],
        '#dependency' => [
          'edit-options-field-config-' . $safe . '-merge-option' => [
            'merge',
            'merge_unique',
          ],
        ],
      ];
      $form['field_config'][$field]['separator'] = [
        '#id' => 'views-merge-rows-separator',
        '#title' => t(''),
        '#type' => 'textfield',
        '#size' => 10,
        '#default_value' => $options['field_config'][$field]['separator'],
        '#dependency' => [
          'edit-options-field-config-' . $safe . '-merge-option' => [
            'merge',
            'merge_unique',
          ],
        ],
      ];
      $form['field_config'][$field]['suffix'] = [
        '#id' => 'views-merge-rows-suffix',
        '#title' => t(''),
        '#type' => 'textfield',
        '#size' => 10,
        '#default_value' => $options['field_config'][$field]['suffix'],
        '#dependency' => [
          'edit-options-field-config-' . $safe . '-merge-option' => [
            'merge',
            'merge_unique',
          ],
        ],
      ];
    }
  }
}