Same name and namespace in other branches
  1. 7.x-3.x includes/handlers.inc \views_handler

Base handler, from which all the other handlers are derived. It creates a common interface to create consistency amongst handlers and data.

This class would be abstract in PHP5, but PHP4 doesn't understand that.

Definition terms:

  • table: The actual table this uses; only specify if different from the table this is attached to.
  • real field: The actual field this uses; only specify if different from the field this item is attached to.
  • group: A text string representing the 'group' this item is attached to, for display in the UI. Examples: "Node", "Taxonomy", "Comment", "User", etc. This may be inherited from the parent definition or the 'table' definition.
  • title: The title for this handler in the UI. This may be inherited from the parent definition or the 'table' definition.
  • help: A more informative string to give to the user to explain what this field/handler is or does.
  • access callback: If this field should have access control, this could be a function to use. 'user_access' is a common function to use here. If not specified, no access control is provided.
  • access arguments: An array of arguments for the access callback.

Hierarchy

Expanded class hierarchy of views_handler

1 string reference to 'views_handler'
views_views_handlers in includes/handlers.inc
Implementation of hook_views_handlers() to register all of the basic handlers views uses.

File

includes/handlers.inc, line 250
handlers.inc Defines the various handler objects to help build and display views.

View source
class views_handler extends views_object {

  /**
   * The top object of a view.
   *
   * @var view
   */
  var $view = NULL;

  /**
   * Where the $query object will reside:
   *
   * @var views_plugin_query
   */
  var $query = NULL;

  /**
   * The type of the handler, for example filter/footer/field.
   */
  var $handler_type = NULL;

  /**
   * init the handler with necessary data.
   * @param $view
   *   The $view object this handler is attached to.
   * @param $options
   *   The item from the database; the actual contents of this will vary
   *   based upon the type of handler.
   */
  function init(&$view, $options) {
    $this->view =& $view;
    $display_id = $this->view->current_display;

    // Check to see if this handler type is defaulted. Note that
    // we have to do a lookup because the type is singular but the
    // option is stored as the plural.
    $types = views_object_types();
    $plural = $this->handler_type;
    if (isset($types[$this->handler_type]['plural'])) {
      $plural = $types[$this->handler_type]['plural'];
    }
    if ($this->view->display_handler
      ->is_defaulted($plural)) {
      $display_id = 'default';
    }
    $this->localization_keys = array(
      $display_id,
      $this->handler_type,
      $options['table'],
      $options['id'],
    );
    $this
      ->unpack_options($this->options, $options);

    // This exist on most handlers, but not all. So they are still optional.
    if (isset($options['table'])) {
      $this->table = $options['table'];
    }
    if (isset($this->definition['real field'])) {
      $this->real_field = $this->definition['real field'];
    }
    if (isset($this->definition['field'])) {
      $this->real_field = $this->definition['field'];
    }
    if (isset($options['field'])) {
      $this->field = $options['field'];
      if (!isset($this->real_field)) {
        $this->real_field = $options['field'];
      }
    }
    $this->query =& $view->query;
  }
  function option_definition() {
    $options = parent::option_definition();
    $options['id'] = array(
      'default' => '',
    );
    $options['table'] = array(
      'default' => '',
    );
    $options['field'] = array(
      'default' => '',
    );
    $options['relationship'] = array(
      'default' => 'none',
    );
    $options['group_type'] = array(
      'default' => 'group',
    );
    $options['ui_name'] = array(
      'default' => '',
    );
    return $options;
  }

  /**
   * Return a string representing this handler's name in the UI.
   */
  function ui_name($short = FALSE) {
    if (!empty($this->options['ui_name'])) {
      $title = check_plain($this->options['ui_name']);
      return $title;
    }
    $title = $short && isset($this->definition['title short']) ? $this->definition['title short'] : $this->definition['title'];
    return t('!group: !title', array(
      '!group' => $this->definition['group'],
      '!title' => $title,
    ));
  }

  /**
   * Shortcut to get a handler's raw field value.
   *
   * This should be overridden for handlers with formulae or other
   * non-standard fields. Because this takes an argument, fields
   * overriding this can just call return parent::get_field($formula)
   */
  function get_field($field = NULL) {
    if (!isset($field)) {
      if (!empty($this->formula)) {
        $field = $this
          ->get_formula();
      }
      else {
        $field = $this->table_alias . '.' . $this->real_field;
      }
    }

    // If grouping, check to see if the aggregation method needs to modify the field.
    if ($this->view->display_handler
      ->use_group_by()) {
      $info = $this->query
        ->get_aggregation_info();
      if (!empty($info[$this->options['group_type']]['method']) && function_exists($info[$this->options['group_type']]['method'])) {
        return $info[$this->options['group_type']]['method']($this->options['group_type'], $field);
      }
    }
    return $field;
  }

  /**
   * Sanitize the value for output.
   *
   * @param $value
   *   The value being rendered.
   * @param $type
   *   The type of sanitization needed. If not provided, check_plain() is used.
   */
  function sanitize_value($value, $type = NULL) {
    switch ($type) {
      case 'xss':
        $value = filter_xss($value);
        break;
      case 'url':
        $value = check_url($value);
        break;
      default:
        $value = check_plain($value);
        break;
    }
    return $value;
  }

  /**
   * Validate the options form.
   */
  function options_validate($form, &$form_state) {
  }
  function options_form(&$form, &$form_state) {
    $form['ui_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Administrative Title'),
      '#description' => t('This title will be displayed on the views edit page instead of the default one. This might be useful if you have the same item twice.'),
      '#default_value' => $this->options['ui_name'],
    );
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
  function options_submit($form, &$form_state) {
  }

  /**
   * Provides the handler some groupby.
   */
  function use_group_by() {
    return TRUE;
  }

  /**
   * If a handler has 'extra options' it will get a little settings widget and
   * another form called extra_options.
   */
  function has_extra_options() {
    return FALSE;
  }

  /**
   * Provide defaults for the handler.
   */
  function extra_options(&$option) {
  }

  /**
   * Provide a form for setting options.
   */
  function extra_options_form(&$form, &$form_state) {
  }

  /**
   * Validate the options form.
   */
  function extra_options_validate($form, &$form_state) {
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
  function extra_options_submit($form, &$form_state) {
  }

  /**
   * Determine if a handler can be exposed.
   */
  function can_expose() {
    return FALSE;
  }

  /**
   * Set new exposed option defaults when exposed setting is flipped
   * on.
   */
  function expose_options() {
  }

  /**
   * Get information about the exposed form for the form renderer.
   */
  function exposed_info() {
  }

  /**
   * Render our chunk of the exposed handler form when selecting
   */
  function exposed_form(&$form, &$form_state) {
  }

  /**
   * Validate the exposed handler form
   */
  function exposed_validate(&$form, &$form_state) {
  }

  /**
   * Submit the exposed handler form
   */
  function exposed_submit(&$form, &$form_state) {
  }

  /**
   * Overridable form for exposed handler options.
   *
   * If overridden, it is best to call the parent or re-implement
   * the stuff here.
   *
   * Many handlers will need to override this in order to provide options
   * that are nicely tailored to the given filter.
   */
  function expose_form(&$form, &$form_state) {
    $form['expose']['start_left'] = array(
      '#value' => '<div class="views-left-50">',
    );
    $this
      ->expose_form_left($form, $form_state);
    $form['expose']['end_left'] = array(
      '#value' => '</div>',
    );
    $form['expose']['start_checkboxes'] = array(
      '#value' => '<div class="form-checkboxes views-left-40 clear-block">',
    );
    $this
      ->expose_form_right($form, $form_state);
    $form['expose']['end_checkboxes'] = array(
      '#value' => '</div>',
    );
  }
  function expose_form_left(&$form, &$form_state) {
  }
  function expose_form_right(&$form, &$form_state) {
  }

  /**
   * Validate the options form.
   */
  function expose_validate($form, &$form_state) {
  }

  /**
   * Perform any necessary changes to the form exposes prior to storage.
   * There is no need for this function to actually store the data.
   */
  function expose_submit($form, &$form_state) {
  }

  /**
   * Shortcut to display the expose/hide button.
   */
  function show_expose_button(&$form, &$form_state) {
    $form['expose_button'] = array(
      '#prefix' => '<div class="views-expose clear-block">',
      '#suffix' => '</div>',
    );
    if (empty($this->options['exposed'])) {
      $form['expose_button']['button'] = array(
        '#type' => 'submit',
        '#value' => t('Expose'),
        '#submit' => array(
          'views_ui_config_item_form_expose',
        ),
      );
      $form['expose_button']['markup'] = array(
        '#prefix' => '<div class="description">',
        '#value' => t('This item is currently not exposed. If you <strong>expose</strong> it, users will be able to change the filter as they view it.'),
        '#suffix' => '</div>',
      );
    }
    else {
      $form['expose_button']['button'] = array(
        '#type' => 'submit',
        '#value' => t('Hide'),
        '#submit' => array(
          'views_ui_config_item_form_expose',
        ),
      );
      $form['expose_button']['markup'] = array(
        '#prefix' => '<div class="description">',
        '#value' => t('This item is currently exposed. If you <strong>hide</strong> it, users will not be able to change the filter as they view it.'),
        '#suffix' => '</div>',
      );
    }
  }

  /**
   * Shortcut to display the exposed options form.
   */
  function show_expose_form(&$form, &$form_state) {
    if (empty($this->options['exposed'])) {
      return;
    }
    $form['expose'] = array(
      '#prefix' => '<div class="views-expose-options clear-block">',
      '#suffix' => '</div>',
    );
    $this
      ->expose_form($form, $form_state);

    // When we click the expose button, we add new gadgets to the form but they
    // have no data in $_POST so their defaults get wiped out. This prevents
    // these defaults from getting wiped out. This setting will only be TRUE
    // during a 2nd pass rerender.
    if (!empty($form_state['force_expose_options'])) {
      foreach (element_children($form['expose']) as $id) {
        if (isset($form['expose'][$id]['#default_value']) && !isset($form['expose'][$id]['#value'])) {
          $form['expose'][$id]['#value'] = $form['expose'][$id]['#default_value'];
        }
      }
    }
  }

  /**
   * Check whether current user has access to this handler.
   *
   * @return boolean
   */
  function access() {
    if (isset($this->definition['access callback']) && function_exists($this->definition['access callback'])) {
      if (isset($this->definition['access arguments']) && is_array($this->definition['access arguments'])) {
        return call_user_func_array($this->definition['access callback'], $this->definition['access arguments']);
      }
      return $this->definition['access callback']();
    }
    return TRUE;
  }

  /**
   * Run before the view is built.
   *
   * This gives all the handlers some time to set up before any handler has
   * been fully run.
   */
  function pre_query() {
  }

  /**
   * Called just prior to query(), this lets a handler set up any relationship
   * it needs.
   */
  function set_relationship() {

    // Ensure this gets set to something.
    $this->relationship = NULL;

    // Don't process non-existant relationships.
    if (empty($this->options['relationship']) || $this->options['relationship'] == 'none') {
      return;
    }
    $relationship = $this->options['relationship'];

    // Ignore missing/broken relationships.
    if (empty($this->view->relationship[$relationship])) {
      return;
    }

    // Check to see if the relationship has already processed. If not, then we
    // cannot process it.
    if (empty($this->view->relationship[$relationship]->alias)) {
      return;
    }

    // Finally!
    $this->relationship = $this->view->relationship[$relationship]->alias;
  }

  /**
   * Add this handler into the query.
   *
   * If we were using PHP5, this would be abstract.
   *
   * @param bool $group_by
   *   (optional) Whether the view is using an aggregation query. If you
   *    override this method you can specify $group_by as optional parameter.
   */
  function query() {
  }

  /**
   * Ensure the main table for this handler is in the query. This is used
   * a lot.
   */
  function ensure_my_table() {
    if (!isset($this->table_alias)) {
      if (!method_exists($this->query, 'ensure_table')) {
        vpr(t('Ensure my table called but query has no ensure_table method.'));
        return;
      }
      $this->table_alias = $this->query
        ->ensure_table($this->table, $this->relationship);
    }
    return $this->table_alias;
  }

  /**
   * Provide text for the administrative summary
   */
  function admin_summary() {
  }

  /**
   * Determine if the argument needs a style plugin.
   *
   * @return TRUE/FALSE
   */
  function needs_style_plugin() {
    return FALSE;
  }

  /**
   * Determine if this item is 'exposed', meaning it provides form elements
   * to let users modify the view.
   *
   * @return TRUE/FALSE
   */
  function is_exposed() {
    return !empty($this->options['exposed']);
  }

  /**
   * Take input from exposed handlers and assign to this handler, if necessary.
   */
  function accept_exposed_input($input) {
    return TRUE;
  }

  /**
   * If set to remember exposed input in the session, store it there.
   */
  function store_exposed_input($input, $status) {
    return TRUE;
  }

  /**
   * Get the join object that should be used for this handler.
   *
   * This method isn't used a great deal, but it's very handy for easily
   * getting the join if it is necessary to make some changes to it, such
   * as adding an 'extra'.
   */
  function get_join() {

    // get the join from this table that links back to the base table.
    // Determine the primary table to seek
    if (empty($this->query->relationships[$this->relationship])) {
      $base_table = $this->query->base_table;
    }
    else {
      $base_table = $this->query->relationships[$this->relationship]['base'];
    }
    $join = views_get_table_join($this->table, $base_table);
    if ($join) {
      return drupal_clone($join);
    }
  }

  /**
   * Validates the handler against the complete View.
   *
   * This is called when the complete View is being validated. For validating
   * the handler options form use options_validate().
   *
   * @see views_handler::options_validate()
   *
   * @return
   *   Empty array if the handler is valid; an array of error strings if it is not.
   */
  function validate() {
    return array();
  }

  /**
   * Determine if the handler is considered 'broken', meaning it's a
   * a placeholder used when a handler can't be found.
   */
  function broken() {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_handler::$handler_type property The type of the handler, for example filter/footer/field.
views_handler::$query property Where the $query object will reside: 6
views_handler::$view property The top object of a view. Overrides views_object::$view
views_handler::accept_exposed_input function Take input from exposed handlers and assign to this handler, if necessary. 1
views_handler::access function Check whether current user has access to this handler. 8
views_handler::admin_summary function Provide text for the administrative summary 4
views_handler::broken function Determine if the handler is considered 'broken', meaning it's a a placeholder used when a handler can't be found. 6
views_handler::can_expose function Determine if a handler can be exposed. 2
views_handler::ensure_my_table function Ensure the main table for this handler is in the query. This is used a lot. 8
views_handler::exposed_form function Render our chunk of the exposed handler form when selecting 2
views_handler::exposed_info function Get information about the exposed form for the form renderer. 1
views_handler::exposed_submit function Submit the exposed handler form
views_handler::exposed_validate function Validate the exposed handler form 4
views_handler::expose_form function Overridable form for exposed handler options.
views_handler::expose_form_left function 2
views_handler::expose_form_right function 2
views_handler::expose_options function Set new exposed option defaults when exposed setting is flipped on. 2
views_handler::expose_submit function Perform any necessary changes to the form exposes prior to storage. There is no need for this function to actually store the data.
views_handler::expose_validate function Validate the options form. 1
views_handler::extra_options function Provide defaults for the handler.
views_handler::extra_options_form function Provide a form for setting options. 1
views_handler::extra_options_submit function Perform any necessary changes to the form values prior to storage. There is no need for this function to actually store the data.
views_handler::extra_options_validate function Validate the options form.
views_handler::get_field function Shortcut to get a handler's raw field value.
views_handler::get_join function Get the join object that should be used for this handler.
views_handler::has_extra_options function If a handler has 'extra options' it will get a little settings widget and another form called extra_options. 1
views_handler::init function init the handler with necessary data. 5
views_handler::is_exposed function Determine if this item is 'exposed', meaning it provides form elements to let users modify the view.
views_handler::needs_style_plugin function Determine if the argument needs a style plugin. 1
views_handler::options_form function 6
views_handler::options_submit function Perform any necessary changes to the form values prior to storage. There is no need for this function to actually store the data. 4
views_handler::options_validate function Validate the options form. 4
views_handler::option_definition function Information about options for all kinds of purposes will be held here. Overrides views_object::option_definition 6
views_handler::pre_query function Run before the view is built. 1
views_handler::query function Add this handler into the query. 6
views_handler::sanitize_value function Sanitize the value for output.
views_handler::set_relationship function Called just prior to query(), this lets a handler set up any relationship it needs.
views_handler::show_expose_button function Shortcut to display the expose/hide button.
views_handler::show_expose_form function Shortcut to display the exposed options form.
views_handler::store_exposed_input function If set to remember exposed input in the session, store it there. 1
views_handler::ui_name function Return a string representing this handler's name in the UI. 10
views_handler::use_group_by function Provides the handler some groupby. 2
views_handler::validate function Validates the handler against the complete View. 1
views_object::$definition property Handler's definition
views_object::$options property Except for displays, options for the object will be held here. 1
views_object::construct function Views handlers use a special construct function so that we can more easily construct them with variable arguments. 6
views_object::destroy function 2
views_object::export_option function 1
views_object::export_options function
views_object::options function Set default options on this object. Called by the constructor in a complex chain to deal with backward compatibility. 1
views_object::set_default_options function Set default options. For backward compatibility, it sends the options array; this is a feature that will likely disappear at some point.
views_object::set_definition function Let the handler know what its full definition is.
views_object::unpack_options function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable function Unpack a single option definition.
views_object::unpack_translatables function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults function