Theming multiple value CCK fields in Views
I’m working on a site right now that has a Publications content type, which in turn has a multiple value CCK text field for Author. If more than one author was input I needed them to display as a comma separated list in a view. How do you go about modifying this? My first stop was the theming info link in Views, and that helped me narrow it down to a template file to use, views-view-field.tpl.php(). But that file didn’t allow me to modify the individual fields without getting a whole load of other HTML in the bargain.
< ?php // $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $ /** * This template is used to print a single field in a view. It is not * actually used in default Views, as this is registered as a theme * function which has better performance. For single overrides, the * template is perfectly okay. * * Variables available: * - $view: The view object * - $field: The field handler object that can process the input * - $row: The raw SQL result that can be used * - $output: The processed output that will normally be used. * * When fetching output from the $row, this construct should be used: * $data = $row->{$field->field_alias} * * The above will guarantee that you'll always get the correct data, * regardless of any changes in the aliasing that might happen if * the view is modified. */ ?> < ?php print $output; ?> |
Using the brilliant “Theme developer” extension of the Devel module I was able to pin point the theme function – theme_content_view_multiple_field that would allow me to modify the actual value of the field without the wrapper div’s etc (I’m not getting into the views divitis argument.
Here’s a quick snippet of code to place in your template.php file of your theme to allow you to modify multiple value fields:
function custom_theme_content_view_multiple_field($items, $field, $values) { $output = ''; switch($field['field_name']) { // If this is the author field then we need to comma separate the authors case 'field_author': $output .= '<div class="field-item">'. implode(', ', $items) .'</div>'; break; default: foreach ($items as $item) { if (!empty($item) || $item == '0') { $output .= '<div class="field-item">'. $item .'</div>'; } } break; } return $output; } |
I used a switch statement because I have a lot of other multiple fields that need to be themed in different ways and it’s nicer than a whole load of if statements. Also switch provides a default state if a condition isn’t met.
November 11th, 2009 at 2:48 am
Thank you! saved a lot of time :)
January 5th, 2010 at 3:58 pm
This was extremely helpful for a site I’m working on – although I’m not sure why there isn’t a simple template file for this (would be easier for me to wrap my head around it!).
Thanks!
March 24th, 2010 at 12:37 pm
You have to take into consideration that if only one value is stored in the multiple value field the _content_view_multiple_field is not called because the field is not multiple for views.
In this case you have to use custom_theme_text_formatter_default for example (in case that you cck field type was text).
April 7th, 2010 at 9:31 pm
Hi guys, there is a very helpful module for this called custom formatters. I just solved this very problem with it. It would be great if content_formatters had a disk-based content templates, but even now it allows custom PHP formatters for any field, including multiple-valued ones. There are samples you can clone to get started presto.