background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

The Fivestar module offers a great way to allow users to rate your content. Enabling this functionality for a node type was straight forward. My next goal was to show the fivestar widget in a view for each node row. Unfortunately, the row styling for my view was "Fields", so the fivestar widget would not appear. Apparently, the fivestar widget is only available for the teaser & full node display options.

No worries, you can always override the themable output of a view. Using the "Theme information" section on the view edit screen, I decided to create a new template file in my theme directory for "Row style output". For example:

views-view-fields--MY-VIEW-NAME.tpl.php

I then copied the contents of default view template into this file.

<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
* @file views-view-fields.tpl.php
* Default simple view template to all the fields as a row.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
*   - $field->content: The output of the field.
*   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
*   - $field->class: The safe class id to use.
*   - $field->handler: The Views field handler object controlling this field. Do not use
*     var_export to dump this object, as it can't handle the recursion.
*   - $field->inline: Whether or not the field should be inline.
*   - $field->inline_html: either div or span based on the above flag.
*   - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*/
?>

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
     
// $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
     
?>

      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>

By examining the hook_nodeapi() function of the Fivestar module, I decided to use the fivestar_widget_form() function to embed the widget in my view. This function accepts one argument, the $node object. Inside the first php tag in my new template file, I added the following code to load the node object:

<?php
$node
= node_load($row->nid);
?>

Last, I determined a good insertion point for the fivestar widget in this template file, and added the following code:

<?php
if (function_exists('fivestar_widget_form')) print fivestar_widget_form($node);
?>

If done correctly the Fivestar widget should now be embedded in the view for each node row.

Eric.London's picture

Hardcoding something in your module or theme can be just as bad as hacking the core. If the Drupal admin interface allows you the change and configure something, your modules and themes must be scalable enough to account for these changes. This code snippet (example) shows you how you can lookup the CCK field labels, so you'll never have to hard code a label in your theme/module again.

<?php
// create a container for the field labels
$fieldLabels = array();

// load node
$node = node_load('MYCCKNODEID');

// loop through node properties
foreach ($node as $k => $v) {
 
// ensure this property is a field
 
if (substr($k,0,6) == 'field_') {
   
// use the CCK function to get the field data for this field
   
$fieldData = content_fields($k, $node->type);

   
// add the label to the array
   
$fieldLabels[$k] = $fieldData['widget']['label'];
  }
}

// for debug, you could now show all the fields with their labels:
echo "<pre>";
print_r($fieldLabels);
echo
"</pre>";
?>

Eric.London's picture

You may have noticed that if you create a custom node type that has uses the radios widget, there is no visual error indicator attached to the radios. I added some jQuery to my theme to highlight the label of the radios when there is an error associated with them.

<?php
$(document).ready(function(){
  $(
'form#node-form div.form-item').each(function(){
    var
result = $(this).find('div.form-radios input.form-radio.error').length;
    if (
result) {
      $(
this).find('label:first').removeClass('error').addClass('error');
    }
  });
});
?>

Eric.London's picture

Here's how you can change the layout of CCK radio widgets to inline using jQuery

<?php
$(document).ready(function(){
  $(
"form#node-form div.form-item div.form-radios div.form-item").css('display', 'inline');
});
?>

In my case, I wanted to change this layout for only one form. Unfortunately, there were not enough unique selectors assigned to the form elements, so I used a jQuery attribute selector to limit the match by the form's action.

<?php
$(document).ready(function(){
  $(
"form#node-form[@action='/MYPATH'] div.form-item div.form-radios div.form-item").css('display', 'inline');
});
?>

Syndicate content