Working with Views can be a little confusing for anyone new to Drupal. Don't get me wrong, Views is an absolutely brilliant piece of programming (thanks merlinofchaos!), but there are so many options that finding the correct toggle can be daunting. And no matter how brilliant, eventually you'll discover you've outgrown all available options in rewriting and variable usage and you need to figure out how to modify values via php. The answer? Views templates. And Views helps you out there too!
And How
For this example we are going to focus on one specific problem: creating a list of image thumbnails.
In our hypothetical we have extended a node type via cck to include an image upload called [field_image_value] that we display when viewing the full node. When each node is created a thumbnail is auto-created via the IMCE Plugin on upload. We've given them a suffix of _thumbnail such that our-image-filename.jpg becomes thumbnail our-image-filename_thumbnail.jpg.
For our block output we want something like this for each node retrieved:
Node Title
Node Teaser, limited to two hundred characters...
Sounds easy right? Actually, yes it is, as long as you know the correct steps.
Step 1: Construct View
Construct your view so that you have [field_image_value], [title] and [teaser] as fields. For [field_image_value] and [title] check the last option, Link this field to its node.
Open [field_image_value], which contains the path to the large image and check Rewrite the output of this field and type the following:
Add an argument to limit your view results to just the node type you have created with an image cck field. Add a block display and save your view.
Now if you add your block to a page you should see we everything you want except all the images are fullsize. Time for view templates.
Step 2: Determine view name
Return to your view and under Basic Settings, click Theme:
Below, you'll see all the possible template file names you could use to theme your view. In this example, we are interested in theming a specific field when it appears inside of a specific view (not every instance) so we'll want to look under our [field_image_value] for the correct template which should follow this format: views-view-field--[Our_View_Name]--[field_image_value].tpl.php
So, for Our_View_Name the correct filename would be:
views-view-field--Our-View-Name--field-image-value.tpl.php
Step 3: Make your template
So we're not starting from scratch copy /modules/views/theme/views-view-field.tpl.php to /themes/yourtheme/views-view-field--Our-View-Name--field-image-value.tpl.php
Now, if you revisit your view, open Basic Settings > Theme:
Opening this template you'll see it's pretty basic. Out of the box, we're just passing a variable along, but Merlin has provided some documentation to assist with more complicated theming:
<?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; ?>
Our situation is pretty straight-forward, we'll just add a tiny bit of php to replace ".jpg" with "_thumb.jpg" for our [field_image_value].
<?php print str_replace(".jpg", "_thumbnail.jpg", $output);?>
Save your template, and flush the Theme Registry cache. You're done!
If you revisit the page displaying your block, you'll see the thumbnails are now displayed.
But what if I don't?
Here are a few gotchas and possible solutions:
- If the image displays as an error then there is likely a typo in your template
- If the original image displays and not the thumbnail make sure you have rescanned your templates and try flushing all your caches. Also, check that your browser isn't caching, if using in most browsers you can use CTRL-R to do a complete refresh
- If your administration theme is different than your production theme, then your templates will not reflect the changes inside of the views previews
Comments