/**
* Returns a view row object in which the property names are unaliased.
*
* This facilitates accessing properties from theme hooks when the
* field_alias is arbitrary unknown, or just non-trivial or messy to access.
* It introduces the potential for field name collisions if there are fields
* with the same names, such as a view of nodes with a relationship to
* another node which lists the value of the same field from both nodes. In
* this case, sitetheme_get_unaliased_row() will rename the subsequent
* occurences of fields with the same name, appending '_0', '_1', '_2' etc to
* them. As an example;
*
* Node types 'article' and 'event' both have the imagefield 'field_image'
* attached. The 'article' node type also has a nodereference field
* 'field_event' attached to it. If the view has both fields
* article->field_image as well as article->event->field_image (accessed
* via a relationship in the view), and article->event->field_image occurs
* first in the fields of the view, then article->event->field_image will
* become return->field_image, and article->field_image will become
* return->field_image_0 (where 'return' is what
* sitetheme_get_unaliased_row() returns).
*
* This is ugly, but a tidier solution is beyond the scope of the problem
* this function is intended to solve. Further Views' field_aliases already
* solve this problem efficiently from a technical standpoint. This function
* is merely to provide more convenient access in common use cases in themes.
*
* It is not uncommon to call this multiple times from theme functions in the
* same row, such as from preprocess functions from different fields in the
* same view. Therefore sitetheme_get_unaliased_row() caches it's result by
* default. However generating an md5 hash for the key of the cache may be
* more expensive than what it is worth. Thus this may be disabled, for
* example, if the owner knows that it will not be unaliasing this row again.
*
* @param &$view: view Object:
* The view object from which $row was generated. It is not modified.
* @param &$row stcClass Object:
* The $row that is to be un-aliased. It is not modified.
* @param $cache Boolean:
* Don't cache the unaliased return value. See notes above.
*/
function sitetheme_get_unaliased_row(&$view, &$row, $cache = TRUE) {
static $cache = array();
// These are useful for profiling and debugging this code.
// static $built_count, $cached_count;
// If we are allowed to cache, generate the key, and return the cached
// result (if it exists).
if ($cache) {
// @todo Is the time it costs to md5()-hash worth the memory it saves?
$key = md5(serialize($row));
if (isset($cache[$key])) {
// These are useful for profiling and debugging this code.
// $cached_count++;
// dpm("Cached; $cached_count");
return $cache[$key];
}
}
// These are useful for profiling and debugging this code.
// $built_count++;
// dpm("Built; $built_count");
// Map the aliased fields to a new row-like object with un-aliased property
// names.
$return = new stdClass();
foreach ($view->query->fields as $alias => $field) {
if (isset($row->{$alias})) {
$name = $field['field'];
// Make the name unique if there are name collisions.
for ($count = 0; isset($return->{$name}); $count++) {
$name = $field['field'] . '_' . $count;
}
// Set the unaliased value
$return->{$name} = $row->{$alias};
}
}
// Store to cache, if cache is enabled.
if ($cache) {
$cache[$key] = $return;
}
return $return;
}