function Rearrange::submitForm

Same name and namespace in other branches
  1. 9 core/modules/views_ui/src/Form/Ajax/Rearrange.php \Drupal\views_ui\Form\Ajax\Rearrange::submitForm()
  2. 10 core/modules/views_ui/src/Form/Ajax/Rearrange.php \Drupal\views_ui\Form\Ajax\Rearrange::submitForm()
  3. 11.x core/modules/views_ui/src/Form/Ajax/Rearrange.php \Drupal\views_ui\Form\Ajax\Rearrange::submitForm()

Overrides ViewsFormBase::submitForm

File

core/modules/views_ui/src/Form/Ajax/Rearrange.php, line 151

Class

Rearrange
Provides a rearrange form for Views handlers.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $view = $form_state->get('view');
    $display_id = $form_state->get('display_id');
    $type = $form_state->get('type');
    $types = ViewExecutable::getHandlerTypes();
    $display =& $view->getExecutable()->displayHandlers
        ->get($display_id);
    $old_fields = $display->getOption($types[$type]['plural']);
    $new_fields = $order = [];
    // Make an array with the weights
    foreach ($form_state->getValue('fields') 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) && isset($info['weight']) && empty($info['removed'])) {
            $order[$field] = $info['weight'];
        }
    }
    // 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];
    }
    $display->setOption($types[$type]['plural'], $new_fields);
    // Store in cache
    $view->cacheSet();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.