3 namespace Drupal\views_ui\Form\Ajax;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Form\FormStateInterface;
10 * Displays the display reorder form.
14 class ReorderDisplays extends ViewsFormBase {
19 public function getFormKey() {
20 return 'reorder-displays';
26 public function getFormId() {
27 return 'views_ui_reorder_displays_form';
33 public function buildForm(array $form, FormStateInterface $form_state) {
34 /** @var $view \Drupal\views\ViewEntityInterface */
35 $view = $form_state->get('view');
36 $display_id = $form_state->get('display_id');
38 $form['#title'] = $this->t('Reorder displays');
39 $form['#section'] = 'reorder';
40 $form['#action'] = $this->url('views_ui.form_reorder_displays', [
42 'view' => $view->id(),
43 'display_id' => $display_id,
50 $displays = $view->get('display');
51 $count = count($displays);
54 uasort($displays, function ($display1, $display2) {
55 if ($display1['position'] != $display2['position']) {
56 return $display1['position'] < $display2['position'] ? -1 : 1;
63 '#id' => 'reorder-displays',
64 '#header' => [$this->t('Display'), $this->t('Weight'), $this->t('Remove')],
65 '#empty' => $this->t('No displays available.'),
69 'relationship' => 'sibling',
74 '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
75 '#suffix' => '</div>',
78 foreach ($displays as $id => $display) {
79 $form['displays'][$id] = [
80 '#display' => $display,
82 'id' => 'display-row-' . $id,
84 '#weight' => $display['position'],
87 // Only make row draggable if it's not the default display.
88 if ($id !== 'default') {
89 $form['displays'][$id]['#attributes']['class'][] = 'draggable';
92 $form['displays'][$id]['title'] = [
93 '#markup' => $display['display_title'],
96 $form['displays'][$id]['weight'] = [
98 '#value' => $display['position'],
100 '#title' => $this->t('Weight for @display', ['@display' => $display['display_title']]),
101 '#title_display' => 'invisible',
103 'class' => ['weight'],
107 $form['displays'][$id]['removed'] = [
109 '#title' => t('Remove @id', ['@id' => $id]),
110 '#title_display' => 'invisible',
111 '#type' => 'checkbox',
112 '#id' => 'display-removed-' . $id,
114 'class' => ['views-remove-checkbox'],
116 '#default_value' => !empty($display['deleted']),
120 '#title' => new FormattableMarkup('<span>@text</span>', ['@text' => $this->t('Remove')]),
121 '#url' => Url::fromRoute('<none>'),
123 'id' => 'display-remove-link-' . $id,
124 'class' => ['views-button-remove', 'display-remove-link'],
125 'alt' => $this->t('Remove this display'),
126 'title' => $this->t('Remove this display'),
129 '#access' => ($id !== 'default'),
132 if (!empty($display['deleted'])) {
133 $form['displays'][$id]['deleted'] = [
138 $form['displays'][$id]['#attributes']['class'][] = 'hidden';
143 $view->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
151 public function submitForm(array &$form, FormStateInterface $form_state) {
152 /** @var $view \Drupal\views_ui\ViewUI */
153 $view = $form_state->get('view');
156 $user_input = $form_state->getUserInput();
157 foreach ($user_input['displays'] as $display => $info) {
158 // Add each value that is a field with a weight to our list, but only if
159 // it has had its 'removed' checkbox checked.
160 if (is_array($info) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
161 $order[$display] = $info['weight'];
165 // Sort the order array.
168 // Remove the default display from ordering.
169 unset($order['default']);
170 // Increment up positions.
173 foreach (array_keys($order) as $display) {
174 $order[$display] = $position++;
177 // Setting up position and removing deleted displays.
178 $displays = $view->get('display');
179 foreach ($displays as $display_id => &$display) {
180 // Don't touch the default.
181 if ($display_id === 'default') {
182 $display['position'] = 0;
185 if (isset($order[$display_id])) {
186 $display['position'] = $order[$display_id];
189 $display['deleted'] = TRUE;
192 $view->set('display', $displays);
196 $url = $view->urlInfo('edit-form')
197 ->setOption('fragment', 'views-tab-default');
198 $form_state->setRedirectUrl($url);