Skip to content
WizardPluginBase.php 41.6 KiB
Newer Older
Earl Miles's avatar
Earl Miles committed
<?php

/**
 * @file
Bram Goffings's avatar
Bram Goffings committed
 * Definition of Drupal\views\Plugin\views\wizard\WizardPluginBase.
namespace Drupal\views\Plugin\views\wizard;
use Drupal\Component\Utility\NestedArray;
use Drupal\views\Plugin\Core\Entity\View;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\wizard\WizardInterface;
Bram Goffings's avatar
Bram Goffings committed
 * Provides the interface and base class for Views Wizard plugins.
 *
 * This is a very generic Views Wizard class that can be constructed for any
 * base table.
abstract class WizardPluginBase extends PluginBase implements WizardInterface {
   * The base table connected with the wizard.
Earl Miles's avatar
Earl Miles committed
  protected $base_table;
   * The entity type connected with the wizard.
   *
   * There might be base tables connected with entity types, if not this would
   * be empty.
   *
   * @var string
   */
Earl Miles's avatar
Earl Miles committed
  protected $entity_type;
  /**
   * Contains the information from entity_get_info of the $entity_type.
   *
   * @var array
   */
Earl Miles's avatar
Earl Miles committed
  protected $entity_info = array();
  /**
   * An array of validated view objects, keyed by a hash.
   *
   * @var array
   */
Earl Miles's avatar
Earl Miles committed
  protected $validated_views = array();
  /**
   * The table column used for sorting by create date of this wizard.
   *
   * @var string
   */
  protected $createdColumn;

  /**
   * A views item configuration array used for a jump-menu field.
   *
   * @var array
   */
  protected $pathField = array();

  /**
   * Additional fields required to generate the pathField.
   *
   * @var array
   */
  protected $pathFieldsSupplemental = array();

  /**
   * Views items configuration arrays for filters added by the wizard.
   *
   * @var array
   */
  protected $filters = array();

  /**
   * Views items configuration arrays for sorts added by the wizard.
   *
   * @var array
   */
  protected $sorts = array();

  /**
   * The available store criteria.
   *
   * @var array
   */
  protected $availableSorts = array();

  /**
   * Default values for filters.
   *
   * By default, filters are not exposed and added to the first non-reserved
   * filter group.
   *
   * @var array()
   */
Earl Miles's avatar
Earl Miles committed
  protected $filter_defaults = array(
    'id' => NULL,
    'expose' => array('operator' => FALSE),
    'group' => 1,
  );

  public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->base_table = $this->definition['base_table'];
Earl Miles's avatar
Earl Miles committed

    $entities = entity_get_info();
    foreach ($entities as $entity_type => $entity_info) {
      if (isset($entity_info['base_table']) && $this->base_table == $entity_info['base_table']) {
Earl Miles's avatar
Earl Miles committed
        $this->entity_info = $entity_info;
        $this->entity_type = $entity_type;
      }
    }
  }

   * Gets the createdColumn property.
   *   The name of the column containing the created date.
   */
  public function getCreatedColumn() {
    return $this->createdColumn;
  }

  /**
   * Gets the pathField property.
   *   The pathField array.
   *
   * @todo Rename this to be something about jump menus, and/or resolve this
   *   dependency.
  public function getPathField() {
    return $this->pathField;
   * Gets the pathFieldsSupplemental property.
   * @return array()
   *
   * @todo Rename this to be something about jump menus, and/or remove this.
   */
  public function getPathFieldsSupplemental() {
    return $this->pathFieldsSupplemental;
  }

  /**
   * Gets the filters property.
   *
   * @return array
   */
  public function getFilters() {
    $filters = array();

    foreach ($this->filters as $name => $info) {
      $default['id'] = $name;
      $filters[$name] = $info + $default;
    }

    return $filters;
  }

  /**
   * Gets the availableSorts property.
   *
   * @return array
   */
  public function getAvailableSorts() {
    return $this->availableSorts;
  }

  /**
   * Gets the sorts property.
   *
   * @return array
   */
  public function getSorts() {
    return $this->sorts;
  }
   * Implements Drupal\views\Plugin\views\wizard\WizardInterface::build_form().
  function build_form(array $form, array &$form_state) {
Earl Miles's avatar
Earl Miles committed
    $style_options = views_fetch_plugin_names('style', 'normal', array($this->base_table));
    $feed_row_options = views_fetch_plugin_names('row', 'feed', array($this->base_table));
    $path_prefix = url(NULL, array('absolute' => TRUE));
Earl Miles's avatar
Earl Miles committed

    // Add filters and sorts which apply to the view as a whole.
    $this->build_filters($form, $form_state);
    $this->build_sorts($form, $form_state);

    $form['displays']['page'] = array(
      '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend')),
Earl Miles's avatar
Earl Miles committed
      '#tree' => TRUE,
    );
    $form['displays']['page']['create'] = array(
      '#title' => t('Create a page'),
      '#type' => 'checkbox',
      '#attributes' => array('class' => array('strong')),
Earl Miles's avatar
Earl Miles committed
      '#id' => 'edit-page-create',
    );

    // All options for the page display are included in this container so they
    // can be hidden as a group when the "Create a page" checkbox is unchecked.
Earl Miles's avatar
Earl Miles committed
    $form['displays']['page']['options'] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('options-set')),
      '#states' => array(
        'visible' => array(
          ':input[name="page[create]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
      '#prefix' => '<div><div id="edit-page-wrapper">',
      '#suffix' => '</div></div>',
      '#parents' => array('page'),
    );

    $form['displays']['page']['options']['title'] = array(
      '#title' => t('Page title'),
      '#type' => 'textfield',
    );
    $form['displays']['page']['options']['path'] = array(
      '#title' => t('Path'),
      '#type' => 'textfield',
      '#field_prefix' => $path_prefix,
    );
    $form['displays']['page']['options']['style'] = array(
Earl Miles's avatar
Earl Miles committed
      '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
    );

    // Create the dropdown for choosing the display format.
    $form['displays']['page']['options']['style']['style_plugin'] = array(
      '#title' => t('Display format'),
      '#type' => 'select',
      '#options' => $style_options,
    );
    $style_form = &$form['displays']['page']['options']['style'];
    $style_form['style_plugin']['#default_value'] = static::getSelected($form_state, array('page', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
Earl Miles's avatar
Earl Miles committed
    // Changing this dropdown updates $form['displays']['page']['options'] via
    // AJAX.
    views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'page', 'options'));

    $this->build_form_style($form, $form_state, 'page');
    $form['displays']['page']['options']['items_per_page'] = array(
      '#title' => t('Items to display'),
      '#type' => 'number',
      '#default_value' => 10,
      '#min' => 0,
    $form['displays']['page']['options']['pager'] = array(
Earl Miles's avatar
Earl Miles committed
      '#title' => t('Use a pager'),
      '#type' => 'checkbox',
      '#default_value' => TRUE,
    );
    $form['displays']['page']['options']['link'] = array(
      '#title' => t('Create a menu link'),
      '#type' => 'checkbox',
      '#id' => 'edit-page-link',
    );
    $form['displays']['page']['options']['link_properties'] = array(
      '#type' => 'container',
      '#states' => array(
        'visible' => array(
          ':input[name="page[link]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
      '#prefix' => '<div id="edit-page-link-properties-wrapper">',
      '#suffix' => '</div>',
    );
    if (\Drupal::moduleHandler()->moduleExists('menu')) {
Earl Miles's avatar
Earl Miles committed
      $menu_options = menu_get_menus();
    }
    else {
      // These are not yet translated.
      $menu_options = menu_list_system_menus();
      foreach ($menu_options as $name => $title) {
        $menu_options[$name] = t($title);
      }
    }
    $form['displays']['page']['options']['link_properties']['menu_name'] = array(
      '#title' => t('Menu'),
      '#type' => 'select',
      '#options' => $menu_options,
    );
    $form['displays']['page']['options']['link_properties']['title'] = array(
      '#title' => t('Link text'),
      '#type' => 'textfield',
    );
    // Only offer a feed if we have at least one available feed row style.
    if ($feed_row_options) {
      $form['displays']['page']['options']['feed'] = array(
        '#title' => t('Include an RSS feed'),
        '#type' => 'checkbox',
        '#id' => 'edit-page-feed',
      );
      $form['displays']['page']['options']['feed_properties'] = array(
        '#type' => 'container',
        '#states' => array(
          'visible' => array(
            ':input[name="page[feed]"]' => array('checked' => TRUE),
          ),
Earl Miles's avatar
Earl Miles committed
        ),
        '#prefix' => '<div id="edit-page-feed-properties-wrapper">',
        '#suffix' => '</div>',
      );
      $form['displays']['page']['options']['feed_properties']['path'] = array(
        '#title' => t('Feed path'),
        '#type' => 'textfield',
        '#field_prefix' => $path_prefix,
      );
      // This will almost never be visible.
      $form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
        '#title' => t('Feed row style'),
        '#type' => 'select',
        '#options' => $feed_row_options,
        '#default_value' => key($feed_row_options),
        '#access' => (count($feed_row_options) > 1),
        '#states' => array(
          'visible' => array(
            ':input[name="page[feed]"]' => array('checked' => TRUE),
          ),
Earl Miles's avatar
Earl Miles committed
        ),
        '#prefix' => '<div id="edit-page-feed-properties-row-plugin-wrapper">',
        '#suffix' => '</div>',
      );
    }

    if (!\Drupal::moduleHandler()->moduleExists('block')) {
Earl Miles's avatar
Earl Miles committed
    $form['displays']['block'] = array(
      '#attributes' => array('class' => array('views-attachment', 'fieldset-no-legend')),
Earl Miles's avatar
Earl Miles committed
      '#tree' => TRUE,
    );
    $form['displays']['block']['create'] = array(
      '#title' => t('Create a block'),
      '#type' => 'checkbox',
      '#attributes' => array('class' => array('strong')),
      '#id' => 'edit-block-create',
    );

    // All options for the block display are included in this container so they
    // can be hidden as a group when the "Create a page" checkbox is unchecked.
Earl Miles's avatar
Earl Miles committed
    $form['displays']['block']['options'] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('options-set')),
      '#states' => array(
        'visible' => array(
          ':input[name="block[create]"]' => array('checked' => TRUE),
        ),
Earl Miles's avatar
Earl Miles committed
      ),
      '#prefix' => '<div id="edit-block-wrapper">',
      '#suffix' => '</div>',
      '#parents' => array('block'),
    );

    $form['displays']['block']['options']['title'] = array(
      '#title' => t('Block title'),
      '#type' => 'textfield',
    );
    $form['displays']['block']['options']['style'] = array(
Earl Miles's avatar
Earl Miles committed
      '#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
    );

    // Create the dropdown for choosing the display format.
    $form['displays']['block']['options']['style']['style_plugin'] = array(
      '#title' => t('Display format'),
      '#type' => 'select',
      '#options' => $style_options,
    );
    $style_form = &$form['displays']['block']['options']['style'];
    $style_form['style_plugin']['#default_value'] = static::getSelected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
Earl Miles's avatar
Earl Miles committed
    // Changing this dropdown updates $form['displays']['block']['options'] via
    // AJAX.
    views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));

    $this->build_form_style($form, $form_state, 'block');
    $form['displays']['block']['options']['items_per_page'] = array(
      '#title' => t('Items per page'),
      '#type' => 'number',
      '#default_value' => 5,
      '#min' => 0,
Earl Miles's avatar
Earl Miles committed
    );
    $form['displays']['block']['options']['pager'] = array(
      '#title' => t('Use a pager'),
      '#type' => 'checkbox',
      '#default_value' => FALSE,
    );

    return $form;
  }

  /**
   * Gets the current value of a #select element, from within a form constructor function.
   *
   * This function is intended for use in highly dynamic forms (in particular the
   * add view wizard) which are rebuilt in different ways depending on which
   * triggering element (AJAX or otherwise) was most recently fired. For example,
   * sometimes it is necessary to decide how to build one dynamic form element
   * based on the value of a different dynamic form element that may not have
   * even been present on the form the last time it was submitted. This function
   * takes care of resolving those conflicts and gives you the proper current
   * value of the requested #select element.
   *
   * By necessity, this function sometimes uses non-validated user input from
   * $form_state['input'] in making its determination. Although it performs some
   * minor validation of its own, it is not complete. The intention is that the
   * return value of this function should only be used to help decide how to
   * build the current form the next time it is reloaded, not to be saved as if
   * it had gone through the normal, final form validation process. Do NOT use
   * the results of this function for any other purpose besides deciding how to
   * build the next version of the form.
   *
   * @param $form_state
   *   The  standard associative array containing the current state of the form.
   * @param $parents
   *   An array of parent keys that point to the part of the submitted form
   *   values that are expected to contain the element's value (in the case where
   *   this form element was actually submitted). In a simple case (assuming
   *   #tree is TRUE throughout the form), if the select element is located in
   *   $form['wrapper']['select'], so that the submitted form values would
   *   normally be found in $form_state['values']['wrapper']['select'], you would
   *   pass array('wrapper', 'select') for this parameter.
   * @param $default_value
   *   The default value to return if the #select element does not currently have
   *   a proper value set based on the submitted input.
   * @param $element
   *   An array representing the current version of the #select element within
   *   the form.
   *
   * @return
   *   The current value of the #select element. A common use for this is to feed
   *   it back into $element['#default_value'] so that the form will be rendered
   *   with the correct value selected.
   */
  public static function getSelected($form_state, $parents, $default_value, $element) {
    // For now, don't trust this to work on anything but a #select element.
    if (!isset($element['#type']) || $element['#type'] != 'select' || !isset($element['#options'])) {
      return $default_value;
    }

    // If there is a user-submitted value for this element that matches one of
    // the currently available options attached to it, use that. We need to check
    // $form_state['input'] rather than $form_state['values'] here because the
    // triggering element often has the #limit_validation_errors property set to
    // prevent unwanted errors elsewhere on the form. This means that the
    // $form_state['values'] array won't be complete. We could make it complete
    // by adding each required part of the form to the #limit_validation_errors
    // property individually as the form is being built, but this is difficult to
    // do for a highly dynamic and extensible form. This method is much simpler.
    if (!empty($form_state['input'])) {
      $key_exists = NULL;
      $submitted = NestedArray::getValue($form_state['input'], $parents, $key_exists);
      // Check that the user-submitted value is one of the allowed options before
      // returning it. This is not a substitute for actual form validation;
      // rather it is necessary because, for example, the same select element
      // might have #options A, B, and C under one set of conditions but #options
      // D, E, F under a different set of conditions. So the form submission
      // might have occurred with option A selected, but when the form is rebuilt
      // option A is no longer one of the choices. In that case, we don't want to
      // use the value that was submitted anymore but rather fall back to the
      // default value.
      if ($key_exists && in_array($submitted, array_keys($element['#options']))) {
        return $submitted;
      }
    }

    // Fall back on returning the default value if nothing was returned above.
    return $default_value;
  }

   * Adds the style options to the wizard form.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   * @param string $type
   *   The display ID (e.g. 'page' or 'block').
  protected function build_form_style(array &$form, array &$form_state, $type) {
Earl Miles's avatar
Earl Miles committed
    $style_form =& $form['displays'][$type]['options']['style'];
    $style = $style_form['style_plugin']['#default_value'];
    $style_plugin = Views::pluginManager('style')->createInstance($style);
    if (isset($style_plugin) && $style_plugin->usesRowPlugin()) {
      $options = $this->row_style_options();
Earl Miles's avatar
Earl Miles committed
      $style_form['row_plugin'] = array(
        '#type' => 'select',
        '#title' => t('of'),
        '#options' => $options,
        '#access' => count($options) > 1,
      );
      // For the block display, the default value should be "titles (linked)",
      // if it's available (since that's the most common use case).
      $block_with_linked_titles_available = ($type == 'block' && isset($options['titles_linked']));
      $default_value = $block_with_linked_titles_available ? 'titles_linked' : key($options);
      $style_form['row_plugin']['#default_value'] = static::getSelected($form_state, array($type, 'style', 'row_plugin'), $default_value, $style_form['row_plugin']);
Earl Miles's avatar
Earl Miles committed
      // Changing this dropdown updates the individual row options via AJAX.
      views_ui_add_ajax_trigger($style_form, 'row_plugin', array('displays', $type, 'options', 'style', 'row_options'));

      // This is the region that can be updated by AJAX. The base class doesn't
      // add anything here, but child classes can.
      $style_form['row_options'] = array(
        '#theme_wrappers' => array('container'),
      );
    }
    elseif ($style_plugin->usesFields()) {
Earl Miles's avatar
Earl Miles committed
      $style_form['row_plugin'] = array('#markup' => '<span>' . t('of fields') . '</span>');
    }
  }

  /**
   * Retrieves row style plugin names.
   *
   * @return array
   *   Returns the plugin names available for the base table of the wizard.
  protected function row_style_options() {
Earl Miles's avatar
Earl Miles committed
    // Get all available row plugins by default.
    $options = views_fetch_plugin_names('row', 'normal', array($this->base_table));
    return $options;
  }

  /**
   * Builds the form structure for selecting the view's filters.
Earl Miles's avatar
Earl Miles committed
   *
   * By default, this adds "of type" and "tagged with" filters (when they are
   * available).
   */
  protected function build_filters(&$form, &$form_state) {
    module_load_include('inc', 'views_ui', 'admin');
    $bundles = entity_get_bundles($this->entity_type);
Earl Miles's avatar
Earl Miles committed
    // If the current base table support bundles and has more than one (like user).
    if (isset($this->entity_info['bundle_keys']) && !empty($bundles)) {
Earl Miles's avatar
Earl Miles committed
      // Get all bundles and their human readable names.
      $options = array('all' => t('All'));
Earl Miles's avatar
Earl Miles committed
        $options[$type] = $bundle['label'];
      }
      $form['displays']['show']['type'] = array(
        '#type' => 'select',
        '#title' => t('of type'),
        '#options' => $options,
      );
      $selected_bundle = static::getSelected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
Earl Miles's avatar
Earl Miles committed
      $form['displays']['show']['type']['#default_value'] = $selected_bundle;
      // Changing this dropdown updates the entire content of $form['displays']
      // via AJAX, since each bundle might have entirely different fields
      // attached to it, etc.
      views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays'));
    }
  }

  /**
   * Builds the form structure for selecting the view's sort order.
Earl Miles's avatar
Earl Miles committed
   *
   * By default, this adds a "sorted by [date]" filter (when it is available).
   */
  protected function build_sorts(&$form, &$form_state) {
    $sorts = array(
      'none' => t('Unsorted'),
    );
    // Check if we are allowed to sort by creation date.
    $created_column = $this->getCreatedColumn();
    if ($created_column) {
Earl Miles's avatar
Earl Miles committed
      $sorts += array(
        $created_column . ':DESC' => t('Newest first'),
        $created_column . ':ASC' => t('Oldest first'),
    if ($available_sorts = $this->getAvailableSorts()) {
      $sorts += $available_sorts;
Earl Miles's avatar
Earl Miles committed
    }

    // If there is no sorts option available continue.
    if (!empty($sorts)) {
      $form['displays']['show']['sort'] = array(
        '#type' => 'select',
        '#title' => t('sorted by'),
        '#options' => $sorts,
        '#default_value' => isset($created_column) ? $created_column . ':DESC' : 'none',
  /**
   * Instantiates a view object from form values.
   *
   * @return Drupal\views_ui\ViewUI
   *   The instantiated view UI object.
Earl Miles's avatar
Earl Miles committed
  protected function instantiate_view($form, &$form_state) {
    // Build the basic view properties and create the view.
    $values = array(
      'label' => $form_state['values']['label'],
      'description' => $form_state['values']['description'],
      'base_table' => $this->base_table,
      'langcode' => language_default()->langcode,
    $view = entity_create('view', $values);
Earl Miles's avatar
Earl Miles committed

    // Build all display options for this view.
    $display_options = $this->build_display_options($form, $form_state);

    // Allow the fully built options to be altered. This happens before adding
    // the options to the view, so that once they are eventually added we will
    // be able to get all the overrides correct.
    $this->alter_display_options($display_options, $form, $form_state);

    $this->addDisplays($view, $display_options, $form, $form_state);
   * Builds an array of display options for the view.
Earl Miles's avatar
Earl Miles committed
   *   An array whose keys are the names of each display and whose values are
   *   arrays of options for that display.
   */
  protected function build_display_options($form, $form_state) {
    // Display: Master
    $display_options['default'] = $this->default_display_options();
Earl Miles's avatar
Earl Miles committed
    $display_options['default'] += array(
      'filters' => array(),
      'sorts' => array(),
    );
    $display_options['default']['filters'] += $this->default_display_filters($form, $form_state);
    $display_options['default']['sorts'] += $this->default_display_sorts($form, $form_state);

    // Display: Page
    if (!empty($form_state['values']['page']['create'])) {
      $display_options['page'] = $this->page_display_options($form, $form_state);

      // Display: Feed (attached to the page)
      if (!empty($form_state['values']['page']['feed'])) {
        $display_options['feed'] = $this->page_feed_display_options($form, $form_state);
      }
    }

    // Display: Block
    if (!empty($form_state['values']['block']['create'])) {
      $display_options['block'] = $this->block_display_options($form, $form_state);
    }

    return $display_options;
  }

  /**
   * Alters the full array of display options before they are added to the view.
Earl Miles's avatar
Earl Miles committed
   */
  protected function alter_display_options(&$display_options, $form, $form_state) {
    foreach ($display_options as $display_type => $options) {
      // Allow style plugins to hook in and provide some settings.
      $style_plugin = Views::pluginManager('style')->createInstance($options['style']['type']);
      $style_plugin->wizardSubmit($form, $form_state, $this, $display_options, $display_type);
   * Adds the array of display options to the view, with appropriate overrides.
  protected function addDisplays(View $view, $display_options, $form, $form_state) {
    // Initialize and store the view executable to get the display plugin
    // instances.
    $executable = $view->get('executable');
Earl Miles's avatar
Earl Miles committed
    // Display: Master
    $default_display = $view->newDisplay('default', 'Master', 'default');
Earl Miles's avatar
Earl Miles committed
    foreach ($display_options['default'] as $option => $value) {
      $default_display->setOption($option, $value);
Earl Miles's avatar
Earl Miles committed
    }

    // Display: Page
    if (isset($display_options['page'])) {
      $display = $view->newDisplay('page', 'Page', 'page_1');
Earl Miles's avatar
Earl Miles committed
      // The page display is usually the main one (from the user's point of
      // view). Its options should therefore become the overall view defaults,
      // so that new displays which are added later automatically inherit them.
      $this->setDefaultOptions($display_options['page'], $display, $default_display);
      // Display: Feed (attached to the page).
Earl Miles's avatar
Earl Miles committed
      if (isset($display_options['feed'])) {
        $display = $view->newDisplay('feed', 'Feed', 'feed_1');
Earl Miles's avatar
Earl Miles committed
        $this->set_override_options($display_options['feed'], $display, $default_display);
      }
    }

Earl Miles's avatar
Earl Miles committed
    if (isset($display_options['block'])) {
      $display = $view->newDisplay('block', 'Block', 'block_1');
Earl Miles's avatar
Earl Miles committed
      // When there is no page, the block display options should become the
      // overall view defaults.
      if (!isset($display_options['page'])) {
        $this->setDefaultOptions($display_options['block'], $display, $default_display);
Earl Miles's avatar
Earl Miles committed
      }
      else {
        $this->set_override_options($display_options['block'], $display, $default_display);
      }
    }

    // Initialize displays and merge all plugin default values.
    $executable->mergeDefaults();
   * Assembles the default display options for the view.
   *
   * Most wizards will need to override this method to provide some fields
Earl Miles's avatar
Earl Miles committed
   * or a different row plugin.
  protected function default_display_options() {
Earl Miles's avatar
Earl Miles committed
    $display_options = array();
    $display_options['access']['type'] = 'none';
    $display_options['cache']['type'] = 'none';
    $display_options['query']['type'] = 'views_query';
    $display_options['exposed_form']['type'] = 'basic';
    $display_options['pager']['type'] = 'full';
    $display_options['style']['type'] = 'default';
    $display_options['row']['type'] = 'fields';
    // Add default options array to each plugin type.
    foreach ($display_options as &$options) {
      $options['options'] = array();
    }

    // Add a least one field so the view validates and the user has a preview.
    // The base field can provide a default in its base settings; otherwise,
    // choose the first field with a field handler.
    $data = Views::viewsData()->get($this->base_table);
Earl Miles's avatar
Earl Miles committed
    if (isset($data['table']['base']['defaults']['field'])) {
      $field = $data['table']['base']['defaults']['field'];
    }
    else {
      foreach ($data as $field => $field_data) {
        if (isset($field_data['field']['id'])) {
Earl Miles's avatar
Earl Miles committed
          break;
        }
      }
    }
    $display_options['fields'][$field] = array(
      'table' => $this->base_table,
      'field' => $field,
      'id' => $field,
    );

    return $display_options;
  }

   * Retrieves all filter information used by the default display.
   *
   * Additional to the one provided by the plugin this method takes care about
   * adding additional filters based on user input.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   *
   * @return array
   *   An array of filter arrays keyed by ID. A sort array contains the options
   *   accepted by a filter handler.
Earl Miles's avatar
Earl Miles committed
  protected function default_display_filters($form, $form_state) {
    $filters = array();

    // Add any filters provided by the plugin.
    foreach ($this->getFilters() as $name => $info) {
      $filters[$name] = $info;
Earl Miles's avatar
Earl Miles committed
    }

    // Add any filters specified by the user when filling out the wizard.
    $filters = array_merge($filters, $this->default_display_filters_user($form, $form_state));

    return $filters;
  }

   * Retrieves filter information based on user input for the default display.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   *
   * @return array
   *   An array of filter arrays keyed by ID. A sort array contains the options
   *   accepted by a filter handler.
  protected function default_display_filters_user(array $form, array &$form_state) {
Earl Miles's avatar
Earl Miles committed
    $filters = array();

    if (!empty($form_state['values']['show']['type']) && $form_state['values']['show']['type'] != 'all') {
      $bundle_key = $this->entity_info['bundle_keys']['bundle'];
Earl Miles's avatar
Earl Miles committed
      // Figure out the table where $bundle_key lives. It may not be the same as
      // the base table for the view; the taxonomy vocabulary machine_name, for
      // example, is stored in taxonomy_vocabulary, not taxonomy_term_data.
      module_load_include('inc', 'views_ui', 'admin');
Earl Miles's avatar
Earl Miles committed
      $fields = views_fetch_fields($this->base_table, 'filter');
      if (isset($fields[$this->base_table . '.' . $bundle_key])) {
        $table = $this->base_table;
      }
      else {
        foreach ($fields as $field_name => $value) {
          if ($pos = strpos($field_name, '.' . $bundle_key)) {
            $table = substr($field_name, 0, $pos);
            break;
          }
        }
      }
      $table_data = Views::viewsData()->get($table);
      // If the 'in' operator is being used, map the values to an array.
Bram Goffings's avatar
Bram Goffings committed
      $handler = $table_data[$bundle_key]['filter']['id'];
      $handler_definition = Views::pluginManager('filter')->getDefinition($handler);
Daniel Wehner's avatar
Daniel Wehner committed
      if ($handler == 'in_operator' || is_subclass_of($handler_definition['class'], 'Drupal\\views\\Plugin\\views\\filter\\InOperator')) {
Earl Miles's avatar
Earl Miles committed
        $value = drupal_map_assoc(array($form_state['values']['show']['type']));
      }
      // Otherwise, use just a single value.
Earl Miles's avatar
Earl Miles committed
      else {
        $value = $form_state['values']['show']['type'];
      }

      $filters[$bundle_key] = array(
        'id' => $bundle_key,
        'table' => $table,
        'field' => $bundle_key,
        'value' => $value,
      );
    }

    return $filters;
  }

   * Retrieves all sort information used by the default display.
   *
   * Additional to the one provided by the plugin this method takes care about
   * adding additional sorts based on user input.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   *
   * @return array
   *   An array of sort arrays keyed by ID. A sort array contains the options
   *   accepted by a sort handler.
Earl Miles's avatar
Earl Miles committed
  protected function default_display_sorts($form, $form_state) {
    $sorts = array();

    // Add any sorts provided by the plugin.
    foreach ($this->getSorts() as $name => $info) {
      $sorts[$name] = $info;
Earl Miles's avatar
Earl Miles committed
    }

    // Add any sorts specified by the user when filling out the wizard.
    $sorts = array_merge($sorts, $this->default_display_sorts_user($form, $form_state));

    return $sorts;
  }

   * Retrieves sort information based on user input for the default display.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   *
   * @return array
   *   An array of sort arrays keyed by ID. A sort array contains the options
   *   accepted by a sort handler.
Earl Miles's avatar
Earl Miles committed
  protected function default_display_sorts_user($form, $form_state) {
    $sorts = array();

    // Don't add a sort if there is no form value or the user set the sort to
    // 'none'.
Earl Miles's avatar
Earl Miles committed
    if (!empty($form_state['values']['show']['sort']) && $form_state['values']['show']['sort'] != 'none') {
      list($column, $sort) = explode(':', $form_state['values']['show']['sort']);
      // Column either be a column-name or the table-columnn-ame.
      $column = explode('-', $column);
      if (count($column) > 1) {
        $table = $column[0];
        $column = $column[1];
      }
      else {
        $table = $this->base_table;
        $column = $column[0];
      }

      // If the input is invalid, for example when the #default_value contains
      // created from node, but the wizard type is another base table, make
      // sure it is not added. This usually don't happen if you have js
      // enabled.
      if (isset($data[$column]['sort'])) {
        $sorts[$column] = array(
          'id' => $column,
          'table' => $table,
          'field' => $column,
          'order' => $sort,
       );
      }
   * Retrieves the page display options.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   *
   * @return array
  protected function page_display_options(array $form, array &$form_state) {
Earl Miles's avatar
Earl Miles committed
    $display_options = array();
    $page = $form_state['values']['page'];
    $display_options['title'] = $page['title'];
    $display_options['path'] = $page['path'];
    $display_options['style'] = array('type' => $page['style']['style_plugin']);
Earl Miles's avatar
Earl Miles committed
    // Not every style plugin supports row style plugins.
    // Make sure that the selected row plugin is a valid one.
    $options = $this->row_style_options();
    $display_options['row'] = array('type' => (isset($page['style']['row_plugin']) && isset($options[$page['style']['row_plugin']])) ? $page['style']['row_plugin'] : 'fields');

    // If the specific 0 items per page, use no pager.
Earl Miles's avatar
Earl Miles committed
    if (empty($page['items_per_page'])) {
      $display_options['pager']['type'] = 'none';
    }
    // If the user checked the pager checkbox use a full pager.
    elseif (isset($page['pager'])) {
Earl Miles's avatar
Earl Miles committed
      $display_options['pager']['type'] = 'full';
    }
    // If the user doesn't have checked the checkbox use the pager which just
    // displays a certain amount of items.
Earl Miles's avatar
Earl Miles committed
    else {
      $display_options['pager']['type'] = 'some';
    }
    $display_options['pager']['options']['items_per_page'] = $page['items_per_page'];

    // Generate the menu links settings if the user checked the link checkbox.
Earl Miles's avatar
Earl Miles committed
    if (!empty($page['link'])) {
      $display_options['menu']['type'] = 'normal';
      $display_options['menu']['title'] = $page['link_properties']['title'];
      $display_options['menu']['name'] = $page['link_properties']['menu_name'];
    }
    return $display_options;
  }

   * Retrieves the block display options.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   *
   * @return array