field.tpl.php Default template implementation to display the value of a field.
This file is not used and is here as a starting point for customization only.
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".
Other variables:
- $element['#object']: The entity to which the field is attached.
- $element['#view_mode']: View mode, e.g. 'full', 'teaser'...
- $element['#field_name']: The field name.
- $element['#field_type']: The field type.
- $element['#field_language']: The field language.
- $element['#field_translatable']: Whether the field is translatable or not.
- $element['#label_display']: Position of label display, inline, above, or hidden.
- $field_name_css: The css-compatible field name.
- $field_type_css: The css-compatible field type.
- $classes_array: Array of html class attribute values. It is flattened into a string within the variable $classes.
See also
File
modules/field/theme/field.tpl.phpView source
- <?php
-
- /**
- * @file field.tpl.php
- * Default template implementation to display the value of a field.
- *
- * This file is not used and is here as a starting point for customization only.
- * @see theme_field()
- *
- * 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".
- *
- * Other variables:
- * - $element['#object']: The entity to which the field is attached.
- * - $element['#view_mode']: View mode, e.g. 'full', 'teaser'...
- * - $element['#field_name']: The field name.
- * - $element['#field_type']: The field type.
- * - $element['#field_language']: The field language.
- * - $element['#field_translatable']: Whether the field is translatable or not.
- * - $element['#label_display']: Position of label display, inline, above, or
- * hidden.
- * - $field_name_css: The css-compatible field name.
- * - $field_type_css: The css-compatible field type.
- * - $classes_array: Array of html class attribute values. It is flattened
- * into a string within the variable $classes.
- *
- * @see template_preprocess_field()
- * @see theme_field()
- *
- * @ingroup themeable
- */
- ?>
- <!--
- THIS FILE IS NOT USED AND IS HERE AS A STARTING POINT FOR CUSTOMIZATION ONLY.
- See http://api.drupal.org/api/function/theme_field/7 for details.
- After copying this file to your theme's folder and customizing it, remove this
- HTML comment.
- -->
- <div class="<?php print $classes; ?>"<?php print $attributes; ?>>
- <?php if (!$label_hidden): ?>
- <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>: </div>
- <?php endif; ?>
- <div class="field-items"<?php print $content_attributes; ?>>
- <?php foreach ($items as $delta => $item): ?>
- <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
- <?php endforeach; ?>
- </div>
- </div>
-
Comments
Docs
PermalinkIt would be nice if what you needed to rename the templates to was noted in the docs/comments. Maybe I missed it, but it's hard to find.
Custom field.tpl.php
PermalinkI have just started to delve into field level theming (because D7's render is so nice) so take this with a grain of salt but, my convention (verified) is.
Field: field_page_photos
File: /mytheme/anyfolder(optional)/field--field-page-photos.tpl.php
Cache: Clear it.
Note, that...
$node object and field $item inconsistency
PermalinkWhy is it that (with a text field, for example) in the field template I get $item['#markup'] only, where in the $node object I have $node->myfield['und'][0]['value'], $node->myfield['und'][0]['safe_value'], etc?
Is there the possibility of getting a non-safe value when using the field template?
Possible override templates
PermalinkPossible override templates are:
include this in comments of the template files
PermalinkI would like to see this helpful list included in the comments of the template files.
for example: /modules/field/theme/field.tpl.php
you only need one
Permalinkyou only need one field.tpl.php to get rif of the div crazyness in the output of drupal ...
<?php foreach ($items as $delta => $item) : ?><?php print render($item); ?>
<?php endforeach; ?>
instead of using a lot of field template files ...
we know this concept from views and it was/is the total wrong direction ...
complicate solutions for simple tasks ... the node ['view'] object was much easier to handle in a lot of use cases ...
field level over ride templates for views
PermalinkIs it possible to over ride the default theme_field() for a specific view ?
I can over ride all fields by putting the field.tpl.php file in my themes folder, but I want to customize only for certain views.
For specific fields, the structure appears to be this:
themes/seven/field--contentfieldname.tpl.php
For specific fields on specific content types, it appears to be this:
themes/seven/field--contentfieldname--contenttypename.tpl.php
How would I override the field type only for a specfic view?
thanks
Views has its own template system
PermalinkBecause views essentially takes entities apart and joins fields accross enttities, it has its own field templates. Checkout the "theme" folder in the views module and find the field template and copy it to your theme. Hit the "Theme information" button under the advanced section in views to get a list of what to rename your copy of the template to to take over the display of different fields.
Template suggestions
PermalinkFor general template suggestions, including this list, see: http://drupal.org/node/1089656
Theming and output of dates
PermalinkI wasn't sure where to drop this tip but I think it would be useful to other folks.. there's a great example of code here:
http://drupal.org/node/1108164#comment-5111780
[snippet]
<?php$field_name
='field_date';$items = field_get_items('node', $node, $field_name);
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, $node->type);
$item=$items[0]; //Just display the first date
$display=field_get_display($instance, 'default', $node);
$display['settings']['format_type']='short'; //Alter the display
$vars['dates'] =date_formatter_process('date_default', 'node', $node, $field, $instance, $langcode, $item, $display);
$output=theme('date_display_combination', $vars);
?>
Regarding the Countdown Timer Field
Permalinkhi,
Now is there any way that a field which is a timer (provided by the great great Countdown Timer Field module (http://drupal.org/project/field_countdown)) would start only on a flag click on that node