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