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

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