class ViewPreviewFormController

Form controller for the Views preview form.

Hierarchy

Expanded class hierarchy of ViewPreviewFormController

File

drupal/core/modules/views_ui/lib/Drupal/views_ui/ViewPreviewFormController.php, line 17
Contains Drupal\views_ui\ViewPreviewFormController.

Namespace

Drupal\views_ui
View source
class ViewPreviewFormController extends ViewFormControllerBase implements EntityControllerInterface {

  /**
   * The views temp store.
   *
   * @var \Drupal\user\TempStore
   */
  protected $tempStore;

  /**
   * Constructs a new ViewPreviewFormController object.
   *
   * @param string $operation
   *   The name of the current operation.
   * @param \Drupal\user\TempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   */
  public function __construct($operation, TempStoreFactory $temp_store_factory) {
    parent::__construct($operation);
    $this->tempStore = $temp_store_factory
      ->get('views');
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info, $operation = NULL) {
    return new static($operation, $container
      ->get('user.tempstore'));
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, array &$form_state) {
    $view = $this->entity;
    $form['#prefix'] = '<div id="views-preview-wrapper" class="views-admin clearfix">';
    $form['#suffix'] = '</div>';
    $form['#id'] = 'views-ui-preview-form';

    // Reset the cache of IDs. Drupal rather aggressively prevents ID
    // duplication but this causes it to remember IDs that are no longer even
    // being used.
    $seen_ids_init =& drupal_static('drupal_html_id:init');
    $seen_ids_init = array();
    $form_state['no_cache'] = TRUE;
    $form['controls']['#attributes'] = array(
      'class' => array(
        'clearfix',
      ),
    );

    // Add a checkbox controlling whether or not this display auto-previews.
    $form['controls']['live_preview'] = array(
      '#type' => 'checkbox',
      '#id' => 'edit-displays-live-preview',
      '#title' => t('Auto preview'),
      '#default_value' => config('views.settings')
        ->get('ui.always_live_preview'),
    );

    // Add the arguments textfield
    $form['controls']['view_args'] = array(
      '#type' => 'textfield',
      '#title' => t('Preview with contextual filters:'),
      '#description' => t('Separate contextual filter values with a "/". For example, %example.', array(
        '%example' => '40/12/10',
      )),
      '#id' => 'preview-args',
    );
    $args = array();
    if (!empty($form_state['values']['view_args'])) {
      $args = explode('/', $form_state['values']['view_args']);
    }
    if (!empty($form_state['show_preview'])) {
      $form['preview'] = array(
        '#weight' => 110,
        '#theme_wrappers' => array(
          'container',
        ),
        '#attributes' => array(
          'id' => 'views-live-preview',
        ),
        '#markup' => $view
          ->renderPreview($this->displayID, $args),
      );
    }
    $form['#action'] = url('admin/structure/views/view/' . $view
      ->id() . '/preview/' . $this->displayID);
    return $form;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::actions().
   */
  protected function actions(array $form, array &$form_state) {
    $view = $this->entity;
    return array(
      '#attributes' => array(
        'id' => 'preview-submit-wrapper',
      ),
      'button' => array(
        '#type' => 'submit',
        '#value' => t('Update preview'),
        '#attributes' => array(
          'class' => array(
            'arguments-preview',
          ),
        ),
        '#submit' => array(
          array(
            $this,
            'submitPreview',
          ),
        ),
        '#id' => 'preview-submit',
        '#ajax' => array(
          'path' => 'admin/structure/views/view/' . $view
            ->id() . '/preview/' . $this->displayID,
          'wrapper' => 'views-preview-wrapper',
          'event' => 'click',
          'progress' => array(
            'type' => 'throbber',
          ),
          'method' => 'replace',
        ),
      ),
    );
  }

  /**
   * Form submission handler for the Preview button.
   */
  public function submitPreview($form, &$form_state) {

    // Rebuild the form with a pristine $view object.
    $view = $this->entity;

    // Attempt to load the view from temp store, otherwise create a new one.
    if (!($new_view = $this->tempStore
      ->get($view
      ->id()))) {
      $new_view = new ViewUI($view);
    }
    $form_state['build_info']['args'][0] = $new_view;
    $form_state['show_preview'] = TRUE;
    $form_state['rebuild'] = TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityFormController::$entity protected property The entity being used by this form.
EntityFormController::$operation protected property The name of the current operation.
EntityFormController::actionsElement protected function Returns the action form element for the current entity form.
EntityFormController::buildEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::buildEntity(). Overrides EntityFormControllerInterface::buildEntity 2
EntityFormController::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
EntityFormController::delete public function Form submission handler for the 'delete' action. 12
EntityFormController::getBaseFormID public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormID
EntityFormController::getEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getEntity(). Overrides EntityFormControllerInterface::getEntity
EntityFormController::getFormDisplay public function Returns the form display. Overrides EntityFormControllerInterface::getFormDisplay
EntityFormController::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
EntityFormController::getFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getFormLangcode(). Overrides EntityFormControllerInterface::getFormLangcode
EntityFormController::getOperation public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getOperation(). Overrides EntityFormControllerInterface::getOperation
EntityFormController::isDefaultFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::isDefaultFormLangcode(). Overrides EntityFormControllerInterface::isDefaultFormLangcode
EntityFormController::save public function Form submission handler for the 'save' action. 20
EntityFormController::setEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::setEntity(). Overrides EntityFormControllerInterface::setEntity
EntityFormController::setFormDisplay public function Sets the form display. Overrides EntityFormControllerInterface::setFormDisplay
EntityFormController::setOperation public function Sets the operation for this form.
EntityFormController::submit public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::submit(). Overrides EntityFormControllerInterface::submit 13
EntityFormController::submitEntityLanguage protected function Handle possible entity language changes. 1
EntityFormController::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityFormController::updateFormLangcode protected function Updates the form language to reflect any change to the entity language.
EntityFormController::validate public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::validate(). Overrides EntityFormControllerInterface::validate 11
EntityFormController::validateForm public function Form validation handler. Overrides FormInterface::validateForm
ViewFormControllerBase::$displayID protected property The name of the display used by the form.
ViewFormControllerBase::getAdminCSS public static function Creates an array of Views admin CSS for adding or attaching.
ViewFormControllerBase::getDisplayLabel public function Placeholder function for overriding $display['display_title'].
ViewFormControllerBase::getDisplayTabs public function Adds tabs for navigating across Displays when editing a View.
ViewFormControllerBase::init public function Initialize the form state and the entity before the first form build. Overrides EntityFormController::init 2
ViewFormControllerBase::isDefaultDisplayShown public function Controls whether or not the default display should have its own tab on edit.
ViewFormControllerBase::prepareEntity protected function Overrides Drupal\Core\Entity\EntityFormController::prepareForm(). Overrides EntityFormController::prepareEntity 2
ViewPreviewFormController::$tempStore protected property The views temp store.
ViewPreviewFormController::actions protected function Overrides Drupal\Core\Entity\EntityFormController::actions(). Overrides EntityFormController::actions
ViewPreviewFormController::createInstance public static function Instantiates a new instance of this entity controller. Overrides EntityControllerInterface::createInstance
ViewPreviewFormController::form public function Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityFormController::form
ViewPreviewFormController::submitPreview public function Form submission handler for the Preview button.
ViewPreviewFormController::__construct public function Constructs a new ViewPreviewFormController object. Overrides EntityFormController::__construct