3 namespace Drupal\views_ui;
5 use Drupal\Core\Form\FormStateInterface;
9 * Form controller for the Views preview form.
11 class ViewPreviewForm extends ViewFormBase {
16 public function form(array $form, FormStateInterface $form_state) {
17 $view = $this->entity;
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';
23 $form_state->disableCache();
25 $form['controls']['#attributes'] = ['class' => ['clearfix']];
27 $form['controls']['title'] = [
28 '#prefix' => '<h2 class="view-preview-form__title">',
29 '#markup' => $this->t('Preview'),
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'),
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',
50 if (!$form_state->isValueEmpty('view_args')) {
51 $args = explode('/', $form_state->getValue('view_args'));
54 $user_input = $form_state->getUserInput();
55 if ($form_state->get('show_preview') || !empty($user_input['js'])) {
58 '#theme_wrappers' => ['container'],
59 '#attributes' => ['id' => 'views-live-preview', 'class' => ['views-live-preview']],
60 'preview' => $view->renderPreview($this->displayID, $args),
63 $uri = $view->urlInfo('preview-form');
64 $uri->setRouteParameter('display_id', $this->displayID);
65 $form['#action'] = $uri->toString();
73 protected function actions(array $form, FormStateInterface $form_state) {
74 $view = $this->entity;
77 'id' => 'preview-submit-wrapper',
78 'class' => ['preview-submit-wrapper']
82 '#value' => $this->t('Update preview'),
83 '#attributes' => ['class' => ['arguments-preview']],
84 '#submit' => ['::submitPreview'],
85 '#id' => 'preview-submit',
87 'url' => Url::fromRoute('entity.view.preview_form', ['view' => $view->id(), 'display_id' => $this->displayID]),
88 'wrapper' => 'views-preview-wrapper',
90 'progress' => ['type' => 'fullscreen'],
91 'method' => 'replaceWith',
92 'disable-refocus' => TRUE,
99 * Form submission handler for the Preview button.
101 public function submitPreview($form, FormStateInterface $form_state) {
102 $form_state->set('show_preview', TRUE);
103 $form_state->setRebuild();