1ffb710ca1dcafe2b1ab6515f52aedcb13f50960
[yaffs-website] / web / core / modules / views_ui / src / Form / Ajax / Rearrange.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 use Drupal\views\ViewEntityInterface;
9 use Drupal\views\ViewExecutable;
10
11 /**
12  * Provides a rearrange form for Views handlers.
13  */
14 class Rearrange extends ViewsFormBase {
15
16   /**
17    * Constructs a new Rearrange object.
18    */
19   public function __construct($type = NULL) {
20     $this->setType($type);
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function getFormKey() {
27     return 'rearrange';
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL) {
34     $this->setType($type);
35     return parent::getForm($view, $display_id, $js);
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function getFormId() {
42     return 'views_ui_rearrange_form';
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function buildForm(array $form, FormStateInterface $form_state) {
49     $view = $form_state->get('view');
50     $display_id = $form_state->get('display_id');
51     $type = $form_state->get('type');
52
53     $types = ViewExecutable::getHandlerTypes();
54     $executable = $view->getExecutable();
55     if (!$executable->setDisplay($display_id)) {
56       $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
57       return $form;
58     }
59     $display = &$executable->displayHandlers->get($display_id);
60     $form['#title'] = $this->t('Rearrange @type', ['@type' => $types[$type]['ltitle']]);
61     $form['#section'] = $display_id . 'rearrange-item';
62
63     if ($display->defaultableSections($types[$type]['plural'])) {
64       $section = $types[$type]['plural'];
65       $form_state->set('section', $section);
66       views_ui_standard_display_dropdown($form, $form_state, $section);
67     }
68
69     $count = 0;
70
71     // Get relationship labels
72     $relationships = [];
73     foreach ($display->getHandlers('relationship') as $id => $handler) {
74       $relationships[$id] = $handler->adminLabel();
75     }
76
77     $form['fields'] = [
78       '#type' => 'table',
79       '#header' => ['', $this->t('Weight'), $this->t('Remove')],
80       '#empty' => $this->t('No fields available.'),
81       '#tabledrag' => [
82         [
83           'action' => 'order',
84           'relationship' => 'sibling',
85           'group' => 'weight',
86         ]
87       ],
88       '#tree' => TRUE,
89       '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
90       '#suffix' => '</div>',
91     ];
92
93     foreach ($display->getOption($types[$type]['plural']) as $id => $field) {
94       $form['fields'][$id] = [];
95
96       $form['fields'][$id]['#attributes'] = ['class' => ['draggable'], 'id' => 'views-row-' . $id];
97
98       $handler = $display->getHandler($type, $id);
99       if ($handler) {
100         $name = $handler->adminLabel() . ' ' . $handler->adminSummary();
101         if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
102           $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
103         }
104         $markup = $name;
105       }
106       else {
107         $name = $id;
108         $markup = $this->t('Broken field @id', ['@id' => $id]);
109       }
110       $form['fields'][$id]['name'] = ['#markup' => $markup];
111
112       $form['fields'][$id]['weight'] = [
113         '#type' => 'textfield',
114         '#default_value' => ++$count,
115         '#attributes' => ['class' => ['weight']],
116         '#title' => t('Weight for @title', ['@title' => $name]),
117         '#title_display' => 'invisible',
118       ];
119
120       $form['fields'][$id]['removed'] = [
121         '#type' => 'checkbox',
122         '#title' => t('Remove @title', ['@title' => $name]),
123         '#title_display' => 'invisible',
124         '#id' => 'views-removed-' . $id,
125         '#attributes' => ['class' => ['views-remove-checkbox']],
126         '#default_value' => 0,
127         '#suffix' => \Drupal::l(SafeMarkup::format('<span>@text</span>', ['@text' => $this->t('Remove')]),
128           Url::fromRoute('<none>', [], [
129             'attributes' => [
130               'id' => 'views-remove-link-' . $id,
131               'class' => ['views-hidden', 'views-button-remove', 'views-remove-link'],
132               'alt' => $this->t('Remove this item'),
133               'title' => $this->t('Remove this item'),
134             ],
135           ])
136         ),
137       ];
138     }
139
140     $view->getStandardButtons($form, $form_state, 'views_ui_rearrange_form');
141
142     return $form;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function submitForm(array &$form, FormStateInterface $form_state) {
149     $view = $form_state->get('view');
150     $display_id = $form_state->get('display_id');
151     $type = $form_state->get('type');
152
153     $types = ViewExecutable::getHandlerTypes();
154     $display = &$view->getExecutable()->displayHandlers->get($display_id);
155
156     $old_fields = $display->getOption($types[$type]['plural']);
157     $new_fields = $order = [];
158
159     // Make an array with the weights
160     foreach ($form_state->getValue('fields') as $field => $info) {
161       // add each value that is a field with a weight to our list, but only if
162       // it has had its 'removed' checkbox checked.
163       if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
164         $order[$field] = $info['weight'];
165       }
166     }
167
168     // Sort the array
169     asort($order);
170
171     // Create a new list of fields in the new order.
172     foreach (array_keys($order) as $field) {
173       $new_fields[$field] = $old_fields[$field];
174     }
175     $display->setOption($types[$type]['plural'], $new_fields);
176
177     // Store in cache
178     $view->cacheSet();
179   }
180
181 }