3 namespace Drupal\views_ui\Form\Ajax;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Form\FormStateInterface;
10 * Displays the display reorder form.
12 class ReorderDisplays extends ViewsFormBase {
17 public function getFormKey() {
18 return 'reorder-displays';
24 public function getFormId() {
25 return 'views_ui_reorder_displays_form';
31 public function buildForm(array $form, FormStateInterface $form_state) {
32 /** @var $view \Drupal\views\ViewEntityInterface */
33 $view = $form_state->get('view');
34 $display_id = $form_state->get('display_id');
36 $form['#title'] = $this->t('Reorder displays');
37 $form['#section'] = 'reorder';
38 $form['#action'] = $this->url('views_ui.form_reorder_displays', [
40 'view' => $view->id(),
41 'display_id' => $display_id,
48 $displays = $view->get('display');
49 $count = count($displays);
52 uasort($displays, function ($display1, $display2) {
53 if ($display1['position'] != $display2['position']) {
54 return $display1['position'] < $display2['position'] ? -1 : 1;
61 '#id' => 'reorder-displays',
62 '#header' => [$this->t('Display'), $this->t('Weight'), $this->t('Remove')],
63 '#empty' => $this->t('No displays available.'),
67 'relationship' => 'sibling',
72 '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
73 '#suffix' => '</div>',
76 foreach ($displays as $id => $display) {
77 $form['displays'][$id] = [
78 '#display' => $display,
80 'id' => 'display-row-' . $id,
82 '#weight' => $display['position'],
85 // Only make row draggable if it's not the default display.
86 if ($id !== 'default') {
87 $form['displays'][$id]['#attributes']['class'][] = 'draggable';
90 $form['displays'][$id]['title'] = [
91 '#markup' => $display['display_title'],
94 $form['displays'][$id]['weight'] = [
96 '#value' => $display['position'],
98 '#title' => $this->t('Weight for @display', ['@display' => $display['display_title']]),
99 '#title_display' => 'invisible',
101 'class' => ['weight'],
105 $form['displays'][$id]['removed'] = [
107 '#title' => t('Remove @id', ['@id' => $id]),
108 '#title_display' => 'invisible',
109 '#type' => 'checkbox',
110 '#id' => 'display-removed-' . $id,
112 'class' => ['views-remove-checkbox'],
114 '#default_value' => !empty($display['deleted']),
118 '#title' => SafeMarkup::format('<span>@text</span>', ['@text' => $this->t('Remove')]),
119 '#url' => Url::fromRoute('<none>'),
121 'id' => 'display-remove-link-' . $id,
122 'class' => ['views-button-remove', 'display-remove-link'],
123 'alt' => $this->t('Remove this display'),
124 'title' => $this->t('Remove this display'),
127 '#access' => ($id !== 'default'),
130 if (!empty($display['deleted'])) {
131 $form['displays'][$id]['deleted'] = [
136 $form['displays'][$id]['#attributes']['class'][] = 'hidden';
141 $view->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
149 public function submitForm(array &$form, FormStateInterface $form_state) {
150 /** @var $view \Drupal\views_ui\ViewUI */
151 $view = $form_state->get('view');
154 $user_input = $form_state->getUserInput();
155 foreach ($user_input['displays'] as $display => $info) {
156 // Add each value that is a field with a weight to our list, but only if
157 // it has had its 'removed' checkbox checked.
158 if (is_array($info) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
159 $order[$display] = $info['weight'];
163 // Sort the order array.
166 // Remove the default display from ordering.
167 unset($order['default']);
168 // Increment up positions.
171 foreach (array_keys($order) as $display) {
172 $order[$display] = $position++;
175 // Setting up position and removing deleted displays.
176 $displays = $view->get('display');
177 foreach ($displays as $display_id => &$display) {
178 // Don't touch the default.
179 if ($display_id === 'default') {
180 $display['position'] = 0;
183 if (isset($order[$display_id])) {
184 $display['position'] = $order[$display_id];
187 $display['deleted'] = TRUE;
190 $view->set('display', $displays);
194 $url = $view->urlInfo('edit-form')
195 ->setOption('fragment', 'views-tab-default');
196 $form_state->setRedirectUrl($url);