You are here

public function ReorderDisplays::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::submitForm()

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/ReorderDisplays.php, line 151

Class

ReorderDisplays
Displays the display reorder form.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var $view \Drupal\views_ui\ViewUI */
  $view = $form_state
    ->get('view');
  $order = [];
  $user_input = $form_state
    ->getUserInput();
  foreach ($user_input['displays'] as $display => $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) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
      $order[$display] = $info['weight'];
    }
  }

  // Sort the order array.
  asort($order);

  // Remove the default display from ordering.
  unset($order['default']);

  // Increment up positions.
  $position = 1;
  foreach (array_keys($order) as $display) {
    $order[$display] = $position++;
  }

  // Setting up position and removing deleted displays.
  $displays = $view
    ->get('display');
  foreach ($displays as $display_id => &$display) {

    // Don't touch the default.
    if ($display_id === 'default') {
      $display['position'] = 0;
      continue;
    }
    if (isset($order[$display_id])) {
      $display['position'] = $order[$display_id];
    }
    else {
      $display['deleted'] = TRUE;
    }
  }
  $view
    ->set('display', $displays);

  // Store in cache.
  $view
    ->cacheSet();
  $url = $view
    ->toUrl('edit-form')
    ->setOption('fragment', 'views-tab-default');
  $form_state
    ->setRedirectUrl($url);
}