Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created January 25, 2010 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bangpound/285700 to your computer and use it in GitHub Desktop.
Save bangpound/285700 to your computer and use it in GitHub Desktop.
Semantic Views: add a field's value to the class attribute of its row.
<?php
/**
* Add content type to the class of the row.
*/
function MODULE_preprocess_semanticviews_view_unformatted(&$vars, $hook) {
if ($hook == 'semanticviews_view_unformatted__package__node_content_1') {
// The index in the field array is the simple field name and is consistent
// across joins. e.g. when the field is 'type', the field alias could be
// 'node_node_data_field_package_content_type' if it's at the end of a
// join.
$alias = $vars['view']->field['type']->field_alias;
// The 'type' field is excluded, and when Views themes style output, the
// excluded fields are not available in $vars['rows']. Reaching into the
// result property of the view object is necessary.
foreach ($vars['view']->result as $id => $row) {
// The value is the machine name of the content type.
$type = $row->{$alias};
// Row attributes are used by Semantic Views to add class attributes to
// each themed row of Views output.
if (empty($vars['row_attributes'][$id]['class'])) {
$vars['row_attributes'][$id]['class'] = $type;
}
else {
$vars['row_attributes'][$id]['class'] .= ' '. $type;
}
}
}
}
<!-- This is actual output from Semantic Views. The content type is the last item on the rows' class attributes. -->
<div id="package-node_content_1" class="view view-package view-id-package view-display-id-node_content_1 view-dom-id-4">
<ul>
<li class="first odd blog">
<a href="/node/16529">EI video of Olmert protest goes global</a>
</li>
<li class="even video">
<a href="/node/17637">Citizens arrest, disruption of Olmert in San Francisco</a>
</li>
<li class="last odd story">
<a href="/node/14985">The ugly reality of settlement-made beauty products</a>
</li>
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment