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

Hierarchy

Expanded class hierarchy of views_handler

File

includes/handlers.inc, line 155
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
   */
  public $view = NULL;

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

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

  /**
   * The alias of the table of this handler which is used in the query.
   */
  public $table_alias;

  /**
   * The actual field in the database table, maybe different
   * on other kind of query plugins/special handlers.
   */
  public $real_field;

  /**
   * The relationship used for this field.
   */
  public $relationship = NULL;

  /**
   * Init the handler with necessary data.
   *
   * @param view $view
   *   The $view object this handler is attached to.
   * @param array $options
   *   The item from the database; the actual contents of this will vary
   *   based upon the type of handler.
   */
  public 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.
    // If the 'moved to' keyword moved our handler, let's fix that now.
    if (isset($this->actual_table)) {
      $options['table'] = $this->actual_table;
    }
    if (isset($this->actual_field)) {
      $options['field'] = $this->actual_field;
    }
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public 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.
   */
  public 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'];
    if (empty($this->definition['group'])) {
      return $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)
   */
  public 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()) {
      $this->view
        ->init_query();
      if ($this->query) {
        $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 string $value
   *   The value being rendered.
   * @param string $type
   *   The type of sanitization needed. If not provided, check_plain() is used.
   *
   * @return string
   *   Returns the safe value.
   */
  public function sanitize_value($value, $type = NULL) {
    if (!is_null($value) && strlen($value) > 0) {
      switch ($type) {
        case 'xss':
          $value = filter_xss($value);
          break;
        case 'xss_admin':
          $value = filter_xss_admin($value);
          break;
        case 'url':
          $value = check_url($value);
          break;
        default:
          $value = check_plain($value);
          break;
      }
    }
    return $value;
  }

  /**
   * Transform a string by a certain method.
   *
   * @param string $string
   *    The input you want to transform.
   * @param array $option
   *    How do you want to transform it, possible values:
   *      - upper: Uppercase the string.
   *      - lower: lowercase the string.
   *      - ucfirst: Make the first char uppercase.
   *      - ucwords: Make each word in the string uppercase.
   *
   * @return string
   *    The transformed string.
   */
  public function case_transform($string, $option) {
    global $multibyte;
    switch ($option) {
      default:
        return $string;
      case 'upper':
        return drupal_strtoupper($string);
      case 'lower':
        return drupal_strtolower($string);
      case 'ucfirst':
        return drupal_strtoupper(drupal_substr($string, 0, 1)) . drupal_substr($string, 1);
      case 'ucwords':
        if ($multibyte == UNICODE_MULTIBYTE) {
          return mb_convert_case($string, MB_CASE_TITLE);
        }
        else {
          return ucwords($string);
        }
    }
  }

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

  /**
   * Build the options form.
   */
  public function options_form(&$form, &$form_state) {

    // Some form elements belong in a fieldset for presentation, but can't
    // be moved into one because of the form_state['values'] hierarchy. Those
    // elements can add a #fieldset => 'fieldset_name' property, and they'll
    // be moved to their fieldset during pre_render.
    $form['#pre_render'][] = 'views_ui_pre_render_add_fieldset_markup';
    $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'],
      '#fieldset' => 'more',
    );

    // This form is long and messy enough that the "Administrative title" option
    // belongs in a "more options" fieldset at the bottom of the form.
    $form['more'] = array(
      '#type' => 'fieldset',
      '#title' => t('More'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 150,
    );

    // Allow to alter the default values brought into the form.
    // Triggers hook_views_handler_options_alter().
    drupal_alter('views_handler_options', $this->options, $this);
  }

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

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

  /**
   * Provide a form for aggregation settings.
   */
  public function groupby_form(&$form, &$form_state) {
    $view =& $form_state['view'];
    $display_id = $form_state['display_id'];
    $types = views_object_types();
    $type = $form_state['type'];
    $id = $form_state['id'];
    $form['#title'] = check_plain($view->display[$display_id]->display_title) . ': ';
    $form['#title'] .= t('Configure aggregation settings for @type %item', array(
      '@type' => $types[$type]['lstitle'],
      '%item' => $this
        ->ui_name(),
    ));
    $form['#section'] = $display_id . '-' . $type . '-' . $id;
    $view
      ->init_query();
    $info = $view->query
      ->get_aggregation_info();
    foreach ($info as $id => $aggregate) {
      $group_types[$id] = $aggregate['title'];
    }
    $form['group_type'] = array(
      '#type' => 'select',
      '#title' => t('Aggregation type'),
      '#default_value' => $this->options['group_type'],
      '#description' => t('Select the aggregation function to use on this field.'),
      '#options' => $group_types,
    );
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
  public function groupby_form_submit(&$form, &$form_state) {
    $item =& $form_state['handler']->options;
    $item['group_type'] = $form_state['values']['options']['group_type'];
  }

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

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

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

  /**
   * Validate the options form.
   */
  public 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.
   */
  public function extra_options_submit($form, &$form_state) {
  }

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

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

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

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

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

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

  /**
   * Form for exposed handler options.
   */
  public function expose_form(&$form, &$form_state) {
  }

  /**
   * Validate the options form.
   */
  public 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.
   */
  public function expose_submit($form, &$form_state) {
  }

  /**
   * Shortcut to display the expose/hide button.
   */
  public function show_expose_button(&$form, &$form_state) {
  }

  /**
   * Shortcut to display the exposed options form.
   */
  public function show_expose_form(&$form, &$form_state) {
    if (empty($this->options['exposed'])) {
      return;
    }
    $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 bool
   */
  public 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.
   */
  public function pre_query() {
  }

  /**
   * Run after the view is executed, before the result is cached.
   *
   * This gives all the handlers some time to modify values. This is primarily
   * used so that handlers that pull up secondary data can put it in the
   * $values so that the raw data can be utilized externally.
   */
  public function post_execute(&$values) {
  }

  /**
   * Provides a unique placeholders for handlers.
   */
  public function placeholder() {
    return $this->query
      ->placeholder($this->options['table'] . '_' . $this->options['field']);
  }

  /**
   * Called just prior to query(), this lets a handler set up any relationship
   * it needs.
   */
  public 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;
  }

  /**
   * Ensure the main table for this handler is in the query. This is used
   * a lot.
   */
  public 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.
   */
  public function admin_summary() {
  }

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

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

  /**
   * Returns TRUE if the exposed filter works like a grouped filter.
   */
  public function is_a_group() {
    return FALSE;
  }

  /**
   * Define if the exposed input has to be submitted multiple times.
   * This is TRUE when exposed filters grouped are using checkboxes as
   * widgets.
   */
  public function multiple_exposed_input() {
    return FALSE;
  }

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

  /**
   * If set to remember exposed input in the session, store it there.
   */
  public 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'.
   */
  public 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 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 array
   *   Empty array if the handler is valid; an array of error strings if it is
   *   not.
   */
  public function validate() {
    return array();
  }

  /**
   * Determine if the handler is considered 'broken'.
   *
   * Generally only returns TRUE if the handler can't be found.
   *
   * @return bool
   *   The handler could not be loaded.
   */
  public function broken() {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_handler::$handler_type public property The type of the handler, for example filter/footer/field.
views_handler::$query public property Where the $query object will reside:. 1
views_handler::$real_field public property The actual field in the database table, maybe different on other kind of query plugins/special handlers.
views_handler::$relationship public property The relationship used for this field.
views_handler::$table_alias public property The alias of the table of this handler which is used in the query.
views_handler::$view public property The top object of a view. Overrides views_object::$view
views_handler::accept_exposed_input public function Take input from exposed handlers and assign to this handler, if necessary. 1
views_handler::access public function Check whether current user has access to this handler. 10
views_handler::admin_summary public function Provide text for the administrative summary. 4
views_handler::broken public function Determine if the handler is considered 'broken'. 6
views_handler::can_expose public function Determine if a handler can be exposed. 2
views_handler::case_transform public function Transform a string by a certain method.
views_handler::ensure_my_table public function Ensure the main table for this handler is in the query. This is used a lot. 8
views_handler::exposed_form public function Render our chunk of the exposed handler form when selecting. 1
views_handler::exposed_info public function Get information about the exposed form for the form renderer. 1
views_handler::exposed_submit public function Submit the exposed handler form.
views_handler::exposed_validate public function Validate the exposed handler form. 4
views_handler::expose_form public function Form for exposed handler options. 2
views_handler::expose_options public function Set new exposed option defaults when exposed setting is flipped on. 2
views_handler::expose_submit public 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 public function Validate the options form. 1
views_handler::extra_options public function Provide defaults for the handler.
views_handler::extra_options_form public function Provide a form for setting options. 1
views_handler::extra_options_submit public 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 public function Validate the options form.
views_handler::get_field public function Shortcut to get a handler's raw field value.
views_handler::get_join public function Get the join object that should be used for this handler.
views_handler::groupby_form public function Provide a form for aggregation settings. 1
views_handler::groupby_form_submit public function Perform any necessary changes to the form values prior to storage. There is no need for this function to actually store the data. 1
views_handler::has_extra_options public function If a handler has 'extra options' it will get a little settings widget and another form called extra_options. 1
views_handler::init public function Init the handler with necessary data. 6
views_handler::is_a_group public function Returns TRUE if the exposed filter works like a grouped filter. 1
views_handler::is_exposed public function Determine if this item is 'exposed', meaning it provides form elements to let users modify the view.
views_handler::multiple_exposed_input public function Define if the exposed input has to be submitted multiple times. This is TRUE when exposed filters grouped are using checkboxes as widgets. 1
views_handler::needs_style_plugin public function Determine if the argument needs a style plugin. 1
views_handler::options_form public function Build the options form. 6
views_handler::options_submit public function Perform any necessary changes to the form values prior to storage. There is no need for this function to actually store the data. 5
views_handler::options_validate public function Validate the options form. 4
views_handler::option_definition public function Information about options for all kinds of purposes will be held here. Overrides views_object::option_definition 6
views_handler::placeholder public function Provides a unique placeholders for handlers.
views_handler::post_execute public function Run after the view is executed, before the result is cached. 1
views_handler::pre_query public function Run before the view is built. 1
views_handler::sanitize_value public function Sanitize the value for output.
views_handler::set_relationship public function Called just prior to query(), this lets a handler set up any relationship it needs.
views_handler::show_expose_button public function Shortcut to display the expose/hide button. 2
views_handler::show_expose_form public function Shortcut to display the exposed options form.
views_handler::store_exposed_input public function If set to remember exposed input in the session, store it there. 1
views_handler::ui_name public function Return a string representing this handler's name in the UI. 9
views_handler::use_group_by public function Provides the handler some groupby. 2
views_handler::validate public function Validates the handler against the complete View. 1
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function