Fix for Get an unaliased views row object for views for views_view_field preprocess functions

  1. /**
  2.  * Returns a view row object in which the property names are unaliased.
  3.  *
  4.  * This facilitates accessing properties from theme hooks when the
  5.  * field_alias is arbitrary unknown, or just non-trivial or messy to access.
  6.  * It introduces the potential for field name collisions if there are fields
  7.  * with the same names, such as a view of nodes with a relationship to
  8.  * another node which lists the value of the same field from both nodes.  In
  9.  * this case, sitetheme_get_unaliased_row() will rename the subsequent
  10.  * occurences of fields with the same name, appending '_0', '_1', '_2' etc to
  11.  * them.  As an example;
  12.  *
  13.  *   Node types 'article' and 'event' both have the imagefield 'field_image'
  14.  *   attached.  The 'article' node type also has a nodereference field
  15.  *   'field_event' attached to it.  If the view has both fields
  16.  *   article->field_image as well as article->event->field_image (accessed
  17.  *   via a relationship in the view), and article->event->field_image occurs
  18.  *   first in the fields of the view, then article->event->field_image will
  19.  *   become return->field_image, and article->field_image will become
  20.  *   return->field_image_0 (where 'return' is what
  21.  *   sitetheme_get_unaliased_row() returns).
  22.  *
  23.  * This is ugly, but a tidier solution is beyond the scope of the problem
  24.  * this function is intended to solve.  Further Views' field_aliases already
  25.  * solve this problem efficiently from a technical standpoint.  This function
  26.  * is merely to provide more convenient access in common use cases in themes.
  27.  *
  28.  * It is not uncommon to call this multiple times from theme functions in the
  29.  * same row, such as from preprocess functions from different fields in the
  30.  * same view.  Therefore sitetheme_get_unaliased_row() caches it's result by
  31.  * default.  However generating an md5 hash for the key of the cache may be
  32.  * more expensive than what it is worth.  Thus this may be disabled, for
  33.  * example, if the owner knows that it will not be unaliasing this row again.
  34.  *
  35.  * @param &$view: view Object:
  36.  *    The view object from which $row was generated.  It is not modified.
  37.  * @param &$row stcClass Object:
  38.  *    The $row that is to be un-aliased.  It is not modified.
  39.  * @param $cache Boolean:
  40.  *    Don't cache the unaliased return value.  See notes above.
  41.  */
  42. function sitetheme_get_unaliased_row(&$view, &$row, $cache = TRUE) {
  43.   static $cache = array();
  44.   // These are useful for profiling and debugging this code.
  45.   // static $built_count, $cached_count;
  46.  
  47.   // If we are allowed to cache, generate the key, and return the cached
  48.   // result (if it exists).
  49.   if ($cache) {
  50.     // @todo Is the time it costs to md5()-hash worth the memory it saves?
  51.     $key = md5(serialize($row));
  52.     if (isset($cache[$key])) {
  53.  
  54.       // These are useful for profiling and debugging this code.
  55.       // $cached_count++;
  56.       // dpm("Cached; $cached_count");
  57.  
  58.       return $cache[$key];
  59.     }
  60.   }
  61.  
  62.   // These are useful for profiling and debugging this code.
  63.   // $built_count++;
  64.   // dpm("Built; $built_count");
  65.  
  66.   // Map the aliased fields to a new row-like object with un-aliased property
  67.   // names.
  68.   $return = new stdClass();
  69.   foreach ($view->query->fields as $alias => $field) {
  70.     if (isset($row->{$alias})) {
  71.       $name = $field['field'];
  72.  
  73.       // Make the name unique if there are name collisions.
  74.       for ($count = 0; isset($return->{$name}); $count++) {
  75.         $name = $field['field'] . '_' . $count;
  76.       }
  77.  
  78.       // Set the unaliased value
  79.       $return->{$name} = $row->{$alias};
  80.     }
  81.   }
  82.  
  83.   // Store to cache, if cache is enabled.
  84.   if ($cache) {
  85.     $cache[$key] = $return;
  86.   }
  87.  
  88.   return $return;
  89. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.