Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\views_ui;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
7 use Drupal\Core\Ajax\AjaxResponse;
|
Chris@0
|
8 use Drupal\Core\Ajax\HtmlCommand;
|
Chris@0
|
9 use Drupal\Core\Ajax\ReplaceCommand;
|
Chris@0
|
10 use Drupal\Core\Datetime\DateFormatterInterface;
|
Chris@0
|
11 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
12 use Drupal\Core\Render\ElementInfoManagerInterface;
|
Chris@0
|
13 use Drupal\Core\Url;
|
Chris@0
|
14 use Drupal\user\SharedTempStoreFactory;
|
Chris@0
|
15 use Drupal\views\Views;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
17 use Symfony\Component\HttpFoundation\RequestStack;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Form controller for the Views edit form.
|
Chris@0
|
21 */
|
Chris@0
|
22 class ViewEditForm extends ViewFormBase {
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The views temp store.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\user\SharedTempStore
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $tempStore;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * The request object.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Symfony\Component\HttpFoundation\RequestStack
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $requestStack;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * The date formatter service.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var \Drupal\Core\Datetime\DateFormatterInterface
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $dateFormatter;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * The element info manager.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var \Drupal\Core\Render\ElementInfoManagerInterface
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $elementInfo;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Constructs a new ViewEditForm object.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
|
Chris@0
|
56 * The factory for the temp store object.
|
Chris@0
|
57 * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
|
Chris@0
|
58 * The request stack object.
|
Chris@0
|
59 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
Chris@0
|
60 * The date Formatter service.
|
Chris@0
|
61 * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
|
Chris@0
|
62 * The element info manager.
|
Chris@0
|
63 */
|
Chris@0
|
64 public function __construct(SharedTempStoreFactory $temp_store_factory, RequestStack $requestStack, DateFormatterInterface $date_formatter, ElementInfoManagerInterface $element_info) {
|
Chris@0
|
65 $this->tempStore = $temp_store_factory->get('views');
|
Chris@0
|
66 $this->requestStack = $requestStack;
|
Chris@0
|
67 $this->dateFormatter = $date_formatter;
|
Chris@0
|
68 $this->elementInfo = $element_info;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * {@inheritdoc}
|
Chris@0
|
73 */
|
Chris@0
|
74 public static function create(ContainerInterface $container) {
|
Chris@0
|
75 return new static(
|
Chris@0
|
76 $container->get('user.shared_tempstore'),
|
Chris@0
|
77 $container->get('request_stack'),
|
Chris@0
|
78 $container->get('date.formatter'),
|
Chris@0
|
79 $container->get('element_info')
|
Chris@0
|
80 );
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
87 $view = $this->entity;
|
Chris@0
|
88 $display_id = $this->displayID;
|
Chris@0
|
89 // Do not allow the form to be cached, because $form_state->get('view') can become
|
Chris@0
|
90 // stale between page requests.
|
Chris@0
|
91 // See views_ui_ajax_get_form() for how this affects #ajax.
|
Chris@0
|
92 // @todo To remove this and allow the form to be cacheable:
|
Chris@0
|
93 // - Change $form_state->get('view') to $form_state->getTemporary()['view'].
|
Chris@0
|
94 // - Add a #process function to initialize $form_state->getTemporary()['view']
|
Chris@0
|
95 // on cached form submissions.
|
Chris@0
|
96 // - Use \Drupal\Core\Form\FormStateInterface::loadInclude().
|
Chris@0
|
97 $form_state->disableCache();
|
Chris@0
|
98
|
Chris@0
|
99 if ($display_id) {
|
Chris@0
|
100 if (!$view->getExecutable()->setDisplay($display_id)) {
|
Chris@0
|
101 $form['#markup'] = $this->t('Invalid display id @display', ['@display' => $display_id]);
|
Chris@0
|
102 return $form;
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 $form['#tree'] = TRUE;
|
Chris@0
|
107
|
Chris@0
|
108 $form['#attached']['library'][] = 'core/jquery.ui.tabs';
|
Chris@0
|
109 $form['#attached']['library'][] = 'core/jquery.ui.dialog';
|
Chris@0
|
110 $form['#attached']['library'][] = 'core/drupal.states';
|
Chris@0
|
111 $form['#attached']['library'][] = 'core/drupal.tabledrag';
|
Chris@0
|
112 $form['#attached']['library'][] = 'views_ui/views_ui.admin';
|
Chris@0
|
113 $form['#attached']['library'][] = 'views_ui/admin.styling';
|
Chris@0
|
114
|
Chris@0
|
115 $form += [
|
Chris@0
|
116 '#prefix' => '',
|
Chris@0
|
117 '#suffix' => '',
|
Chris@0
|
118 ];
|
Chris@0
|
119
|
Chris@0
|
120 $view_status = $view->status() ? 'enabled' : 'disabled';
|
Chris@0
|
121 $form['#prefix'] .= '<div class="views-edit-view views-admin ' . $view_status . ' clearfix">';
|
Chris@0
|
122 $form['#suffix'] = '</div>' . $form['#suffix'];
|
Chris@0
|
123
|
Chris@0
|
124 $form['#attributes']['class'] = ['form-edit'];
|
Chris@0
|
125
|
Chris@0
|
126 if ($view->isLocked()) {
|
Chris@0
|
127 $username = [
|
Chris@0
|
128 '#theme' => 'username',
|
Chris@0
|
129 '#account' => $this->entityManager->getStorage('user')->load($view->lock->owner),
|
Chris@0
|
130 ];
|
Chris@0
|
131 $lock_message_substitutions = [
|
Chris@0
|
132 '@user' => \Drupal::service('renderer')->render($username),
|
Chris@0
|
133 '@age' => $this->dateFormatter->formatTimeDiffSince($view->lock->updated),
|
Chris@0
|
134 ':url' => $view->url('break-lock-form'),
|
Chris@0
|
135 ];
|
Chris@0
|
136 $form['locked'] = [
|
Chris@0
|
137 '#type' => 'container',
|
Chris@0
|
138 '#attributes' => ['class' => ['view-locked', 'messages', 'messages--warning']],
|
Chris@0
|
139 '#children' => $this->t('This view is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href=":url">break this lock</a>.', $lock_message_substitutions),
|
Chris@0
|
140 '#weight' => -10,
|
Chris@0
|
141 ];
|
Chris@0
|
142 }
|
Chris@0
|
143 else {
|
Chris@0
|
144 $form['changed'] = [
|
Chris@0
|
145 '#type' => 'container',
|
Chris@0
|
146 '#attributes' => ['class' => ['view-changed', 'messages', 'messages--warning']],
|
Chris@0
|
147 '#children' => $this->t('You have unsaved changes.'),
|
Chris@0
|
148 '#weight' => -10,
|
Chris@0
|
149 ];
|
Chris@0
|
150 if (empty($view->changed)) {
|
Chris@0
|
151 $form['changed']['#attributes']['class'][] = 'js-hide';
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 $form['displays'] = [
|
Chris@0
|
156 '#prefix' => '<h1 class="unit-title clearfix">' . $this->t('Displays') . '</h1>',
|
Chris@0
|
157 '#type' => 'container',
|
Chris@0
|
158 '#attributes' => [
|
Chris@0
|
159 'class' => [
|
Chris@0
|
160 'views-displays',
|
Chris@0
|
161 ],
|
Chris@0
|
162 ],
|
Chris@0
|
163 ];
|
Chris@0
|
164
|
Chris@0
|
165
|
Chris@0
|
166 $form['displays']['top'] = $this->renderDisplayTop($view);
|
Chris@0
|
167
|
Chris@0
|
168 // The rest requires a display to be selected.
|
Chris@0
|
169 if ($display_id) {
|
Chris@0
|
170 $form_state->set('display_id', $display_id);
|
Chris@0
|
171
|
Chris@0
|
172 // The part of the page where editing will take place.
|
Chris@0
|
173 $form['displays']['settings'] = [
|
Chris@0
|
174 '#type' => 'container',
|
Chris@0
|
175 '#id' => 'edit-display-settings',
|
Chris@0
|
176 '#attributes' => [
|
Chris@0
|
177 'class' => ['edit-display-settings'],
|
Chris@0
|
178 ],
|
Chris@0
|
179 ];
|
Chris@0
|
180
|
Chris@0
|
181 // Add a text that the display is disabled.
|
Chris@0
|
182 if ($view->getExecutable()->displayHandlers->has($display_id)) {
|
Chris@0
|
183 if (!$view->getExecutable()->displayHandlers->get($display_id)->isEnabled()) {
|
Chris@0
|
184 $form['displays']['settings']['disabled']['#markup'] = $this->t('This display is disabled.');
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 // Add the edit display content
|
Chris@0
|
189 $tab_content = $this->getDisplayTab($view);
|
Chris@0
|
190 $tab_content['#theme_wrappers'] = ['container'];
|
Chris@0
|
191 $tab_content['#attributes'] = ['class' => ['views-display-tab']];
|
Chris@0
|
192 $tab_content['#id'] = 'views-tab-' . $display_id;
|
Chris@0
|
193 // Mark deleted displays as such.
|
Chris@0
|
194 $display = $view->get('display');
|
Chris@0
|
195 if (!empty($display[$display_id]['deleted'])) {
|
Chris@0
|
196 $tab_content['#attributes']['class'][] = 'views-display-deleted';
|
Chris@0
|
197 }
|
Chris@0
|
198 // Mark disabled displays as such.
|
Chris@0
|
199
|
Chris@0
|
200 if ($view->getExecutable()->displayHandlers->has($display_id) && !$view->getExecutable()->displayHandlers->get($display_id)->isEnabled()) {
|
Chris@0
|
201 $tab_content['#attributes']['class'][] = 'views-display-disabled';
|
Chris@0
|
202 }
|
Chris@0
|
203 $form['displays']['settings']['settings_content'] = [
|
Chris@0
|
204 '#type' => 'container',
|
Chris@0
|
205 'tab_content' => $tab_content,
|
Chris@0
|
206 ];
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 return $form;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 protected function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
216 $actions = parent::actions($form, $form_state);
|
Chris@0
|
217 unset($actions['delete']);
|
Chris@0
|
218
|
Chris@0
|
219 $actions['cancel'] = [
|
Chris@0
|
220 '#type' => 'submit',
|
Chris@0
|
221 '#value' => $this->t('Cancel'),
|
Chris@0
|
222 '#submit' => ['::cancel'],
|
Chris@0
|
223 '#limit_validation_errors' => [],
|
Chris@0
|
224 ];
|
Chris@0
|
225 if ($this->entity->isLocked()) {
|
Chris@0
|
226 $actions['submit']['#access'] = FALSE;
|
Chris@0
|
227 $actions['cancel']['#access'] = FALSE;
|
Chris@0
|
228 }
|
Chris@0
|
229 return $actions;
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 /**
|
Chris@0
|
233 * {@inheritdoc}
|
Chris@0
|
234 */
|
Chris@0
|
235 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
236 parent::validateForm($form, $form_state);
|
Chris@0
|
237
|
Chris@0
|
238 $view = $this->entity;
|
Chris@0
|
239 if ($view->isLocked()) {
|
Chris@0
|
240 $form_state->setErrorByName('', $this->t('Changes cannot be made to a locked view.'));
|
Chris@0
|
241 }
|
Chris@0
|
242 foreach ($view->getExecutable()->validate() as $display_errors) {
|
Chris@0
|
243 foreach ($display_errors as $error) {
|
Chris@0
|
244 $form_state->setErrorByName('', $error);
|
Chris@0
|
245 }
|
Chris@0
|
246 }
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 /**
|
Chris@0
|
250 * {@inheritdoc}
|
Chris@0
|
251 */
|
Chris@0
|
252 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
253 $view = $this->entity;
|
Chris@0
|
254 $executable = $view->getExecutable();
|
Chris@0
|
255 $executable->initDisplay();
|
Chris@0
|
256
|
Chris@0
|
257 // Go through and remove displayed scheduled for removal.
|
Chris@0
|
258 $displays = $view->get('display');
|
Chris@0
|
259 foreach ($displays as $id => $display) {
|
Chris@0
|
260 if (!empty($display['deleted'])) {
|
Chris@0
|
261 // Remove view display from view attachment under the attachments
|
Chris@0
|
262 // options.
|
Chris@0
|
263 $display_handler = $executable->displayHandlers->get($id);
|
Chris@0
|
264 if ($attachments = $display_handler->getAttachedDisplays()) {
|
Chris@0
|
265 foreach ($attachments as $attachment) {
|
Chris@0
|
266 $attached_options = $executable->displayHandlers->get($attachment)->getOption('displays');
|
Chris@0
|
267 unset($attached_options[$id]);
|
Chris@0
|
268 $executable->displayHandlers->get($attachment)->setOption('displays', $attached_options);
|
Chris@0
|
269 }
|
Chris@0
|
270 }
|
Chris@0
|
271 $executable->displayHandlers->remove($id);
|
Chris@0
|
272 unset($displays[$id]);
|
Chris@0
|
273 }
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 // Rename display ids if needed.
|
Chris@0
|
277 foreach ($executable->displayHandlers as $id => $display) {
|
Chris@0
|
278 if (!empty($display->display['new_id']) && $display->display['new_id'] !== $display->display['id'] && empty($display->display['deleted'])) {
|
Chris@0
|
279 $new_id = $display->display['new_id'];
|
Chris@0
|
280 $display->display['id'] = $new_id;
|
Chris@0
|
281 unset($display->display['new_id']);
|
Chris@0
|
282 $executable->displayHandlers->set($new_id, $display);
|
Chris@0
|
283
|
Chris@0
|
284 $displays[$new_id] = $displays[$id];
|
Chris@0
|
285 unset($displays[$id]);
|
Chris@0
|
286
|
Chris@0
|
287 // Redirect the user to the renamed display to be sure that the page itself exists and doesn't throw errors.
|
Chris@0
|
288 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
289 'view' => $view->id(),
|
Chris@0
|
290 'display_id' => $new_id,
|
Chris@0
|
291 ]);
|
Chris@0
|
292 }
|
Chris@0
|
293 elseif (isset($display->display['new_id'])) {
|
Chris@0
|
294 unset($display->display['new_id']);
|
Chris@0
|
295 }
|
Chris@0
|
296 }
|
Chris@0
|
297 $view->set('display', $displays);
|
Chris@0
|
298
|
Chris@0
|
299 // @todo: Revisit this when https://www.drupal.org/node/1668866 is in.
|
Chris@0
|
300 $query = $this->requestStack->getCurrentRequest()->query;
|
Chris@0
|
301 $destination = $query->get('destination');
|
Chris@0
|
302
|
Chris@0
|
303 if (!empty($destination)) {
|
Chris@0
|
304 // Find out the first display which has a changed path and redirect to this url.
|
Chris@0
|
305 $old_view = Views::getView($view->id());
|
Chris@0
|
306 $old_view->initDisplay();
|
Chris@0
|
307 foreach ($old_view->displayHandlers as $id => $display) {
|
Chris@0
|
308 // Only check for displays with a path.
|
Chris@0
|
309 $old_path = $display->getOption('path');
|
Chris@0
|
310 if (empty($old_path)) {
|
Chris@0
|
311 continue;
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 if (($display->getPluginId() == 'page') && ($old_path == $destination) && ($old_path != $view->getExecutable()->displayHandlers->get($id)->getOption('path'))) {
|
Chris@0
|
315 $destination = $view->getExecutable()->displayHandlers->get($id)->getOption('path');
|
Chris@0
|
316 $query->remove('destination');
|
Chris@0
|
317 }
|
Chris@0
|
318 }
|
Chris@0
|
319 // @todo Use Url::fromPath() once https://www.drupal.org/node/2351379 is
|
Chris@0
|
320 // resolved.
|
Chris@0
|
321 $form_state->setRedirectUrl(Url::fromUri("base:$destination"));
|
Chris@0
|
322 }
|
Chris@0
|
323
|
Chris@0
|
324 $view->save();
|
Chris@0
|
325
|
Chris@0
|
326 drupal_set_message($this->t('The view %name has been saved.', ['%name' => $view->label()]));
|
Chris@0
|
327
|
Chris@0
|
328 // Remove this view from cache so we can edit it properly.
|
Chris@0
|
329 $this->tempStore->delete($view->id());
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 /**
|
Chris@0
|
333 * Form submission handler for the 'cancel' action.
|
Chris@0
|
334 *
|
Chris@0
|
335 * @param array $form
|
Chris@0
|
336 * An associative array containing the structure of the form.
|
Chris@0
|
337 * @param \Drupal\Core\Form\FormStateInterface $form_state
|
Chris@0
|
338 * The current state of the form.
|
Chris@0
|
339 */
|
Chris@0
|
340 public function cancel(array $form, FormStateInterface $form_state) {
|
Chris@0
|
341 // Remove this view from cache so edits will be lost.
|
Chris@0
|
342 $view = $this->entity;
|
Chris@0
|
343 $this->tempStore->delete($view->id());
|
Chris@0
|
344 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 /**
|
Chris@0
|
348 * Returns a renderable array representing the edit page for one display.
|
Chris@0
|
349 */
|
Chris@0
|
350 public function getDisplayTab($view) {
|
Chris@0
|
351 $build = [];
|
Chris@0
|
352 $display_id = $this->displayID;
|
Chris@0
|
353 $display = $view->getExecutable()->displayHandlers->get($display_id);
|
Chris@0
|
354 // If the plugin doesn't exist, display an error message instead of an edit
|
Chris@0
|
355 // page.
|
Chris@0
|
356 if (empty($display)) {
|
Chris@0
|
357 // @TODO: Improved UX for the case where a plugin is missing.
|
Chris@0
|
358 $build['#markup'] = $this->t("Error: Display @display refers to a plugin named '@plugin', but that plugin is not available.", ['@display' => $display->display['id'], '@plugin' => $display->display['display_plugin']]);
|
Chris@0
|
359 }
|
Chris@0
|
360 // Build the content of the edit page.
|
Chris@0
|
361 else {
|
Chris@0
|
362 $build['details'] = $this->getDisplayDetails($view, $display->display);
|
Chris@0
|
363 }
|
Chris@0
|
364 // In AJAX context, ViewUI::rebuildCurrentTab() returns this outside of form
|
Chris@0
|
365 // context, so hook_form_views_ui_edit_form_alter() is insufficient.
|
Chris@0
|
366 \Drupal::moduleHandler()->alter('views_ui_display_tab', $build, $view, $display_id);
|
Chris@0
|
367 return $build;
|
Chris@0
|
368 }
|
Chris@0
|
369
|
Chris@0
|
370 /**
|
Chris@0
|
371 * Helper function to get the display details section of the edit UI.
|
Chris@0
|
372 *
|
Chris@0
|
373 * @param $display
|
Chris@0
|
374 *
|
Chris@0
|
375 * @return array
|
Chris@0
|
376 * A renderable page build array.
|
Chris@0
|
377 */
|
Chris@0
|
378 public function getDisplayDetails($view, $display) {
|
Chris@0
|
379 $display_title = $this->getDisplayLabel($view, $display['id'], FALSE);
|
Chris@0
|
380 $build = [
|
Chris@0
|
381 '#theme_wrappers' => ['container'],
|
Chris@0
|
382 '#attributes' => ['id' => 'edit-display-settings-details'],
|
Chris@0
|
383 ];
|
Chris@0
|
384
|
Chris@0
|
385 $is_display_deleted = !empty($display['deleted']);
|
Chris@0
|
386 // The master display cannot be duplicated.
|
Chris@0
|
387 $is_default = $display['id'] == 'default';
|
Chris@0
|
388 // @todo: Figure out why getOption doesn't work here.
|
Chris@0
|
389 $is_enabled = $view->getExecutable()->displayHandlers->get($display['id'])->isEnabled();
|
Chris@0
|
390
|
Chris@0
|
391 if ($display['id'] != 'default') {
|
Chris@0
|
392 $build['top']['#theme_wrappers'] = ['container'];
|
Chris@0
|
393 $build['top']['#attributes']['id'] = 'edit-display-settings-top';
|
Chris@0
|
394 $build['top']['#attributes']['class'] = ['views-ui-display-tab-actions', 'edit-display-settings-top', 'views-ui-display-tab-bucket', 'clearfix'];
|
Chris@0
|
395
|
Chris@0
|
396 // The Delete, Duplicate and Undo Delete buttons.
|
Chris@0
|
397 $build['top']['actions'] = [
|
Chris@0
|
398 '#theme_wrappers' => ['dropbutton_wrapper'],
|
Chris@0
|
399 ];
|
Chris@0
|
400
|
Chris@0
|
401 // Because some of the 'links' are actually submit buttons, we have to
|
Chris@0
|
402 // manually wrap each item in <li> and the whole list in <ul>.
|
Chris@0
|
403 $build['top']['actions']['prefix']['#markup'] = '<ul class="dropbutton">';
|
Chris@0
|
404
|
Chris@0
|
405 if (!$is_display_deleted) {
|
Chris@0
|
406 if (!$is_enabled) {
|
Chris@0
|
407 $build['top']['actions']['enable'] = [
|
Chris@0
|
408 '#type' => 'submit',
|
Chris@0
|
409 '#value' => $this->t('Enable @display_title', ['@display_title' => $display_title]),
|
Chris@0
|
410 '#limit_validation_errors' => [],
|
Chris@0
|
411 '#submit' => ['::submitDisplayEnable', '::submitDelayDestination'],
|
Chris@0
|
412 '#prefix' => '<li class="enable">',
|
Chris@0
|
413 "#suffix" => '</li>',
|
Chris@0
|
414 ];
|
Chris@0
|
415 }
|
Chris@0
|
416 // Add a link to view the page unless the view is disabled or has no
|
Chris@0
|
417 // path.
|
Chris@0
|
418 elseif ($view->status() && $view->getExecutable()->displayHandlers->get($display['id'])->hasPath()) {
|
Chris@0
|
419 $path = $view->getExecutable()->displayHandlers->get($display['id'])->getPath();
|
Chris@0
|
420 if ($path && (strpos($path, '%') === FALSE)) {
|
Chris@0
|
421 if (!parse_url($path, PHP_URL_SCHEME)) {
|
Chris@0
|
422 // @todo Views should expect and store a leading /. See:
|
Chris@0
|
423 // https://www.drupal.org/node/2423913
|
Chris@0
|
424 $url = Url::fromUserInput('/' . ltrim($path, '/'));
|
Chris@0
|
425 }
|
Chris@0
|
426 else {
|
Chris@0
|
427 $url = Url::fromUri("base:$path");
|
Chris@0
|
428 }
|
Chris@0
|
429 $build['top']['actions']['path'] = [
|
Chris@0
|
430 '#type' => 'link',
|
Chris@0
|
431 '#title' => $this->t('View @display_title', ['@display_title' => $display_title]),
|
Chris@0
|
432 '#options' => ['alt' => [$this->t("Go to the real page for this display")]],
|
Chris@0
|
433 '#url' => $url,
|
Chris@0
|
434 '#prefix' => '<li class="view">',
|
Chris@0
|
435 "#suffix" => '</li>',
|
Chris@0
|
436 ];
|
Chris@0
|
437 }
|
Chris@0
|
438 }
|
Chris@0
|
439 if (!$is_default) {
|
Chris@0
|
440 $build['top']['actions']['duplicate'] = [
|
Chris@0
|
441 '#type' => 'submit',
|
Chris@0
|
442 '#value' => $this->t('Duplicate @display_title', ['@display_title' => $display_title]),
|
Chris@0
|
443 '#limit_validation_errors' => [],
|
Chris@0
|
444 '#submit' => ['::submitDisplayDuplicate', '::submitDelayDestination'],
|
Chris@0
|
445 '#prefix' => '<li class="duplicate">',
|
Chris@0
|
446 "#suffix" => '</li>',
|
Chris@0
|
447 ];
|
Chris@0
|
448 }
|
Chris@0
|
449 // Always allow a display to be deleted.
|
Chris@0
|
450 $build['top']['actions']['delete'] = [
|
Chris@0
|
451 '#type' => 'submit',
|
Chris@0
|
452 '#value' => $this->t('Delete @display_title', ['@display_title' => $display_title]),
|
Chris@0
|
453 '#limit_validation_errors' => [],
|
Chris@0
|
454 '#submit' => ['::submitDisplayDelete', '::submitDelayDestination'],
|
Chris@0
|
455 '#prefix' => '<li class="delete">',
|
Chris@0
|
456 "#suffix" => '</li>',
|
Chris@0
|
457 ];
|
Chris@0
|
458
|
Chris@0
|
459 foreach (Views::fetchPluginNames('display', NULL, [$view->get('storage')->get('base_table')]) as $type => $label) {
|
Chris@0
|
460 if ($type == $display['display_plugin']) {
|
Chris@0
|
461 continue;
|
Chris@0
|
462 }
|
Chris@0
|
463
|
Chris@0
|
464 $build['top']['actions']['duplicate_as'][$type] = [
|
Chris@0
|
465 '#type' => 'submit',
|
Chris@0
|
466 '#value' => $this->t('Duplicate as @type', ['@type' => $label]),
|
Chris@0
|
467 '#limit_validation_errors' => [],
|
Chris@0
|
468 '#submit' => ['::submitDuplicateDisplayAsType', '::submitDelayDestination'],
|
Chris@0
|
469 '#prefix' => '<li class="duplicate">',
|
Chris@0
|
470 '#suffix' => '</li>',
|
Chris@0
|
471 ];
|
Chris@0
|
472 }
|
Chris@0
|
473 }
|
Chris@0
|
474 else {
|
Chris@0
|
475 $build['top']['actions']['undo_delete'] = [
|
Chris@0
|
476 '#type' => 'submit',
|
Chris@0
|
477 '#value' => $this->t('Undo delete of @display_title', ['@display_title' => $display_title]),
|
Chris@0
|
478 '#limit_validation_errors' => [],
|
Chris@0
|
479 '#submit' => ['::submitDisplayUndoDelete', '::submitDelayDestination'],
|
Chris@0
|
480 '#prefix' => '<li class="undo-delete">',
|
Chris@0
|
481 "#suffix" => '</li>',
|
Chris@0
|
482 ];
|
Chris@0
|
483 }
|
Chris@0
|
484 if ($is_enabled) {
|
Chris@0
|
485 $build['top']['actions']['disable'] = [
|
Chris@0
|
486 '#type' => 'submit',
|
Chris@0
|
487 '#value' => $this->t('Disable @display_title', ['@display_title' => $display_title]),
|
Chris@0
|
488 '#limit_validation_errors' => [],
|
Chris@0
|
489 '#submit' => ['::submitDisplayDisable', '::submitDelayDestination'],
|
Chris@0
|
490 '#prefix' => '<li class="disable">',
|
Chris@0
|
491 "#suffix" => '</li>',
|
Chris@0
|
492 ];
|
Chris@0
|
493 }
|
Chris@0
|
494 $build['top']['actions']['suffix']['#markup'] = '</ul>';
|
Chris@0
|
495
|
Chris@0
|
496 // The area above the three columns.
|
Chris@0
|
497 $build['top']['display_title'] = [
|
Chris@0
|
498 '#theme' => 'views_ui_display_tab_setting',
|
Chris@0
|
499 '#description' => $this->t('Display name'),
|
Chris@0
|
500 '#link' => $view->getExecutable()->displayHandlers->get($display['id'])->optionLink($display_title, 'display_title'),
|
Chris@0
|
501 ];
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 $build['columns'] = [];
|
Chris@0
|
505 $build['columns']['#theme_wrappers'] = ['container'];
|
Chris@0
|
506 $build['columns']['#attributes'] = ['id' => 'edit-display-settings-main', 'class' => ['clearfix', 'views-display-columns']];
|
Chris@0
|
507
|
Chris@0
|
508 $build['columns']['first']['#theme_wrappers'] = ['container'];
|
Chris@0
|
509 $build['columns']['first']['#attributes'] = ['class' => ['views-display-column', 'first']];
|
Chris@0
|
510
|
Chris@0
|
511 $build['columns']['second']['#theme_wrappers'] = ['container'];
|
Chris@0
|
512 $build['columns']['second']['#attributes'] = ['class' => ['views-display-column', 'second']];
|
Chris@0
|
513
|
Chris@0
|
514 $build['columns']['second']['settings'] = [];
|
Chris@0
|
515 $build['columns']['second']['header'] = [];
|
Chris@0
|
516 $build['columns']['second']['footer'] = [];
|
Chris@0
|
517 $build['columns']['second']['empty'] = [];
|
Chris@0
|
518 $build['columns']['second']['pager'] = [];
|
Chris@0
|
519
|
Chris@0
|
520 // The third column buckets are wrapped in details.
|
Chris@0
|
521 $build['columns']['third'] = [
|
Chris@0
|
522 '#type' => 'details',
|
Chris@0
|
523 '#title' => $this->t('Advanced'),
|
Chris@0
|
524 '#theme_wrappers' => ['details'],
|
Chris@0
|
525 '#attributes' => [
|
Chris@0
|
526 'class' => [
|
Chris@0
|
527 'views-display-column',
|
Chris@0
|
528 'third',
|
Chris@0
|
529 ],
|
Chris@0
|
530 ],
|
Chris@0
|
531 ];
|
Chris@0
|
532 // Collapse the details by default.
|
Chris@0
|
533 $build['columns']['third']['#open'] = \Drupal::config('views.settings')->get('ui.show.advanced_column');
|
Chris@0
|
534
|
Chris@0
|
535 // Each option (e.g. title, access, display as grid/table/list) fits into one
|
Chris@0
|
536 // of several "buckets," or boxes (Format, Fields, Sort, and so on).
|
Chris@0
|
537 $buckets = [];
|
Chris@0
|
538
|
Chris@0
|
539 // Fetch options from the display plugin, with a list of buckets they go into.
|
Chris@0
|
540 $options = [];
|
Chris@0
|
541 $view->getExecutable()->displayHandlers->get($display['id'])->optionsSummary($buckets, $options);
|
Chris@0
|
542
|
Chris@0
|
543 // Place each option into its bucket.
|
Chris@0
|
544 foreach ($options as $id => $option) {
|
Chris@0
|
545 // Each option self-identifies as belonging in a particular bucket.
|
Chris@0
|
546 $buckets[$option['category']]['build'][$id] = $this->buildOptionForm($view, $id, $option, $display);
|
Chris@0
|
547 }
|
Chris@0
|
548
|
Chris@0
|
549 // Place each bucket into the proper column.
|
Chris@0
|
550 foreach ($buckets as $id => $bucket) {
|
Chris@0
|
551 // Let buckets identify themselves as belonging in a column.
|
Chris@0
|
552 if (isset($bucket['column']) && isset($build['columns'][$bucket['column']])) {
|
Chris@0
|
553 $column = $bucket['column'];
|
Chris@0
|
554 }
|
Chris@0
|
555 // If a bucket doesn't pick one of our predefined columns to belong to, put
|
Chris@0
|
556 // it in the last one.
|
Chris@0
|
557 else {
|
Chris@0
|
558 $column = 'third';
|
Chris@0
|
559 }
|
Chris@0
|
560 if (isset($bucket['build']) && is_array($bucket['build'])) {
|
Chris@0
|
561 $build['columns'][$column][$id] = $bucket['build'];
|
Chris@0
|
562 $build['columns'][$column][$id]['#theme_wrappers'][] = 'views_ui_display_tab_bucket';
|
Chris@0
|
563 $build['columns'][$column][$id]['#title'] = !empty($bucket['title']) ? $bucket['title'] : '';
|
Chris@0
|
564 $build['columns'][$column][$id]['#name'] = $id;
|
Chris@0
|
565 }
|
Chris@0
|
566 }
|
Chris@0
|
567
|
Chris@0
|
568 $build['columns']['first']['fields'] = $this->getFormBucket($view, 'field', $display);
|
Chris@0
|
569 $build['columns']['first']['filters'] = $this->getFormBucket($view, 'filter', $display);
|
Chris@0
|
570 $build['columns']['first']['sorts'] = $this->getFormBucket($view, 'sort', $display);
|
Chris@0
|
571 $build['columns']['second']['header'] = $this->getFormBucket($view, 'header', $display);
|
Chris@0
|
572 $build['columns']['second']['footer'] = $this->getFormBucket($view, 'footer', $display);
|
Chris@0
|
573 $build['columns']['second']['empty'] = $this->getFormBucket($view, 'empty', $display);
|
Chris@0
|
574 $build['columns']['third']['arguments'] = $this->getFormBucket($view, 'argument', $display);
|
Chris@0
|
575 $build['columns']['third']['relationships'] = $this->getFormBucket($view, 'relationship', $display);
|
Chris@0
|
576
|
Chris@0
|
577 return $build;
|
Chris@0
|
578 }
|
Chris@0
|
579
|
Chris@0
|
580 /**
|
Chris@0
|
581 * Submit handler to add a restore a removed display to a view.
|
Chris@0
|
582 */
|
Chris@0
|
583 public function submitDisplayUndoDelete($form, FormStateInterface $form_state) {
|
Chris@0
|
584 $view = $this->entity;
|
Chris@0
|
585 // Create the new display
|
Chris@0
|
586 $id = $form_state->get('display_id');
|
Chris@0
|
587 $displays = $view->get('display');
|
Chris@0
|
588 $displays[$id]['deleted'] = FALSE;
|
Chris@0
|
589 $view->set('display', $displays);
|
Chris@0
|
590
|
Chris@0
|
591 // Store in cache
|
Chris@0
|
592 $view->cacheSet();
|
Chris@0
|
593
|
Chris@0
|
594 // Redirect to the top-level edit page.
|
Chris@0
|
595 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
596 'view' => $view->id(),
|
Chris@0
|
597 'display_id' => $id,
|
Chris@0
|
598 ]);
|
Chris@0
|
599 }
|
Chris@0
|
600
|
Chris@0
|
601 /**
|
Chris@0
|
602 * Submit handler to enable a disabled display.
|
Chris@0
|
603 */
|
Chris@0
|
604 public function submitDisplayEnable($form, FormStateInterface $form_state) {
|
Chris@0
|
605 $view = $this->entity;
|
Chris@0
|
606 $id = $form_state->get('display_id');
|
Chris@0
|
607 // setOption doesn't work because this would might affect upper displays
|
Chris@0
|
608 $view->getExecutable()->displayHandlers->get($id)->setOption('enabled', TRUE);
|
Chris@0
|
609
|
Chris@0
|
610 // Store in cache
|
Chris@0
|
611 $view->cacheSet();
|
Chris@0
|
612
|
Chris@0
|
613 // Redirect to the top-level edit page.
|
Chris@0
|
614 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
615 'view' => $view->id(),
|
Chris@0
|
616 'display_id' => $id,
|
Chris@0
|
617 ]);
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 /**
|
Chris@0
|
621 * Submit handler to disable display.
|
Chris@0
|
622 */
|
Chris@0
|
623 public function submitDisplayDisable($form, FormStateInterface $form_state) {
|
Chris@0
|
624 $view = $this->entity;
|
Chris@0
|
625 $id = $form_state->get('display_id');
|
Chris@0
|
626 $view->getExecutable()->displayHandlers->get($id)->setOption('enabled', FALSE);
|
Chris@0
|
627
|
Chris@0
|
628 // Store in cache
|
Chris@0
|
629 $view->cacheSet();
|
Chris@0
|
630
|
Chris@0
|
631 // Redirect to the top-level edit page.
|
Chris@0
|
632 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
633 'view' => $view->id(),
|
Chris@0
|
634 'display_id' => $id,
|
Chris@0
|
635 ]);
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 /**
|
Chris@0
|
639 * Submit handler to delete a display from a view.
|
Chris@0
|
640 */
|
Chris@0
|
641 public function submitDisplayDelete($form, FormStateInterface $form_state) {
|
Chris@0
|
642 $view = $this->entity;
|
Chris@0
|
643 $display_id = $form_state->get('display_id');
|
Chris@0
|
644
|
Chris@0
|
645 // Mark the display for deletion.
|
Chris@0
|
646 $displays = $view->get('display');
|
Chris@0
|
647 $displays[$display_id]['deleted'] = TRUE;
|
Chris@0
|
648 $view->set('display', $displays);
|
Chris@0
|
649 $view->cacheSet();
|
Chris@0
|
650
|
Chris@0
|
651 // Redirect to the top-level edit page. The first remaining display will
|
Chris@0
|
652 // become the active display.
|
Chris@0
|
653 $form_state->setRedirectUrl($view->urlInfo('edit-form'));
|
Chris@0
|
654 }
|
Chris@0
|
655
|
Chris@0
|
656 /**
|
Chris@0
|
657 * Regenerate the current tab for AJAX updates.
|
Chris@0
|
658 *
|
Chris@0
|
659 * @param \Drupal\views_ui\ViewUI $view
|
Chris@0
|
660 * The view to regenerate its tab.
|
Chris@0
|
661 * @param \Drupal\Core\Ajax\AjaxResponse $response
|
Chris@0
|
662 * The response object to add new commands to.
|
Chris@0
|
663 * @param string $display_id
|
Chris@0
|
664 * The display ID of the tab to regenerate.
|
Chris@0
|
665 */
|
Chris@0
|
666 public function rebuildCurrentTab(ViewUI $view, AjaxResponse $response, $display_id) {
|
Chris@0
|
667 $this->displayID = $display_id;
|
Chris@0
|
668 if (!$view->getExecutable()->setDisplay('default')) {
|
Chris@0
|
669 return;
|
Chris@0
|
670 }
|
Chris@0
|
671
|
Chris@0
|
672 // Regenerate the main display area.
|
Chris@0
|
673 $build = $this->getDisplayTab($view);
|
Chris@0
|
674 $response->addCommand(new HtmlCommand('#views-tab-' . $display_id, $build));
|
Chris@0
|
675
|
Chris@0
|
676 // Regenerate the top area so changes to display names and order will appear.
|
Chris@0
|
677 $build = $this->renderDisplayTop($view);
|
Chris@0
|
678 $response->addCommand(new ReplaceCommand('#views-display-top', $build));
|
Chris@0
|
679 }
|
Chris@0
|
680
|
Chris@0
|
681 /**
|
Chris@0
|
682 * Render the top of the display so it can be updated during ajax operations.
|
Chris@0
|
683 */
|
Chris@0
|
684 public function renderDisplayTop(ViewUI $view) {
|
Chris@0
|
685 $display_id = $this->displayID;
|
Chris@0
|
686 $element['#theme_wrappers'][] = 'views_ui_container';
|
Chris@0
|
687 $element['#attributes']['class'] = ['views-display-top', 'clearfix'];
|
Chris@0
|
688 $element['#attributes']['id'] = ['views-display-top'];
|
Chris@0
|
689
|
Chris@0
|
690 // Extra actions for the display
|
Chris@0
|
691 $element['extra_actions'] = [
|
Chris@0
|
692 '#type' => 'dropbutton',
|
Chris@0
|
693 '#attributes' => [
|
Chris@0
|
694 'id' => 'views-display-extra-actions',
|
Chris@0
|
695 ],
|
Chris@0
|
696 '#links' => [
|
Chris@0
|
697 'edit-details' => [
|
Chris@0
|
698 'title' => $this->t('Edit view name/description'),
|
Chris@0
|
699 'url' => Url::fromRoute('views_ui.form_edit_details', ['js' => 'nojs', 'view' => $view->id(), 'display_id' => $display_id]),
|
Chris@0
|
700 'attributes' => ['class' => ['views-ajax-link']],
|
Chris@0
|
701 ],
|
Chris@0
|
702 'analyze' => [
|
Chris@0
|
703 'title' => $this->t('Analyze view'),
|
Chris@0
|
704 'url' => Url::fromRoute('views_ui.form_analyze', ['js' => 'nojs', 'view' => $view->id(), 'display_id' => $display_id]),
|
Chris@0
|
705 'attributes' => ['class' => ['views-ajax-link']],
|
Chris@0
|
706 ],
|
Chris@0
|
707 'duplicate' => [
|
Chris@0
|
708 'title' => $this->t('Duplicate view'),
|
Chris@0
|
709 'url' => $view->urlInfo('duplicate-form'),
|
Chris@0
|
710 ],
|
Chris@0
|
711 'reorder' => [
|
Chris@0
|
712 'title' => $this->t('Reorder displays'),
|
Chris@0
|
713 'url' => Url::fromRoute('views_ui.form_reorder_displays', ['js' => 'nojs', 'view' => $view->id(), 'display_id' => $display_id]),
|
Chris@0
|
714 'attributes' => ['class' => ['views-ajax-link']],
|
Chris@0
|
715 ],
|
Chris@0
|
716 ],
|
Chris@0
|
717 ];
|
Chris@0
|
718
|
Chris@0
|
719 if ($view->access('delete')) {
|
Chris@0
|
720 $element['extra_actions']['#links']['delete'] = [
|
Chris@0
|
721 'title' => $this->t('Delete view'),
|
Chris@0
|
722 'url' => $view->urlInfo('delete-form'),
|
Chris@0
|
723 ];
|
Chris@0
|
724 }
|
Chris@0
|
725
|
Chris@0
|
726 // Let other modules add additional links here.
|
Chris@0
|
727 \Drupal::moduleHandler()->alter('views_ui_display_top_links', $element['extra_actions']['#links'], $view, $display_id);
|
Chris@0
|
728
|
Chris@0
|
729 if (isset($view->type) && $view->type != $this->t('Default')) {
|
Chris@0
|
730 if ($view->type == $this->t('Overridden')) {
|
Chris@0
|
731 $element['extra_actions']['#links']['revert'] = [
|
Chris@0
|
732 'title' => $this->t('Revert view'),
|
Chris@0
|
733 'href' => "admin/structure/views/view/{$view->id()}/revert",
|
Chris@0
|
734 'query' => ['destination' => $view->url('edit-form')],
|
Chris@0
|
735 ];
|
Chris@0
|
736 }
|
Chris@0
|
737 else {
|
Chris@0
|
738 $element['extra_actions']['#links']['delete'] = [
|
Chris@0
|
739 'title' => $this->t('Delete view'),
|
Chris@0
|
740 'url' => $view->urlInfo('delete-form'),
|
Chris@0
|
741 ];
|
Chris@0
|
742 }
|
Chris@0
|
743 }
|
Chris@0
|
744
|
Chris@0
|
745 // Determine the displays available for editing.
|
Chris@0
|
746 if ($tabs = $this->getDisplayTabs($view)) {
|
Chris@0
|
747 if ($display_id) {
|
Chris@0
|
748 $tabs[$display_id]['#active'] = TRUE;
|
Chris@0
|
749 }
|
Chris@0
|
750 $tabs['#prefix'] = '<h2 class="visually-hidden">' . $this->t('Secondary tabs') . '</h2><ul id = "views-display-menu-tabs" class="tabs secondary">';
|
Chris@0
|
751 $tabs['#suffix'] = '</ul>';
|
Chris@0
|
752 $element['tabs'] = $tabs;
|
Chris@0
|
753 }
|
Chris@0
|
754
|
Chris@0
|
755 // Buttons for adding a new display.
|
Chris@0
|
756 foreach (Views::fetchPluginNames('display', NULL, [$view->get('base_table')]) as $type => $label) {
|
Chris@0
|
757 $element['add_display'][$type] = [
|
Chris@0
|
758 '#type' => 'submit',
|
Chris@0
|
759 '#value' => $this->t('Add @display', ['@display' => $label]),
|
Chris@0
|
760 '#limit_validation_errors' => [],
|
Chris@0
|
761 '#submit' => ['::submitDisplayAdd', '::submitDelayDestination'],
|
Chris@0
|
762 '#attributes' => ['class' => ['add-display']],
|
Chris@0
|
763 // Allow JavaScript to remove the 'Add ' prefix from the button label when
|
Chris@0
|
764 // placing the button in a "Add" dropdown menu.
|
Chris@0
|
765 '#process' => array_merge(['views_ui_form_button_was_clicked'], $this->elementInfo->getInfoProperty('submit', '#process', [])),
|
Chris@0
|
766 '#values' => [$this->t('Add @display', ['@display' => $label]), $label],
|
Chris@0
|
767 ];
|
Chris@0
|
768 }
|
Chris@0
|
769
|
Chris@0
|
770 return $element;
|
Chris@0
|
771 }
|
Chris@0
|
772
|
Chris@0
|
773 /**
|
Chris@0
|
774 * Submit handler for form buttons that do not complete a form workflow.
|
Chris@0
|
775 *
|
Chris@0
|
776 * The Edit View form is a multistep form workflow, but with state managed by
|
Chris@0
|
777 * the SharedTempStore rather than $form_state->setRebuild(). Without this
|
Chris@0
|
778 * submit handler, buttons that add or remove displays would redirect to the
|
Chris@0
|
779 * destination parameter (e.g., when the Edit View form is linked to from a
|
Chris@0
|
780 * contextual link). This handler can be added to buttons whose form submission
|
Chris@0
|
781 * should not yet redirect to the destination.
|
Chris@0
|
782 */
|
Chris@0
|
783 public function submitDelayDestination($form, FormStateInterface $form_state) {
|
Chris@0
|
784 $request = $this->requestStack->getCurrentRequest();
|
Chris@0
|
785 $destination = $request->query->get('destination');
|
Chris@0
|
786
|
Chris@0
|
787 $redirect = $form_state->getRedirect();
|
Chris@0
|
788 // If there is a destination, and redirects are not explicitly disabled, add
|
Chris@0
|
789 // the destination as a query string to the redirect and suppress it for the
|
Chris@0
|
790 // current request.
|
Chris@0
|
791 if (isset($destination) && $redirect !== FALSE) {
|
Chris@0
|
792 // Create a valid redirect if one does not exist already.
|
Chris@0
|
793 if (!($redirect instanceof Url)) {
|
Chris@0
|
794 $redirect = Url::createFromRequest($request);
|
Chris@0
|
795 }
|
Chris@0
|
796
|
Chris@0
|
797 // Add the current destination to the redirect unless one exists already.
|
Chris@0
|
798 $options = $redirect->getOptions();
|
Chris@0
|
799 if (!isset($options['query']['destination'])) {
|
Chris@0
|
800 $options['query']['destination'] = $destination;
|
Chris@0
|
801 $redirect->setOptions($options);
|
Chris@0
|
802 }
|
Chris@0
|
803
|
Chris@0
|
804 $form_state->setRedirectUrl($redirect);
|
Chris@0
|
805 $request->query->remove('destination');
|
Chris@0
|
806 }
|
Chris@0
|
807 }
|
Chris@0
|
808
|
Chris@0
|
809 /**
|
Chris@0
|
810 * Submit handler to duplicate a display for a view.
|
Chris@0
|
811 */
|
Chris@0
|
812 public function submitDisplayDuplicate($form, FormStateInterface $form_state) {
|
Chris@0
|
813 $view = $this->entity;
|
Chris@0
|
814 $display_id = $this->displayID;
|
Chris@0
|
815
|
Chris@0
|
816 // Create the new display.
|
Chris@0
|
817 $displays = $view->get('display');
|
Chris@0
|
818 $display = $view->getExecutable()->newDisplay($displays[$display_id]['display_plugin']);
|
Chris@0
|
819 $new_display_id = $display->display['id'];
|
Chris@0
|
820 $displays[$new_display_id] = $displays[$display_id];
|
Chris@0
|
821 $displays[$new_display_id]['id'] = $new_display_id;
|
Chris@0
|
822 $view->set('display', $displays);
|
Chris@0
|
823
|
Chris@0
|
824 // By setting the current display the changed marker will appear on the new
|
Chris@0
|
825 // display.
|
Chris@0
|
826 $view->getExecutable()->current_display = $new_display_id;
|
Chris@0
|
827 $view->cacheSet();
|
Chris@0
|
828
|
Chris@0
|
829 // Redirect to the new display's edit page.
|
Chris@0
|
830 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
831 'view' => $view->id(),
|
Chris@0
|
832 'display_id' => $new_display_id,
|
Chris@0
|
833 ]);
|
Chris@0
|
834 }
|
Chris@0
|
835
|
Chris@0
|
836 /**
|
Chris@0
|
837 * Submit handler to add a display to a view.
|
Chris@0
|
838 */
|
Chris@0
|
839 public function submitDisplayAdd($form, FormStateInterface $form_state) {
|
Chris@0
|
840 $view = $this->entity;
|
Chris@0
|
841 // Create the new display.
|
Chris@0
|
842 $parents = $form_state->getTriggeringElement()['#parents'];
|
Chris@0
|
843 $display_type = array_pop($parents);
|
Chris@0
|
844 $display = $view->getExecutable()->newDisplay($display_type);
|
Chris@0
|
845 $display_id = $display->display['id'];
|
Chris@0
|
846 // A new display got added so the asterisks symbol should appear on the new
|
Chris@0
|
847 // display.
|
Chris@0
|
848 $view->getExecutable()->current_display = $display_id;
|
Chris@0
|
849 $view->cacheSet();
|
Chris@0
|
850
|
Chris@0
|
851 // Redirect to the new display's edit page.
|
Chris@0
|
852 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
853 'view' => $view->id(),
|
Chris@0
|
854 'display_id' => $display_id,
|
Chris@0
|
855 ]);
|
Chris@0
|
856 }
|
Chris@0
|
857
|
Chris@0
|
858 /**
|
Chris@0
|
859 * Submit handler to Duplicate a display as another display type.
|
Chris@0
|
860 */
|
Chris@0
|
861 public function submitDuplicateDisplayAsType($form, FormStateInterface $form_state) {
|
Chris@0
|
862 /** @var \Drupal\views\ViewEntityInterface $view */
|
Chris@0
|
863 $view = $this->entity;
|
Chris@0
|
864 $display_id = $this->displayID;
|
Chris@0
|
865
|
Chris@0
|
866 // Create the new display.
|
Chris@0
|
867 $parents = $form_state->getTriggeringElement()['#parents'];
|
Chris@0
|
868 $display_type = array_pop($parents);
|
Chris@0
|
869
|
Chris@0
|
870 $new_display_id = $view->duplicateDisplayAsType($display_id, $display_type);
|
Chris@0
|
871
|
Chris@0
|
872 // By setting the current display the changed marker will appear on the new
|
Chris@0
|
873 // display.
|
Chris@0
|
874 $view->getExecutable()->current_display = $new_display_id;
|
Chris@0
|
875 $view->cacheSet();
|
Chris@0
|
876
|
Chris@0
|
877 // Redirect to the new display's edit page.
|
Chris@0
|
878 $form_state->setRedirect('entity.view.edit_display_form', [
|
Chris@0
|
879 'view' => $view->id(),
|
Chris@0
|
880 'display_id' => $new_display_id,
|
Chris@0
|
881 ]);
|
Chris@0
|
882 }
|
Chris@0
|
883
|
Chris@0
|
884 /**
|
Chris@0
|
885 * Build a renderable array representing one option on the edit form.
|
Chris@0
|
886 *
|
Chris@0
|
887 * This function might be more logical as a method on an object, if a suitable
|
Chris@0
|
888 * object emerges out of refactoring.
|
Chris@0
|
889 */
|
Chris@0
|
890 public function buildOptionForm(ViewUI $view, $id, $option, $display) {
|
Chris@0
|
891 $option_build = [];
|
Chris@0
|
892 $option_build['#theme'] = 'views_ui_display_tab_setting';
|
Chris@0
|
893
|
Chris@0
|
894 $option_build['#description'] = $option['title'];
|
Chris@0
|
895
|
Chris@0
|
896 $option_build['#link'] = $view->getExecutable()->displayHandlers->get($display['id'])->optionLink($option['value'], $id, '', empty($option['desc']) ? '' : $option['desc']);
|
Chris@0
|
897
|
Chris@0
|
898 $option_build['#links'] = [];
|
Chris@0
|
899 if (!empty($option['links']) && is_array($option['links'])) {
|
Chris@0
|
900 foreach ($option['links'] as $link_id => $link_value) {
|
Chris@0
|
901 $option_build['#settings_links'][] = $view->getExecutable()->displayHandlers->get($display['id'])->optionLink($option['setting'], $link_id, 'views-button-configure', $link_value);
|
Chris@0
|
902 }
|
Chris@0
|
903 }
|
Chris@0
|
904
|
Chris@0
|
905 if (!empty($view->getExecutable()->displayHandlers->get($display['id'])->options['defaults'][$id])) {
|
Chris@0
|
906 $display_id = 'default';
|
Chris@0
|
907 $option_build['#defaulted'] = TRUE;
|
Chris@0
|
908 }
|
Chris@0
|
909 else {
|
Chris@0
|
910 $display_id = $display['id'];
|
Chris@0
|
911 if (!$view->getExecutable()->displayHandlers->get($display['id'])->isDefaultDisplay()) {
|
Chris@0
|
912 if ($view->getExecutable()->displayHandlers->get($display['id'])->defaultableSections($id)) {
|
Chris@0
|
913 $option_build['#overridden'] = TRUE;
|
Chris@0
|
914 }
|
Chris@0
|
915 }
|
Chris@0
|
916 }
|
Chris@0
|
917 $option_build['#attributes']['class'][] = Html::cleanCssIdentifier($display_id . '-' . $id);
|
Chris@0
|
918 return $option_build;
|
Chris@0
|
919 }
|
Chris@0
|
920
|
Chris@0
|
921 /**
|
Chris@0
|
922 * Add information about a section to a display.
|
Chris@0
|
923 */
|
Chris@0
|
924 public function getFormBucket(ViewUI $view, $type, $display) {
|
Chris@0
|
925 $executable = $view->getExecutable();
|
Chris@0
|
926 $executable->setDisplay($display['id']);
|
Chris@0
|
927 $executable->initStyle();
|
Chris@0
|
928
|
Chris@0
|
929 $types = $executable->getHandlerTypes();
|
Chris@0
|
930
|
Chris@0
|
931 $build = [
|
Chris@0
|
932 '#theme_wrappers' => ['views_ui_display_tab_bucket'],
|
Chris@0
|
933 ];
|
Chris@0
|
934
|
Chris@0
|
935 $build['#overridden'] = FALSE;
|
Chris@0
|
936 $build['#defaulted'] = FALSE;
|
Chris@0
|
937
|
Chris@0
|
938 $build['#name'] = $type;
|
Chris@0
|
939 $build['#title'] = $types[$type]['title'];
|
Chris@0
|
940
|
Chris@0
|
941 $rearrange_url = Url::fromRoute('views_ui.form_rearrange', ['js' => 'nojs', 'view' => $view->id(), 'display_id' => $display['id'], 'type' => $type]);
|
Chris@0
|
942 $class = 'icon compact rearrange';
|
Chris@0
|
943
|
Chris@0
|
944 // Different types now have different rearrange forms, so we use this switch
|
Chris@0
|
945 // to get the right one.
|
Chris@0
|
946 switch ($type) {
|
Chris@0
|
947 case 'filter':
|
Chris@0
|
948 // The rearrange form for filters contains the and/or UI, so override
|
Chris@0
|
949 // the used path.
|
Chris@0
|
950 $rearrange_url = Url::fromRoute('views_ui.form_rearrange_filter', ['js' => 'nojs', 'view' => $view->id(), 'display_id' => $display['id']]);
|
Chris@0
|
951 // TODO: Add another class to have another symbol for filter rearrange.
|
Chris@0
|
952 $class = 'icon compact rearrange';
|
Chris@0
|
953 break;
|
Chris@0
|
954 case 'field':
|
Chris@0
|
955 // Fetch the style plugin info so we know whether to list fields or not.
|
Chris@0
|
956 $style_plugin = $executable->style_plugin;
|
Chris@0
|
957 $uses_fields = $style_plugin && $style_plugin->usesFields();
|
Chris@0
|
958 if (!$uses_fields) {
|
Chris@0
|
959 $build['fields'][] = [
|
Chris@0
|
960 '#markup' => $this->t('The selected style or row format does not use fields.'),
|
Chris@0
|
961 '#theme_wrappers' => ['views_ui_container'],
|
Chris@0
|
962 '#attributes' => ['class' => ['views-display-setting']],
|
Chris@0
|
963 ];
|
Chris@0
|
964 return $build;
|
Chris@0
|
965 }
|
Chris@0
|
966 break;
|
Chris@0
|
967 case 'header':
|
Chris@0
|
968 case 'footer':
|
Chris@0
|
969 case 'empty':
|
Chris@0
|
970 if (!$executable->display_handler->usesAreas()) {
|
Chris@0
|
971 $build[$type][] = [
|
Chris@0
|
972 '#markup' => $this->t('The selected display type does not use @type plugins', ['@type' => $type]),
|
Chris@0
|
973 '#theme_wrappers' => ['views_ui_container'],
|
Chris@0
|
974 '#attributes' => ['class' => ['views-display-setting']],
|
Chris@0
|
975 ];
|
Chris@0
|
976 return $build;
|
Chris@0
|
977 }
|
Chris@0
|
978 break;
|
Chris@0
|
979 }
|
Chris@0
|
980
|
Chris@0
|
981 // Create an array of actions to pass to links template.
|
Chris@0
|
982 $actions = [];
|
Chris@0
|
983 $count_handlers = count($executable->display_handler->getHandlers($type));
|
Chris@0
|
984
|
Chris@0
|
985 // Create the add text variable for the add action.
|
Chris@0
|
986 $add_text = $this->t('Add <span class="visually-hidden">@type</span>', ['@type' => $types[$type]['ltitle']]);
|
Chris@0
|
987
|
Chris@0
|
988 $actions['add'] = [
|
Chris@0
|
989 'title' => $add_text,
|
Chris@0
|
990 'url' => Url::fromRoute('views_ui.form_add_handler', ['js' => 'nojs', 'view' => $view->id(), 'display_id' => $display['id'], 'type' => $type]),
|
Chris@0
|
991 'attributes' => ['class' => ['icon compact add', 'views-ajax-link'], 'id' => 'views-add-' . $type],
|
Chris@0
|
992 ];
|
Chris@0
|
993 if ($count_handlers > 0) {
|
Chris@0
|
994 // Create the rearrange text variable for the rearrange action.
|
Chris@0
|
995 $rearrange_text = $type == 'filter' ? $this->t('And/Or Rearrange <span class="visually-hidden">filter criteria</span>') : $this->t('Rearrange <span class="visually-hidden">@type</span>', ['@type' => $types[$type]['ltitle']]);
|
Chris@0
|
996
|
Chris@0
|
997 $actions['rearrange'] = [
|
Chris@0
|
998 'title' => $rearrange_text,
|
Chris@0
|
999 'url' => $rearrange_url,
|
Chris@0
|
1000 'attributes' => ['class' => [$class, 'views-ajax-link'], 'id' => 'views-rearrange-' . $type],
|
Chris@0
|
1001 ];
|
Chris@0
|
1002 }
|
Chris@0
|
1003
|
Chris@0
|
1004 // Render the array of links
|
Chris@0
|
1005 $build['#actions'] = [
|
Chris@0
|
1006 '#type' => 'dropbutton',
|
Chris@0
|
1007 '#links' => $actions,
|
Chris@0
|
1008 '#attributes' => [
|
Chris@0
|
1009 'class' => ['views-ui-settings-bucket-operations'],
|
Chris@0
|
1010 ],
|
Chris@0
|
1011 ];
|
Chris@0
|
1012
|
Chris@0
|
1013 if (!$executable->display_handler->isDefaultDisplay()) {
|
Chris@0
|
1014 if (!$executable->display_handler->isDefaulted($types[$type]['plural'])) {
|
Chris@0
|
1015 $build['#overridden'] = TRUE;
|
Chris@0
|
1016 }
|
Chris@0
|
1017 else {
|
Chris@0
|
1018 $build['#defaulted'] = TRUE;
|
Chris@0
|
1019 }
|
Chris@0
|
1020 }
|
Chris@0
|
1021
|
Chris@0
|
1022 static $relationships = NULL;
|
Chris@0
|
1023 if (!isset($relationships)) {
|
Chris@0
|
1024 // Get relationship labels.
|
Chris@0
|
1025 $relationships = [];
|
Chris@0
|
1026 foreach ($executable->display_handler->getHandlers('relationship') as $id => $handler) {
|
Chris@0
|
1027 $relationships[$id] = $handler->adminLabel();
|
Chris@0
|
1028 }
|
Chris@0
|
1029 }
|
Chris@0
|
1030
|
Chris@0
|
1031 // Filters can now be grouped so we do a little bit extra:
|
Chris@0
|
1032 $groups = [];
|
Chris@0
|
1033 $grouping = FALSE;
|
Chris@0
|
1034 if ($type == 'filter') {
|
Chris@0
|
1035 $group_info = $executable->display_handler->getOption('filter_groups');
|
Chris@0
|
1036 // If there is only one group but it is using the "OR" filter, we still
|
Chris@0
|
1037 // treat it as a group for display purposes, since we want to display the
|
Chris@0
|
1038 // "OR" label next to items within the group.
|
Chris@0
|
1039 if (!empty($group_info['groups']) && (count($group_info['groups']) > 1 || current($group_info['groups']) == 'OR')) {
|
Chris@0
|
1040 $grouping = TRUE;
|
Chris@0
|
1041 $groups = [0 => []];
|
Chris@0
|
1042 }
|
Chris@0
|
1043 }
|
Chris@0
|
1044
|
Chris@0
|
1045 $build['fields'] = [];
|
Chris@0
|
1046
|
Chris@0
|
1047 foreach ($executable->display_handler->getOption($types[$type]['plural']) as $id => $field) {
|
Chris@0
|
1048 // Build the option link for this handler ("Node: ID = article").
|
Chris@0
|
1049 $build['fields'][$id] = [];
|
Chris@0
|
1050 $build['fields'][$id]['#theme'] = 'views_ui_display_tab_setting';
|
Chris@0
|
1051
|
Chris@0
|
1052 $handler = $executable->display_handler->getHandler($type, $id);
|
Chris@0
|
1053 if ($handler->broken()) {
|
Chris@0
|
1054 $build['fields'][$id]['#class'][] = 'broken';
|
Chris@0
|
1055 $field_name = $handler->adminLabel();
|
Chris@0
|
1056 $build['fields'][$id]['#link'] = $this->l($field_name, new Url('views_ui.form_handler', [
|
Chris@0
|
1057 'js' => 'nojs',
|
Chris@0
|
1058 'view' => $view->id(),
|
Chris@0
|
1059 'display_id' => $display['id'],
|
Chris@0
|
1060 'type' => $type,
|
Chris@0
|
1061 'id' => $id,
|
Chris@0
|
1062 ], ['attributes' => ['class' => ['views-ajax-link']]]));
|
Chris@0
|
1063 continue;
|
Chris@0
|
1064 }
|
Chris@0
|
1065
|
Chris@0
|
1066 $field_name = $handler->adminLabel(TRUE);
|
Chris@0
|
1067 if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
|
Chris@0
|
1068 $field_name = '(' . $relationships[$field['relationship']] . ') ' . $field_name;
|
Chris@0
|
1069 }
|
Chris@0
|
1070
|
Chris@0
|
1071 $description = $handler->adminSummary();
|
Chris@0
|
1072 $link_text = $field_name . (empty($description) ? '' : " ($description)");
|
Chris@0
|
1073 $link_attributes = ['class' => ['views-ajax-link']];
|
Chris@0
|
1074 if (!empty($field['exclude'])) {
|
Chris@0
|
1075 $link_attributes['class'][] = 'views-field-excluded';
|
Chris@0
|
1076 // Add a [hidden] marker, if the field is excluded.
|
Chris@0
|
1077 $link_text .= ' [' . $this->t('hidden') . ']';
|
Chris@0
|
1078 }
|
Chris@0
|
1079 $build['fields'][$id]['#link'] = $this->l($link_text, new Url('views_ui.form_handler', [
|
Chris@0
|
1080 'js' => 'nojs',
|
Chris@0
|
1081 'view' => $view->id(),
|
Chris@0
|
1082 'display_id' => $display['id'],
|
Chris@0
|
1083 'type' => $type,
|
Chris@0
|
1084 'id' => $id,
|
Chris@0
|
1085 ], ['attributes' => $link_attributes]));
|
Chris@0
|
1086 $build['fields'][$id]['#class'][] = Html::cleanCssIdentifier($display['id'] . '-' . $type . '-' . $id);
|
Chris@0
|
1087
|
Chris@0
|
1088 if ($executable->display_handler->useGroupBy() && $handler->usesGroupBy()) {
|
Chris@0
|
1089 $build['fields'][$id]['#settings_links'][] = $this->l(SafeMarkup::format('<span class="label">@text</span>', ['@text' => $this->t('Aggregation settings')]), new Url('views_ui.form_handler_group', [
|
Chris@0
|
1090 'js' => 'nojs',
|
Chris@0
|
1091 'view' => $view->id(),
|
Chris@0
|
1092 'display_id' => $display['id'],
|
Chris@0
|
1093 'type' => $type,
|
Chris@0
|
1094 'id' => $id,
|
Chris@0
|
1095 ], ['attributes' => ['class' => ['views-button-configure', 'views-ajax-link'], 'title' => $this->t('Aggregation settings')]]));
|
Chris@0
|
1096 }
|
Chris@0
|
1097
|
Chris@0
|
1098 if ($handler->hasExtraOptions()) {
|
Chris@0
|
1099 $build['fields'][$id]['#settings_links'][] = $this->l(SafeMarkup::format('<span class="label">@text</span>', ['@text' => $this->t('Settings')]), new Url('views_ui.form_handler_extra', [
|
Chris@0
|
1100 'js' => 'nojs',
|
Chris@0
|
1101 'view' => $view->id(),
|
Chris@0
|
1102 'display_id' => $display['id'],
|
Chris@0
|
1103 'type' => $type,
|
Chris@0
|
1104 'id' => $id,
|
Chris@0
|
1105 ], ['attributes' => ['class' => ['views-button-configure', 'views-ajax-link'], 'title' => $this->t('Settings')]]));
|
Chris@0
|
1106 }
|
Chris@0
|
1107
|
Chris@0
|
1108 if ($grouping) {
|
Chris@0
|
1109 $gid = $handler->options['group'];
|
Chris@0
|
1110
|
Chris@0
|
1111 // Show in default group if the group does not exist.
|
Chris@0
|
1112 if (empty($group_info['groups'][$gid])) {
|
Chris@0
|
1113 $gid = 0;
|
Chris@0
|
1114 }
|
Chris@0
|
1115 $groups[$gid][] = $id;
|
Chris@0
|
1116 }
|
Chris@0
|
1117 }
|
Chris@0
|
1118
|
Chris@0
|
1119 // If using grouping, re-order fields so that they show up properly in the list.
|
Chris@0
|
1120 if ($type == 'filter' && $grouping) {
|
Chris@0
|
1121 $store = $build['fields'];
|
Chris@0
|
1122 $build['fields'] = [];
|
Chris@0
|
1123 foreach ($groups as $gid => $contents) {
|
Chris@0
|
1124 // Display an operator between each group.
|
Chris@0
|
1125 if (!empty($build['fields'])) {
|
Chris@0
|
1126 $build['fields'][] = [
|
Chris@0
|
1127 '#theme' => 'views_ui_display_tab_setting',
|
Chris@0
|
1128 '#class' => ['views-group-text'],
|
Chris@0
|
1129 '#link' => ($group_info['operator'] == 'OR' ? $this->t('OR') : $this->t('AND')),
|
Chris@0
|
1130 ];
|
Chris@0
|
1131 }
|
Chris@0
|
1132 // Display an operator between each pair of filters within the group.
|
Chris@0
|
1133 $keys = array_keys($contents);
|
Chris@0
|
1134 $last = end($keys);
|
Chris@0
|
1135 foreach ($contents as $key => $pid) {
|
Chris@0
|
1136 if ($key != $last) {
|
Chris@0
|
1137 $operator = $group_info['groups'][$gid] == 'OR' ? $this->t('OR') : $this->t('AND');
|
Chris@0
|
1138 $store[$pid]['#link'] = SafeMarkup::format('@link <span>@operator</span>', ['@link' => $store[$pid]['#link'], '@operator' => $operator]);
|
Chris@0
|
1139 }
|
Chris@0
|
1140 $build['fields'][$pid] = $store[$pid];
|
Chris@0
|
1141 }
|
Chris@0
|
1142 }
|
Chris@0
|
1143 }
|
Chris@0
|
1144
|
Chris@0
|
1145 return $build;
|
Chris@0
|
1146 }
|
Chris@0
|
1147
|
Chris@0
|
1148 }
|