You are here

itoggle_views_handler.inc in iToggle 7

View handler for iToggle Views

File

modules/views/itoggle_views_handler.inc
View source
<?php

/**
 * @file
 * View handler for iToggle Views
 */

/**
 * A handler to provide a custom field
 *
 * @ingroup views_field_handlers
 */
class itoggle_views_handler_field extends views_handler_field {

  // avoid using drupal_static() intentionally
  protected static $_itoggle_init = FALSE;
  protected $_itoggle;

  // I have found no place to add css/js includes that executes just once per view
  // all functions here will be executed once per itoggle field
  // we use a static cache to avoid this
  function init(&$view, &$options) {

    // find out the field name (ie what entity type and what property)
    $field_name = str_replace('itoggle_', '', $options['id']);
    list($type, $property) = explode('_', $field_name);

    // store info in instance variable
    $this->_itoggle = array(
      'info' => itoggle_get_entity_info(),
      'type' => $type,
      'property' => $property,
    );
    if (self::$_itoggle_init === FALSE) {
      self::$_itoggle_init = TRUE;
      itoggle_include_itoggle();
      itoggle_include_settings();
      drupal_add_js(drupal_get_path('module', 'itoggle') . '/js/itoggle.js');
    }
    parent::init($view, $options);
  }
  function query() {

    // Do nothing, as this handler does not need to do anything to the query itself.
  }

  /**
   * Render the trigger field and its linked popup information.
   */
  function render($values) {
    $type = $this->_itoggle['type'];
    $property = $this->_itoggle['property'];
    $id = $this->_itoggle['info'][$type]['entity keys']['id'];
    if (isset($values->{$id})) {
      $id = $values->{$id};
      $entity = current(entity_load($type, array(
        $id,
      )));

      // @TODO check if value exists in $values array
      $checked = $entity->{$property} == 1;
      return theme('itoggle', array(
        'type' => $type,
        'id' => $id,
        'property' => $property,
        'checked' => $checked,
        'scope' => 'entity',
      ));
    }
    return NULL;
  }

  /**
   * Called to determine what to tell the clicksorter.
   */
  function click_sort($order) {
    $property = $this->_itoggle['property'];
    if (isset($this->field_alias)) {

      // Since fields should always have themselves already added, just
      // add a sort on the field.
      $params = $this->options['group_type'] != 'group' ? array(
        'function' => $this->options['group_type'],
      ) : array();
      $this->query
        ->add_orderby(NULL, NULL, $order, $property, $params);
    }
  }

}

Classes

Namesort descending Description
itoggle_views_handler_field A handler to provide a custom field