ViewPreviewForm.php 3.28 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\views_ui;

5
use Drupal\Core\Form\FormStateInterface;
6
use Drupal\Core\Url;
7

8 9 10
/**
 * Form controller for the Views preview form.
 */
11
class ViewPreviewForm extends ViewFormBase {
12

13
  /**
14
   * {@inheritdoc}
15
   */
16
  public function form(array $form, FormStateInterface $form_state) {
17 18
    $view = $this->entity;

19
    $form['#prefix'] = '<div id="views-preview-wrapper" class="views-preview-wrapper views-admin clearfix">';
20 21 22
    $form['#suffix'] = '</div>';
    $form['#id'] = 'views-ui-preview-form';

23
    $form_state->disableCache();
24 25 26

    $form['controls']['#attributes'] = array('class' => array('clearfix'));

27 28 29 30 31 32
    $form['controls']['title'] = array(
      '#prefix' => '<h2 class="view-preview-form__title">',
      '#markup' => $this->t('Preview'),
      '#suffix' => '</h2>',
    );

33 34 35 36
    // Add a checkbox controlling whether or not this display auto-previews.
    $form['controls']['live_preview'] = array(
      '#type' => 'checkbox',
      '#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 42 43
    );

    // Add the arguments textfield
    $form['controls']['view_args'] = array(
      '#type' => 'textfield',
44 45
      '#title' => $this->t('Preview with contextual filters:'),
      '#description' => $this->t('Separate contextual filter values with a "/". For example, %example.', array('%example' => '40/12/10')),
46 47 48 49
      '#id' => 'preview-args',
    );

    $args = array();
50 51
    if (!$form_state->isValueEmpty('view_args')) {
      $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 57 58
      $form['preview'] = array(
        '#weight' => 110,
        '#theme_wrappers' => array('container'),
59
        '#attributes' => array('id' => 'views-live-preview', 'class' => 'views-live-preview'),
60
        'preview' => $view->renderPreview($this->displayID, $args),
61 62
      );
    }
63
    $uri = $view->urlInfo('preview-form');
64 65
    $uri->setRouteParameter('display_id', $this->displayID);
    $form['#action'] = $uri->toString();
66 67 68 69 70

    return $form;
  }

  /**
71
   * {@inheritdoc}
72
   */
73
  protected function actions(array $form, FormStateInterface $form_state) {
74
    $view = $this->entity;
75 76 77
    return array(
      '#attributes' => array(
        'id' => 'preview-submit-wrapper',
78
        'class' => array('preview-submit-wrapper')
79 80 81
      ),
      'button' => array(
        '#type' => 'submit',
82
        '#value' => $this->t('Update preview'),
83
        '#attributes' => array('class' => array('arguments-preview')),
84
        '#submit' => array('::submitPreview'),
85 86
        '#id' => 'preview-submit',
        '#ajax' => array(
87
          'url' => Url::fromRoute('entity.view.preview_form', ['view' => $view->id(), 'display_id' => $this->displayID]),
88 89
          'wrapper' => 'views-preview-wrapper',
          'event' => 'click',
90
          'progress' => array('type' => 'fullscreen'),
91
          'method' => 'replaceWith',
92
          'disable-refocus' => TRUE,
93 94 95 96 97
        ),
      ),
    );
  }

98 99 100
  /**
   * Form submission handler for the Preview button.
   */
101
  public function submitPreview($form, FormStateInterface $form_state) {
102 103
    $form_state->set('show_preview', TRUE);
    $form_state->setRebuild();
104 105
  }

106
}