1

According to the documentation, these are the available variables:

$items: An array of field values. Use render() to output them.
$label: The item label.
$label_hidden: Whether the label display is set to 'hidden'.
$classes: String of classes that can be used to style contextually through CSS. It can be manipulated through the variable $classes_array from preprocess functions. The default values can be one or more of the following:
field: The current template type, i.e., "theming hook".
field-name-[field_name]: The current field name. For example, if the field name is "field_description" it would result in "field-name-field-description".
field-type-[field_type]: The current field type. For example, if the field type is "text" it would result in "field-type-text".
field-label-[label_display]: The current label position. For example, if the label position is "above" it would result in "field-label-above".

However, I've attempted doing a print_r($items,1) (and the others above) and it seems none of these are actually available to me within my views-view-field--field-download.tpl.php where download is the name of the field I am templating.

Notice: Undefined variable: items in include() (line 12 of /var/www/sites/path/to/templates/views/views-view-field--field-download.tpl.php).

What am I missing?


EDIT:

I also found this documentation, but am having a similar problem. It mentions I should have $fields, $view, and $row... but I am having trouble accessing some of these.

I am able to get some information I need using $row->_field_data["nid"]["entity"]->..., but how do I output the field as it would normally be output? I have conditionals set up and want to output it as it would normally be on one condition, and output something else on another condition. I am able to get the data for the conditions to work properly... but can't figure out how to output the field as it would normally be.

2
  • I assume you are trying to override a view field template?
    – Robin
    Feb 2, 2015 at 19:17
  • @Robin, yes. I have a view with a field named "Download" that I want to use some conditions to determine how it is displayed.
    – Smern
    Feb 2, 2015 at 19:19

1 Answer 1

1

A views field template only has these variables:

 * - $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.

This is the default views field template:

<?php

/**
 * @file
 * 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; ?>

You can see what templates you can use by going to your view, then click "advanced" and then next to "Theme", click on "information". This will show you all available templates you can use and override.

Hope this helps you out.

10
  • Thanks, $output indeed gives me the standard output. However, whenever I attempt to access $field my page is loading up blank... any idea why this would happen?
    – Smern
    Feb 2, 2015 at 19:31
  • That is probably a white screen of death. Are you sure you didn't make any syntax errors in your php?
    – Robin
    Feb 2, 2015 at 19:33
  • Well for example, this line will give me a white screen: print print_r($field, 1);, however if I comment it out I don't get the white screen. Also, where did you get the information on the variable names? I see $fields and not $field here.
    – Smern
    Feb 2, 2015 at 19:34
  • Try var_dump($field);
    – Robin
    Feb 2, 2015 at 19:36
  • Also a white screen. It seems the first comment here is about something similar.. someone below suggests an alternative but uses $fields which doesn't seem to exist and if I use print print_r(array_keys($fields), 1); it complains it's an object instead of an array. Basically without seeing the object I'm having trouble knowing how to access it properly.
    – Smern
    Feb 2, 2015 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.