abstract class ExposedFormPluginBase in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase
Base class for Views exposed filter form plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, StringTranslationTrait- class \Drupal\views\Plugin\views\PluginBase implements DependentPluginInterface, ContainerFactoryPluginInterface, ViewsPluginInterface- class \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase implements CacheableDependencyInterface
 
 
- class \Drupal\views\Plugin\views\PluginBase implements DependentPluginInterface, ContainerFactoryPluginInterface, ViewsPluginInterface
 
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, StringTranslationTrait
Expanded class hierarchy of ExposedFormPluginBase
Related topics
1 file declares its use of ExposedFormPluginBase
- DisplayKernelTest.php in core/modules/ views/ src/ Tests/ Plugin/ DisplayKernelTest.php 
- Contains \Drupal\views\Tests\Plugin\DisplayKernelTest.
File
- core/modules/ views/ src/ Plugin/ views/ exposed_form/ ExposedFormPluginBase.php, line 36 
- Contains \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase.
Namespace
Drupal\views\Plugin\views\exposed_formView source
abstract class ExposedFormPluginBase extends PluginBase implements CacheableDependencyInterface {
  /**
   * {@inheritdoc}
   */
  protected $usesOptions = TRUE;
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['submit_button'] = array(
      'default' => $this
        ->t('Apply'),
    );
    $options['reset_button'] = array(
      'default' => FALSE,
    );
    $options['reset_button_label'] = array(
      'default' => $this
        ->t('Reset'),
    );
    $options['exposed_sorts_label'] = array(
      'default' => $this
        ->t('Sort by'),
    );
    $options['expose_sort_order'] = array(
      'default' => TRUE,
    );
    $options['sort_asc_label'] = array(
      'default' => $this
        ->t('Asc'),
    );
    $options['sort_desc_label'] = array(
      'default' => $this
        ->t('Desc'),
    );
    return $options;
  }
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['submit_button'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Submit button text'),
      '#default_value' => $this->options['submit_button'],
      '#required' => TRUE,
    );
    $form['reset_button'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Include reset button (resets all applied exposed filters)'),
      '#default_value' => $this->options['reset_button'],
    );
    $form['reset_button_label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Reset button label'),
      '#description' => $this
        ->t('Text to display in the reset button of the exposed form.'),
      '#default_value' => $this->options['reset_button_label'],
      '#required' => TRUE,
      '#states' => array(
        'invisible' => array(
          'input[name="exposed_form_options[reset_button]"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
    $form['exposed_sorts_label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Exposed sorts label'),
      '#default_value' => $this->options['exposed_sorts_label'],
      '#required' => TRUE,
    );
    $form['expose_sort_order'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow people to choose the sort order'),
      '#description' => $this
        ->t('If sort order is not exposed, the sort criteria settings for each sort will determine its order.'),
      '#default_value' => $this->options['expose_sort_order'],
    );
    $form['sort_asc_label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for ascending sort'),
      '#default_value' => $this->options['sort_asc_label'],
      '#required' => TRUE,
      '#states' => array(
        'visible' => array(
          'input[name="exposed_form_options[expose_sort_order]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['sort_desc_label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label for descending sort'),
      '#default_value' => $this->options['sort_desc_label'],
      '#required' => TRUE,
      '#states' => array(
        'visible' => array(
          'input[name="exposed_form_options[expose_sort_order]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  /**
   * Render the exposed filter form.
   *
   * This actually does more than that; because it's using FAPI, the form will
   * also assign data to the appropriate handlers for use in building the
   * query.
   */
  public function renderExposedForm($block = FALSE) {
    // Deal with any exposed filters we may have, before building.
    $form_state = (new FormState())
      ->setStorage([
      'view' => $this->view,
      'display' => &$this->view->display_handler->display,
      'rerender' => TRUE,
    ])
      ->setMethod('get')
      ->setAlwaysProcess()
      ->disableRedirect();
    // Some types of displays (eg. attachments) may wish to use the exposed
    // filters of their parent displays instead of showing an additional
    // exposed filter form for the attachment as well as that for the parent.
    if (!$this->view->display_handler
      ->displaysExposed() || !$block && $this->view->display_handler
      ->getOption('exposed_block')) {
      $form_state
        ->set('rerender', NULL);
    }
    if (!empty($this->ajax)) {
      $form_state
        ->set('ajax', TRUE);
    }
    $form = \Drupal::formBuilder()
      ->buildForm('\\Drupal\\views\\Form\\ViewsExposedForm', $form_state);
    if (!$this->view->display_handler
      ->displaysExposed() || !$block && $this->view->display_handler
      ->getOption('exposed_block')) {
      return array();
    }
    else {
      return $form;
    }
  }
  public function query() {
    $view = $this->view;
    $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
    $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
    if (!empty($sort_by)) {
      // Make sure the original order of sorts is preserved
      // (e.g. a sticky sort is often first)
      if (isset($view->sort[$sort_by])) {
        $view->query->orderby = array();
        foreach ($view->sort as $key => $sort) {
          if (!$sort
            ->isExposed()) {
            $sort
              ->query();
          }
          elseif ($key == $sort_by) {
            if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array(
              'ASC',
              'DESC',
            ))) {
              $sort->options['order'] = $exposed_data['sort_order'];
            }
            $sort
              ->setRelationship();
            $sort
              ->query();
          }
        }
      }
    }
  }
  public function preRender($values) {
  }
  public function postRender(&$output) {
  }
  public function preExecute() {
  }
  public function postExecute() {
  }
  /**
   * Alters the view exposed form.
   *
   * @param $form
   *   The form build array. Passed by reference.
   * @param $form_state
   *   The form state. Passed by reference.
   */
  public function exposedFormAlter(&$form, FormStateInterface $form_state) {
    if (!empty($this->options['submit_button'])) {
      $form['actions']['submit']['#value'] = $this->options['submit_button'];
    }
    // Check if there is exposed sorts for this view
    $exposed_sorts = array();
    foreach ($this->view->sort as $id => $handler) {
      if ($handler
        ->canExpose() && $handler
        ->isExposed()) {
        $exposed_sorts[$id] = Html::escape($handler->options['expose']['label']);
      }
    }
    if (count($exposed_sorts)) {
      $form['sort_by'] = array(
        '#type' => 'select',
        '#options' => $exposed_sorts,
        '#title' => $this->options['exposed_sorts_label'],
      );
      $sort_order = array(
        'ASC' => $this->options['sort_asc_label'],
        'DESC' => $this->options['sort_desc_label'],
      );
      $user_input = $form_state
        ->getUserInput();
      if (isset($user_input['sort_by']) && isset($this->view->sort[$user_input['sort_by']])) {
        $default_sort_order = $this->view->sort[$user_input['sort_by']]->options['order'];
      }
      else {
        $first_sort = reset($this->view->sort);
        $default_sort_order = $first_sort->options['order'];
      }
      if (!isset($user_input['sort_by'])) {
        $keys = array_keys($exposed_sorts);
        $user_input['sort_by'] = array_shift($keys);
        $form_state
          ->setUserInput($user_input);
      }
      if ($this->options['expose_sort_order']) {
        $form['sort_order'] = array(
          '#type' => 'select',
          '#options' => $sort_order,
          '#title' => $this
            ->t('Order', array(), array(
            'context' => 'Sort order',
          )),
          '#default_value' => $default_sort_order,
        );
      }
      $form['submit']['#weight'] = 10;
    }
    if (!empty($this->options['reset_button'])) {
      $form['actions']['reset'] = array(
        '#value' => $this->options['reset_button_label'],
        '#type' => 'submit',
        '#weight' => 10,
      );
      // Get an array of exposed filters, keyed by identifier option.
      $exposed_filters = [];
      foreach ($this->view->filter as $id => $handler) {
        if ($handler
          ->canExpose() && $handler
          ->isExposed() && !empty($handler->options['expose']['identifier'])) {
          $exposed_filters[$handler->options['expose']['identifier']] = $id;
        }
      }
      $all_exposed = array_merge($exposed_sorts, $exposed_filters);
      // Set the access to FALSE if there is no exposed input.
      if (!array_intersect_key($all_exposed, $this->view
        ->getExposedInput())) {
        $form['actions']['reset']['#access'] = FALSE;
      }
    }
    $pager = $this->view->display_handler
      ->getPlugin('pager');
    if ($pager) {
      $pager
        ->exposedFormAlter($form, $form_state);
      $form_state
        ->set('pager_plugin', $pager);
    }
  }
  public function exposedFormValidate(&$form, FormStateInterface $form_state) {
    if ($pager_plugin = $form_state
      ->get('pager_plugin')) {
      $pager_plugin
        ->exposedFormValidate($form, $form_state);
    }
  }
  /**
   * This function is executed when exposed form is submitted.
   *
   * @param $form
   *   Nested array of form elements that comprise the form.
   * @param $form_state
   *   The current state of the form.
   * @param $exclude
   *   Nested array of keys to exclude of insert into
   *   $view->exposed_raw_input
   */
  public function exposedFormSubmit(&$form, FormStateInterface $form_state, &$exclude) {
    if (!$form_state
      ->isValueEmpty('op') && $form_state
      ->getValue('op') == $this->options['reset_button_label']) {
      $this
        ->resetForm($form, $form_state);
    }
    if ($pager_plugin = $form_state
      ->get('pager_plugin')) {
      $pager_plugin
        ->exposedFormSubmit($form, $form_state, $exclude);
      $exclude[] = 'pager_plugin';
    }
  }
  public function resetForm(&$form, FormStateInterface $form_state) {
    // _SESSION is not defined for users who are not logged in.
    // If filters are not overridden, store the 'remember' settings on the
    // default display. If they are, store them on this display. This way,
    // multiple displays in the same view can share the same filters and
    // remember settings.
    $display_id = $this->view->display_handler
      ->isDefaulted('filters') ? 'default' : $this->view->current_display;
    if (isset($_SESSION['views'][$this->view->storage
      ->id()][$display_id])) {
      unset($_SESSION['views'][$this->view->storage
        ->id()][$display_id]);
    }
    // Set the form to allow redirect.
    if (empty($this->view->live_preview) && !\Drupal::request()
      ->isXmlHttpRequest()) {
      $form_state
        ->disableRedirect(FALSE);
    }
    else {
      $form_state
        ->setRebuild();
      $this->view->exposed_data = array();
    }
    $form_state
      ->setRedirect('<current>');
    $form_state
      ->setValues([]);
  }
  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    $contexts = [];
    if ($this->options['expose_sort_order']) {
      // The sort order query arg is just important in case there is a exposed
      // sort order.
      $has_exposed_sort_handler = FALSE;
      /** @var \Drupal\views\Plugin\views\sort\SortPluginBase $sort_handler */
      foreach ($this->displayHandler
        ->getHandlers('sort') as $sort_handler) {
        if ($sort_handler
          ->isExposed()) {
          $has_exposed_sort_handler = TRUE;
        }
      }
      if ($has_exposed_sort_handler) {
        $contexts[] = 'url.query_args:sort_order';
      }
    }
    return $contexts;
  }
  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return [];
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| ExposedFormPluginBase:: | protected | property | Denotes whether the plugin has an additional options form. Overrides PluginBase:: | |
| ExposedFormPluginBase:: | public | function | Provide a form to edit options for this plugin. Overrides PluginBase:: | 1 | 
| ExposedFormPluginBase:: | protected | function | Information about options for all kinds of purposes will be held here. Overrides PluginBase:: | 1 | 
| ExposedFormPluginBase:: | public | function | Alters the view exposed form. | |
| ExposedFormPluginBase:: | public | function | This function is executed when exposed form is submitted. | |
| ExposedFormPluginBase:: | public | function | ||
| ExposedFormPluginBase:: | public | function | The cache contexts associated with this object. Overrides CacheableDependencyInterface:: | |
| ExposedFormPluginBase:: | public | function | The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: | |
| ExposedFormPluginBase:: | public | function | The cache tags associated with this object. Overrides CacheableDependencyInterface:: | |
| ExposedFormPluginBase:: | public | function | ||
| ExposedFormPluginBase:: | public | function | ||
| ExposedFormPluginBase:: | public | function | ||
| ExposedFormPluginBase:: | public | function | 1 | |
| ExposedFormPluginBase:: | public | function | Add anything to the query that we might need to. Overrides PluginBase:: | 1 | 
| ExposedFormPluginBase:: | public | function | Render the exposed filter form. | |
| ExposedFormPluginBase:: | public | function | ||
| PluginBase:: | protected | property | Configuration information passed into the plugin. | 2 | 
| PluginBase:: | public | property | Plugins's definition | |
| PluginBase:: | public | property | The display object this plugin is for. | |
| PluginBase:: | public | property | Options for this plugin will be held here. | |
| PluginBase:: | protected | property | The plugin implementation definition. | |
| PluginBase:: | protected | property | The plugin_id. | |
| PluginBase:: | protected | property | Stores the render API renderer. | 2 | 
| PluginBase:: | public | property | The top object of a view. | 1 | 
| PluginBase:: | public | function | Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: | 20 | 
| PluginBase:: | public static | function | Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | 50 | 
| PluginBase:: | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
| PluginBase:: | public | function | Clears a plugin. Overrides ViewsPluginInterface:: | 2 | 
| PluginBase:: | protected | function | Do the work to filter out stored options depending on the defined options. | |
| PluginBase:: | public | function | Filter out stored options depending on the defined options. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public | function | Returns an array of available token replacements. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public | function | Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Returns the plugin provider. Overrides ViewsPluginInterface:: | |
| PluginBase:: | protected | function | Returns the render API renderer. | 1 | 
| PluginBase:: | public | function | Adds elements for available core tokens to a form. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public | function | Returns a string with any core tokens replaced. Overrides ViewsPluginInterface:: | |
| PluginBase:: | constant | Include entity row languages when listing languages. | ||
| PluginBase:: | constant | Include negotiated languages when listing languages. | ||
| PluginBase:: | public | function | Initialize the plugin. Overrides ViewsPluginInterface:: | 8 | 
| PluginBase:: | protected | function | Makes an array of languages, optionally including special languages. | |
| PluginBase:: | public | function | Return the human readable name of the display. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public static | function | Moves form elements into fieldsets for presentation purposes. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public static | function | Flattens the structure of form elements. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public static | function | Returns substitutions for Views queries for languages. | |
| PluginBase:: | protected | function | Fills up the options of the plugin with defaults. | |
| PluginBase:: | public | function | Handle any special handling on the validate form. Overrides ViewsPluginInterface:: | 16 | 
| PluginBase:: | public | function | Returns the summary of the settings in the display. Overrides ViewsPluginInterface:: | 6 | 
| PluginBase:: | public | function | Provide a full list of possible theme templates used by this style. Overrides ViewsPluginInterface:: | 1 | 
| PluginBase:: | public | function | Unpack options over our existing defaults, drilling down into arrays
so that defaults don't get totally blown away. Overrides ViewsPluginInterface:: | |
| PluginBase:: | public | function | Returns the usesOptions property. Overrides ViewsPluginInterface:: | 8 | 
| PluginBase:: | public | function | Validate that the plugin is correct and can be saved. Overrides ViewsPluginInterface:: | 5 | 
| PluginBase:: | public | function | Validate the options form. Overrides ViewsPluginInterface:: | 15 | 
| PluginBase:: | protected | function | Replaces Views' tokens in a given string. The resulting string will be sanitized with Xss::filterAdmin. | 1 | 
| PluginBase:: | constant | Query string to indicate the site default language. | ||
| PluginBase:: | public | function | Constructs a PluginBase object. Overrides PluginBase:: | |
| StringTranslationTrait:: | protected | property | The string translation service. | |
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | 
