1
votes

I know this is probably a n00b question but I've searched everywhere for an answer and havent found anything.

I have a CCK multiple value field for "Features" where a product can have a random number of multiple features entered for it. I am editing the view so I can style the output of the features on the product page.

Right now in my view I can output the entire list of features at once using:

<?php print $fields['field_features_value']->content ?> 

This will give me a list all the features given for a product. But what I want to do is loop through and pull out each individual feature and format/style it separately. How exactly would I do this?

2
Well I thought this would be a common question but I've searched everywhere and I still havent found how to do this with Views row theming.Justin

2 Answers

0
votes

I ran into this yesterday again, and spent hours trying to Google the syntax, to no avail.

I was able to get this to work, but I must admit it is not the best way. It is duplicating some of the work that Views has already potentially done for us, and should be considered a brute-force approach.

My use case involved theming each filefield file in a node separately, per node-based row:

<?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

$output = explode('|', $output); // I've rewritten the field output in Views like this: [field_portfolio_image-field_name]|[nid]
$paths = $output[0]; // I've set filefield to show file paths rather than the file
$nid = $output[1]; // The NID is all that's really needed for this approach

$node = node_load($nid);
$slots = $node->field_portfolio_image;

foreach($slots as $prop) {
        print '<a href="'.$prop[filepath].'" title="'.$prop[data][description].'" rel="gallery-'.$nid.'" class="colorbox hidden">'.$prop[data][description].'</a>';
    }

?>

I used the Devel module heavily here (image reference for this example attached), in order to get the nested values I needed.

Devel view of fields used

I know there is a better, proper way of doing this, rather than reloading the node data, since views should already have access to this upon page load.

0
votes

When theming views is too specific I set conditions, relationships, parameters and all the views stuff except fields. The only field I use is node id.

Then when doing the theming I use...

$node = node_load($nid);

... to get the node object. You can inspect the node object with the dpm function that comes with the devel module.

dpm($node);

This "technique" works well for nodes and when you don't care about optimization or speed, because if you are going to do this with 1000 nodes you should load the nodes on batch.