RearrangeFilter::submitForm

public RearrangeFilter::submitForm(array &$form, FormStateInterface $form_state)

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ViewsFormBase::submitForm

File

core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php, line 224

Class

RearrangeFilter
Provides a rearrange form for Views filters.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $types = ViewExecutable::getHandlerTypes();
  $view = $form_state->get('view');
  $display = &$view->getExecutable()->displayHandlers->get($form_state->get('display_id'));
  $remember_groups = array();

  if (!empty($view->form_cache)) {
    $old_fields = $view->form_cache['handlers'];
  }
  else {
    $old_fields = $display->getOption($types['filter']['plural']);
  }

  $groups = $form_state->getValue('filter_groups');
  // Whatever button was clicked, re-calculate field information.
  $new_fields = $order = array();

  // Make an array with the weights
  foreach ($form_state->getValue('filters') as $field => $info) {
    // add each value that is a field with a weight to our list, but only if
    // it has had its 'removed' checkbox checked.
    if (is_array($info) && empty($info['removed'])) {
      if (isset($info['weight'])) {
        $order[$field] = $info['weight'];
      }

      if (isset($info['group'])) {
        $old_fields[$field]['group'] = $info['group'];
        $remember_groups[$info['group']][] = $field;
      }
    }
  }

  // Sort the array
  asort($order);

  // Create a new list of fields in the new order.
  foreach (array_keys($order) as $field) {
    $new_fields[$field] = $old_fields[$field];
  }

  // If the #group property is set on the clicked button, that means we are
  // either adding or removing a group, not actually updating the filters.
  $triggering_element = $form_state->getTriggeringElement();
  if (!empty($triggering_element['#group'])) {
    if ($triggering_element['#group'] == 'add') {
      // Add a new group
      $groups['groups'][] = 'AND';
    }
    else {
      // Renumber groups above the removed one down.
      foreach (array_keys($groups['groups']) as $group_id) {
        if ($group_id >= $triggering_element['#group']) {
          $old_group = $group_id + 1;
          if (isset($groups['groups'][$old_group])) {
            $groups['groups'][$group_id] = $groups['groups'][$old_group];
            if (isset($remember_groups[$old_group])) {
              foreach ($remember_groups[$old_group] as $id) {
                $new_fields[$id]['group'] = $group_id;
              }
            }
          }
          else {
            // If this is the last one, just unset it.
            unset($groups['groups'][$group_id]);
          }
        }
      }
    }
    // Update our cache with values so that cancel still works the way
    // people expect.
    $view->form_cache = [
      'key' => 'rearrange-filter',
      'groups' => $groups,
      'handlers' => $new_fields,
    ];

    // Return to this form except on actual Update.
    $view->addFormToStack('rearrange-filter', $form_state->get('display_id'), 'filter');
  }
  else {
    // The actual update button was clicked. Remove the empty groups, and
    // renumber them sequentially.
    ksort($remember_groups);
    $groups['groups'] = static::arrayKeyPlus(array_values(array_intersect_key($groups['groups'], $remember_groups)));
    // Change the 'group' key on each field to match. Here, $mapping is an
    // array whose keys are the old group numbers and whose values are the new
    // (sequentially numbered) ones.
    $mapping = array_flip(static::arrayKeyPlus(array_keys($remember_groups)));
    foreach ($new_fields as &$new_field) {
      $new_field['group'] = $mapping[$new_field['group']];
    }

    // Write the changed handler values.
    $display->setOption($types['filter']['plural'], $new_fields);
    $display->setOption('filter_groups', $groups);
    if (isset($view->form_cache)) {
      unset($view->form_cache);
    }
  }

  // Store in cache.
  $view->cacheSet();
}
doc_Drupal
2016-10-29 09:36:44
Comments
Leave a Comment

Please login to continue.