comparison core/modules/views_ui/src/ViewPreviewForm.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\views_ui;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Url;
7
8 /**
9 * Form controller for the Views preview form.
10 */
11 class ViewPreviewForm extends ViewFormBase {
12
13 /**
14 * {@inheritdoc}
15 */
16 public function form(array $form, FormStateInterface $form_state) {
17 $view = $this->entity;
18
19 $form['#prefix'] = '<div id="views-preview-wrapper" class="views-preview-wrapper views-admin clearfix">';
20 $form['#suffix'] = '</div>';
21 $form['#id'] = 'views-ui-preview-form';
22
23 $form_state->disableCache();
24
25 $form['controls']['#attributes'] = ['class' => ['clearfix']];
26
27 $form['controls']['title'] = [
28 '#prefix' => '<h2 class="view-preview-form__title">',
29 '#markup' => $this->t('Preview'),
30 '#suffix' => '</h2>',
31 ];
32
33 // Add a checkbox controlling whether or not this display auto-previews.
34 $form['controls']['live_preview'] = [
35 '#type' => 'checkbox',
36 '#id' => 'edit-displays-live-preview',
37 '#title' => $this->t('Auto preview'),
38 '#default_value' => \Drupal::config('views.settings')->get('ui.always_live_preview'),
39 ];
40
41 // Add the arguments textfield
42 $form['controls']['view_args'] = [
43 '#type' => 'textfield',
44 '#title' => $this->t('Preview with contextual filters:'),
45 '#description' => $this->t('Separate contextual filter values with a "/". For example, %example.', ['%example' => '40/12/10']),
46 '#id' => 'preview-args',
47 ];
48
49 $args = [];
50 if (!$form_state->isValueEmpty('view_args')) {
51 $args = explode('/', $form_state->getValue('view_args'));
52 }
53
54 $user_input = $form_state->getUserInput();
55 if ($form_state->get('show_preview') || !empty($user_input['js'])) {
56 $form['preview'] = [
57 '#weight' => 110,
58 '#theme_wrappers' => ['container'],
59 '#attributes' => ['id' => 'views-live-preview', 'class' => ['views-live-preview']],
60 'preview' => $view->renderPreview($this->displayID, $args),
61 ];
62 }
63 $uri = $view->urlInfo('preview-form');
64 $uri->setRouteParameter('display_id', $this->displayID);
65 $form['#action'] = $uri->toString();
66
67 return $form;
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 protected function actions(array $form, FormStateInterface $form_state) {
74 $view = $this->entity;
75 return [
76 '#attributes' => [
77 'id' => 'preview-submit-wrapper',
78 'class' => ['preview-submit-wrapper']
79 ],
80 'button' => [
81 '#type' => 'submit',
82 '#value' => $this->t('Update preview'),
83 '#attributes' => ['class' => ['arguments-preview']],
84 '#submit' => ['::submitPreview'],
85 '#id' => 'preview-submit',
86 '#ajax' => [
87 'url' => Url::fromRoute('entity.view.preview_form', ['view' => $view->id(), 'display_id' => $this->displayID]),
88 'wrapper' => 'views-preview-wrapper',
89 'event' => 'click',
90 'progress' => ['type' => 'fullscreen'],
91 'method' => 'replaceWith',
92 'disable-refocus' => TRUE,
93 ],
94 ],
95 ];
96 }
97
98 /**
99 * Form submission handler for the Preview button.
100 */
101 public function submitPreview($form, FormStateInterface $form_state) {
102 $form_state->set('show_preview', TRUE);
103 $form_state->setRebuild();
104 }
105
106 }