You are here

public function ExposedGroups::submitOptionsForm in Views exposed groups 3.0.x

Handle any special handling on the validate form.

Overrides PluginBase::submitOptionsForm

File

src/Plugin/views/exposed_form/ExposedGroups.php, line 286

Class

ExposedGroups
Provides an views exposed form plugin that groups filters.

Namespace

Drupal\views_exposed_groups\Plugin\views\exposed_form

Code

public function submitOptionsForm(&$form, FormStateInterface $form_state) {
  $parents = [
    'manage_groups',
    'groups',
  ];
  $groups_value = $form_state
    ->getValue($parents);
  $new_groups = explode("\n", $groups_value);
  $new_groups = array_map(function ($group_weight, $group_label) {
    $label = trim($group_label);
    return [
      'name' => strtolower(preg_replace('/[^a-zA-Z0-9_]/', '_', $label)),
      'label' => $label,
      'weight' => $group_weight,
      'filters' => [],
    ];
  }, array_keys($new_groups), $new_groups);
  $group_names = array_reduce($new_groups, function (&$result, $group) {
    $result[] = $group['name'];
    return $result;
  }, []);
  $filters = $form_state
    ->getValue([
    'exposed_form_options',
    'filters',
  ]);
  $none_index = array_search('_none', $group_names);
  foreach ($filters as $id => $filter) {
    $index = array_search($filter['group'], $group_names);
    if ($index === FALSE) {

      // The filter is not in any of the new groups so it needs to be moved to
      // the _none group.
      $index = $none_index;
    }
    $new_groups[$index]['filters'][] = [
      'id' => $id,
      'weight' => $filter['weight'],
    ];
  }
  $this->options['exposed_groups'] = $new_groups;
  $form_state
    ->setValue([
    'exposed_form_options',
    'exposed_groups',
  ], $new_groups);
  parent::submitOptionsForm($form, $form_state);
}