public function RearrangeFilter::submitForm

Overrides \Drupal\views_ui\Form\Ajax\ViewsFormBase::submitForm().

Overrides ViewsFormBase::submitForm

File

drupal/core/modules/views_ui/lib/Drupal/views_ui/Form/Ajax/RearrangeFilter.php, line 213
Contains \Drupal\views_ui\Form\Ajax\RearrangeFilter.

Class

RearrangeFilter
Provides a rearrange form for Views filters.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function submitForm(array &$form, array &$form_state) {
  $types = ViewExecutable::viewsHandlerTypes();
  $display =& $form_state['view']
    ->get('executable')->displayHandlers
    ->get($form_state['display_id']);
  $remember_groups = array();
  if (!empty($form_state['view']->form_cache)) {
    $old_fields = $form_state['view']->form_cache['handlers'];
  }
  else {
    $old_fields = $display
      ->getOption($types['filter']['plural']);
  }
  $count = 0;
  $groups = $form_state['values']['filter_groups'];

  // Whatever button was clicked, re-calculate field information.
  $new_fields = $order = array();

  // Make an array with the weights
  foreach ($form_state['values']['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.
  if (!empty($form_state['clicked_button']['#group'])) {
    if ($form_state['clicked_button']['#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 >= $form_state['clicked_button']['#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.
    $form_state['view']->form_cache = array(
      'key' => 'rearrange-filter',
      'groups' => $groups,
      'handlers' => $new_fields,
    );

    // Return to this form except on actual Update.
    $form_state['view']
      ->addFormToStack('rearrange-filter', $form_state['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($form_state['view']->form_cache)) {
      unset($form_state['view']->form_cache);
    }
  }

  // Store in cache.
  $form_state['view']
    ->cacheSet();
}