You are here

SortPluginBase.php in Views (for Drupal 7) 8.3

File

lib/Drupal/views/Plugin/views/sort/SortPluginBase.php
View source
<?php

/**
 * @file
 * Definition of Drupal\views\Plugin\views\sort\SortPluginBase.
 */
namespace Drupal\views\Plugin\views\sort;

use Drupal\views\Plugin\views\HandlerBase;
use Drupal\Core\Annotation\Plugin;

/**
 * @defgroup views_sort_handlers Views sort handlers
 * @{
 * Handlers to tell Views how to sort queries.
 */

/**
 * Base sort handler that has no options and performs a simple sort.
 *
 * @ingroup views_sort_handlers
 */
abstract class SortPluginBase extends HandlerBase {

  /**
   * Determine if a sort can be exposed.
   */
  public function canExpose() {
    return TRUE;
  }

  /**
   * Called to add the sort to a query.
   */
  public function query() {
    $this
      ->ensureMyTable();

    // Add the field.
    $this->query
      ->add_orderby($this->tableAlias, $this->realField, $this->options['order']);
  }
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['order'] = array(
      'default' => 'ASC',
    );
    $options['exposed'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['expose'] = array(
      'contains' => array(
        'label' => array(
          'default' => '',
          'translatable' => TRUE,
        ),
      ),
    );
    return $options;
  }

  /**
   * Display whether or not the sort order is ascending or descending
   */
  public function adminSummary() {
    if (!empty($this->options['exposed'])) {
      return t('Exposed');
    }
    switch ($this->options['order']) {
      case 'ASC':
      case 'asc':
      default:
        return t('asc');
        break;
      case 'DESC':
      case 'desc':
        return t('desc');
        break;
    }
  }

  /**
   * Basic options for all sort criteria
   */
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
    if ($this
      ->canExpose()) {
      $this
        ->showExposeButton($form, $form_state);
    }
    $form['op_val_start'] = array(
      '#value' => '<div class="clearfix">',
    );
    $this
      ->show_sort_form($form, $form_state);
    $form['op_val_end'] = array(
      '#value' => '</div>',
    );
    if ($this
      ->canExpose()) {
      $this
        ->showExposeForm($form, $form_state);
    }
  }

  /**
   * Shortcut to display the expose/hide button.
   */
  public function showExposeButton(&$form, &$form_state) {
    $form['expose_button'] = array(
      '#prefix' => '<div class="views-expose clearfix">',
      '#suffix' => '</div>',
      // Should always come first
      '#weight' => -1000,
    );

    // Add a checkbox for JS users, which will have behavior attached to it
    // so it can replace the button.
    $form['expose_button']['checkbox'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#attributes' => array(
        'class' => array(
          'js-only',
        ),
      ),
    );
    $form['expose_button']['checkbox']['checkbox'] = array(
      '#title' => t('Expose this sort to visitors, to allow them to change it'),
      '#type' => 'checkbox',
    );

    // Then add the button itself.
    if (empty($this->options['exposed'])) {
      $form['expose_button']['markup'] = array(
        '#markup' => '<div class="description exposed-description" style="float: left; margin-right:10px">' . t('This sort is not exposed. Expose it to allow the users to change it.') . '</div>',
      );
      $form['expose_button']['button'] = array(
        '#limit_validation_errors' => array(),
        '#type' => 'submit',
        '#value' => t('Expose sort'),
        '#submit' => array(
          'views_ui_config_item_form_expose',
        ),
      );
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
    }
    else {
      $form['expose_button']['markup'] = array(
        '#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
      );
      $form['expose_button']['button'] = array(
        '#limit_validation_errors' => array(),
        '#type' => 'submit',
        '#value' => t('Hide sort'),
        '#submit' => array(
          'views_ui_config_item_form_expose',
        ),
      );
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
    }
  }

  /**
   * Simple validate handler
   */
  public function validateOptionsForm(&$form, &$form_state) {
    $this
      ->sort_validate($form, $form_state);
    if (!empty($this->options['exposed'])) {
      $this
        ->validateExposeForm($form, $form_state);
    }
  }

  /**
   * Simple submit handler
   */
  public function submitOptionsForm(&$form, &$form_state) {
    unset($form_state['values']['expose_button']);

    // don't store this.
    $this
      ->sort_submit($form, $form_state);
    if (!empty($this->options['exposed'])) {
      $this
        ->submitExposeForm($form, $form_state);
    }
  }

  /**
   * Shortcut to display the value form.
   */
  function show_sort_form(&$form, &$form_state) {
    $options = $this
      ->sort_options();
    if (!empty($options)) {
      $form['order'] = array(
        '#type' => 'radios',
        '#options' => $options,
        '#default_value' => $this->options['order'],
      );
    }
  }
  function sort_validate(&$form, &$form_state) {
  }
  function sort_submit(&$form, &$form_state) {
  }

  /**
   * Provide a list of options for the default sort form.
   * Should be overridden by classes that don't override sort_form
   */
  function sort_options() {
    return array(
      'ASC' => t('Sort ascending'),
      'DESC' => t('Sort descending'),
    );
  }
  public function buildExposeForm(&$form, &$form_state) {

    // #flatten will move everything from $form['expose'][$key] to $form[$key]
    // prior to rendering. That's why the pre_render for it needs to run first,
    // so that when the next pre_render (the one for fieldsets) runs, it gets
    // the flattened data.
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
    $form['expose']['#flatten'] = TRUE;
    $form['expose']['label'] = array(
      '#type' => 'textfield',
      '#default_value' => $this->options['expose']['label'],
      '#title' => t('Label'),
      '#required' => TRUE,
      '#size' => 40,
      '#weight' => -1,
    );
  }

  /**
   * Provide default options for exposed sorts.
   */
  public function defaultExposeOptions() {
    $this->options['expose'] = array(
      'order' => $this->options['order'],
      'label' => $this->definition['title'],
    );
  }

}

/**
 * @}
 */

Classes

Namesort descending Description
SortPluginBase Base sort handler that has no options and performs a simple sort.