You are here

public function ViewsMergeRowsDisplayExtenderPlugin::buildOptionsForm in Views Merge Rows 8.2

@phpstan-ignore-next-line

Overrides DisplayExtenderPluginBase::buildOptionsForm

File

src/Plugin/views/display_extender/ViewsMergeRowsDisplayExtenderPlugin.php, line 39

Class

ViewsMergeRowsDisplayExtenderPlugin
Provides interface to manage merge options on a per-field basis.

Namespace

Drupal\views_merge_rows\Plugin\views\display_extender

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) : void {
  if ($form_state
    ->get('section') != 'views_merge_rows') {
    return;
  }
  $form['#tree'] = TRUE;
  $form['#theme'] = 'merge_rows_theme';
  $form['#title'] .= $this
    ->t('Merge rows with the same content.');
  $form['warning_markup'] = [];
  if ($this->displayHandler
    ->usesPager()) {
    $form['warning_markup'] = [
      '#theme' => 'status_messages',
      '#message_list' => [
        'warning' => [
          $this
            ->t('It is highly recommended to disable pager if you merge rows.'),
        ],
      ],
    ];
  }
  $form['merge_rows'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Merge rows with the same content in the specified fields'),
    '#default_value' => $this->options['merge_rows'],
  ];

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

    // 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' => $this
          ->t('Use values of this field as a filter'),
        'merge' => $this
          ->t('Merge values of this field'),
        'merge_unique' => $this
          ->t('Merge unique values of this field'),
        'first_value' => $this
          ->t('Use the first value of this field'),
        'highest_value' => $this
          ->t('Use the highest value of this field'),
        'lowest_value' => $this
          ->t('Use the lowest value of this field'),
        'average' => $this
          ->t('Use the average value of this field'),
        'std_deviation' => $this
          ->t('Use the sample standard deviation of this field'),
        'sum' => $this
          ->t('Sum values of this field'),
        'count' => $this
          ->t('Count merged values of this field'),
        'count_unique' => $this
          ->t('Count merged unique values of this field'),
        'count_minus_count_unique' => $this
          ->t('Calculate the number of merged values minus the number of merged unique values of this field'),
      ],
      '#default_value' => $this->options['field_config'][$field]['merge_option'] ?? 'merge_unique',
    ];
    $form['field_config'][$field]['exclude_first'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude first option for @field_name', [
        '@field_name' => $name,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $this->options['field_config'][$field]['exclude_first'] ?? FALSE,
    ];
    $form['field_config'][$field]['prefix'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Prefix for @field_name', [
        '@field_name' => $name,
      ]),
      '#title_display' => 'invisible',
      '#size' => 10,
      '#default_value' => $this->options['field_config'][$field]['prefix'] ?? '',
    ];
    $form['field_config'][$field]['separator'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Separator for @field_name', [
        '@field_name' => $name,
      ]),
      '#title_display' => 'invisible',
      '#size' => 10,
      '#default_value' => $this->options['field_config'][$field]['separator'] ?? ', ',
    ];
    $form['field_config'][$field]['suffix'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Suffix for @field_name', [
        '@field_name' => $name,
      ]),
      '#title_display' => 'invisible',
      '#size' => 10,
      '#default_value' => $this->options['field_config'][$field]['suffix'] ?? '',
    ];
  }
  $form['merge_options_description_title'] = [
    '#type' => 'html_tag',
    '#tag' => 'h2',
    '#value' => $this
      ->t('Merge options details'),
  ];
  $form['merge_options_description'] = [
    '#theme' => 'table',
    '#header' => [
      $this
        ->t('Option'),
      $this
        ->t('Description'),
    ],
    '#rows' => [
      [
        $this
          ->t('Use values of this field as a filter'),
        $this
          ->t('Checks which rows should be merged. If several rows contain exactly the same values in all of these fields, they are merged together.'),
      ],
      [
        $this
          ->t('Merge values of this field'),
        $this
          ->t('All the values appears in the resulting row.'),
      ],
      [
        $this
          ->t('Merge unique values of this field'),
        $this
          ->t('The resulting row will contain unique values from the merged rows.'),
      ],
      [
        $this
          ->t('Use the first value of this field'),
        $this
          ->t('Only the value from the first merged rows is used. The values in other rows are disregarded.'),
      ],
      [
        $this
          ->t('Use the highest value of this field'),
        $this
          ->t('The resulting row will contain the highest numerical value from the merged rows.'),
      ],
      [
        $this
          ->t('Use the lowest value of this field'),
        $this
          ->t('The resulting row will contain the highest numerical value from the merged rows.'),
      ],
      [
        $this
          ->t('Use the average value of this field'),
        $this
          ->t('The resulting row will contain the average value from the merged rows.'),
      ],
      [
        $this
          ->t('Use the sample standard deviation of this field'),
        $this
          ->t('The resulting row will contain the sample standard deviation value from the merged rows.'),
      ],
      [
        $this
          ->t('Sum values of this field'),
        $this
          ->t('The resulting row will contain the sum of values from the merged rows.'),
      ],
      [
        $this
          ->t('Count merged values of this field'),
        $this
          ->t('The resulting row will contain the counter of the merged rows.'),
      ],
      [
        $this
          ->t('Count merged unique values of this field'),
        $this
          ->t('The resulting row will contain the counter of the merged unique values.'),
      ],
      [
        $this
          ->t('Calculate the number of merged values minus the number of merged unique values of this field'),
        $this
          ->t('The resulting row will contain the difference between counter of the merged values and the counter of the merged unique values.'),
      ],
    ],
  ];
}