Version 1
[yaffs-website] / web / core / modules / views_ui / src / Form / Ajax / ReorderDisplays.php
1 <?php
2
3 namespace Drupal\views_ui\Form\Ajax;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Url;
8
9 /**
10  * Displays the display reorder form.
11  */
12 class ReorderDisplays extends ViewsFormBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function getFormKey() {
18     return 'reorder-displays';
19   }
20
21   /**
22    * {@inheritdoc}
23    */
24   public function getFormId() {
25     return 'views_ui_reorder_displays_form';
26   }
27
28   /**
29    * {@inheritdoc}
30    */
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');
35
36     $form['#title'] = $this->t('Reorder displays');
37     $form['#section'] = 'reorder';
38     $form['#action'] = $this->url('views_ui.form_reorder_displays', [
39       'js' => 'nojs',
40       'view' => $view->id(),
41       'display_id' => $display_id,
42     ]);
43     $form['view'] = [
44       '#type' => 'value',
45       '#value' => $view
46     ];
47
48     $displays = $view->get('display');
49     $count = count($displays);
50
51     // Sort the displays.
52     uasort($displays, function ($display1, $display2) {
53       if ($display1['position'] != $display2['position']) {
54         return $display1['position'] < $display2['position'] ? -1 : 1;
55       }
56       return 0;
57     });
58
59     $form['displays'] = [
60       '#type' => 'table',
61       '#id' => 'reorder-displays',
62       '#header' => [$this->t('Display'), $this->t('Weight'), $this->t('Remove')],
63       '#empty' => $this->t('No displays available.'),
64       '#tabledrag' => [
65         [
66           'action' => 'order',
67           'relationship' => 'sibling',
68           'group' => 'weight',
69         ]
70       ],
71       '#tree' => TRUE,
72       '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
73       '#suffix' => '</div>',
74     ];
75
76     foreach ($displays as $id => $display) {
77       $form['displays'][$id] = [
78         '#display' => $display,
79         '#attributes' => [
80           'id' => 'display-row-' . $id,
81         ],
82         '#weight' => $display['position'],
83       ];
84
85       // Only make row draggable if it's not the default display.
86       if ($id !== 'default') {
87         $form['displays'][$id]['#attributes']['class'][] = 'draggable';
88       }
89
90       $form['displays'][$id]['title'] = [
91         '#markup' => $display['display_title'],
92       ];
93
94       $form['displays'][$id]['weight'] = [
95         '#type' => 'weight',
96         '#value' => $display['position'],
97         '#delta' => $count,
98         '#title' => $this->t('Weight for @display', ['@display' => $display['display_title']]),
99         '#title_display' => 'invisible',
100         '#attributes' => [
101           'class' => ['weight'],
102         ],
103       ];
104
105       $form['displays'][$id]['removed'] = [
106         'checkbox' => [
107           '#title' => t('Remove @id', ['@id' => $id]),
108           '#title_display' => 'invisible',
109           '#type' => 'checkbox',
110           '#id' => 'display-removed-' . $id,
111           '#attributes' => [
112             'class' => ['views-remove-checkbox'],
113           ],
114           '#default_value' => !empty($display['deleted']),
115         ],
116         'link' => [
117           '#type' => 'link',
118           '#title' => SafeMarkup::format('<span>@text</span>', ['@text' => $this->t('Remove')]),
119           '#url' => Url::fromRoute('<none>'),
120           '#attributes' => [
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'),
125           ],
126         ],
127         '#access' => ($id !== 'default'),
128       ];
129
130       if (!empty($display['deleted'])) {
131         $form['displays'][$id]['deleted'] = [
132           '#type' => 'value',
133           '#value' => TRUE,
134         ];
135
136         $form['displays'][$id]['#attributes']['class'][] = 'hidden';
137       }
138
139     }
140
141     $view->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
142
143     return $form;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function submitForm(array &$form, FormStateInterface $form_state) {
150     /** @var $view \Drupal\views_ui\ViewUI */
151     $view = $form_state->get('view');
152     $order = [];
153
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'];
160       }
161     }
162
163     // Sort the order array.
164     asort($order);
165
166     // Remove the default display from ordering.
167     unset($order['default']);
168     // Increment up positions.
169     $position = 1;
170
171     foreach (array_keys($order) as $display) {
172       $order[$display] = $position++;
173     }
174
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;
181         continue;
182       }
183       if (isset($order[$display_id])) {
184         $display['position'] = $order[$display_id];
185       }
186       else {
187         $display['deleted'] = TRUE;
188       }
189     }
190     $view->set('display', $displays);
191
192     // Store in cache.
193     $view->cacheSet();
194     $url = $view->urlInfo('edit-form')
195       ->setOption('fragment', 'views-tab-default');
196     $form_state->setRedirectUrl($url);
197   }
198
199 }