[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/image/views/ -> image_handler_field_image_node_image.inc (source)

   1  <?php
   2  // $Id: image_handler_field_image_node_image.inc,v 1.3 2009/08/26 14:42:04 joachim Exp $
   3  
   4  /**
   5   * @file
   6   * Views handler for image field.
   7   *
   8   * This is a fake field that does not query anything. Instead, it adds the node
   9   * table fields we need to build a fake node object to send to Image module to
  10   * load the proper image.
  11   */
  12  
  13  /**
  14   * Field handler to provide an image tag.
  15   *
  16   * Inherit from views_handler_field_node so we get the 'link to node' stuff for
  17   * free.
  18   */
  19  class image_handler_field_image_node_image extends views_handler_field_node {
  20  
  21    /**
  22     * Constructor to provide additional fields to add.
  23     *
  24     * Adds the fields that image_load() will need.
  25     */
  26    function construct() {
  27      parent::construct();
  28  
  29      $this->additional_fields['image_node_nid'] = array(
  30        'table' => 'node',
  31        'field' => 'nid',
  32      );
  33      // Node title for image rendering.
  34      $this->additional_fields['image_node_title'] = array(
  35        'table' => 'node',
  36        'field' => 'title',
  37      );
  38      // Node type to verify it is an image node.
  39      $this->additional_fields['image_node_type'] = array(
  40        'table' => 'node',
  41        'field' => 'type',
  42      );
  43    }
  44  
  45    /**
  46     * Define default values for options.
  47     */
  48    function option_definition() {
  49      $options = parent::option_definition();
  50      $options['image_derivative'] = array('default' => array(IMAGE_THUMBNAIL));
  51      return $options;
  52    }
  53  
  54    /**
  55     * Extend the field's basic options with more image specific options.
  56     */
  57    function options_form(&$form, &$form_state) {
  58      parent::options_form($form, $form_state);
  59  
  60      foreach (image_get_sizes() as $key => $size) {
  61        $sizes[$key] = $size['label'];
  62      }
  63      $form['image_derivative'] = array(
  64        '#type' => 'select',
  65        '#title' => t('Image size to show'),
  66        '#options' => $sizes,
  67        '#default_value' => $this->options['image_derivative'],
  68        '#description' => t('Pick an image derivative to display.'),
  69      );
  70    }
  71  
  72    /**
  73     * query() override to not query this fake field.
  74     */
  75    function query() {
  76      $this->ensure_my_table();
  77      $this->add_additional_fields();
  78    }
  79  
  80    /**
  81     * Return field html if the result is an image node.
  82     */
  83    function render($values) {
  84      if ($values->{$this->aliases['image_node_type']} == 'image') {
  85        $image_html = $this->render_html($values);
  86  
  87        // We inherit render_link() from views_handler_field_node, which takes
  88        // care of providing a link to the node if requested.
  89        return $this->render_link($image_html, $values);
  90      }
  91      else {
  92        return NULL;
  93      }
  94    }
  95  
  96    /**
  97     * Return image html, using image_load() and image_display().
  98     *
  99     * We rely on Image module to handle getting the data because although we can
 100     * use the derivative option to restrict the join, we do not have the agility
 101     * to fall back to the original when the requested derivative is larger than
 102     * the image and is absent from the system.
 103     */
 104    function render_html($values) {
 105      $derivative = $this->options['image_derivative'];
 106  
 107      $node = $this->build_image_display_node($values);
 108  
 109      // image_load() will load the files for all derivatives. Derivatives larger
 110      // than the original fall back to the original. Stale derivatives will be
 111      // regenerated.
 112      image_load($node);
 113  
 114      return image_display($node, $derivative);
 115    }
 116  
 117    /**
 118     * Build a pseudo node suitable for image_load() and image_display().
 119     *
 120     * This is lighter than using node_load(). image_load() needs:
 121     * - title (for rendering)
 122     * - nid
 123     */
 124    function build_image_display_node($values) {
 125      $node = new stdClass();
 126      $node->nid = $values->{$this->aliases['image_node_nid']};
 127      $node->title = $values->{$this->aliases['image_node_title']};
 128      return $node;
 129    }
 130  }
 131  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7