0

I want to use a field template for all the fields on my "journal_page" content type that use a widget of "long text".

How would I do this?

N.B. My field is not named "long text". "Long text" is the name of the widget I am using (It is a text field). I want to use the same template file for multiple differently named fields. The only thing they have in common is the widget type.


Steps I have tried

On Drupal Theming suggestions it says you must use the following format: field--[type|name[--content-type]|content-type].tpl.php, so:

  1. field--field-name--content-type.tpl.php
  2. field--content-type.tpl.php
  3. field--field-name.tpl.php
  4. field--field-type.tpl.php

I tried the following but, none have worked:

  1. field--long-text--journal-page.tpl.php
  2. field--long_text--journal_page.tpl.php

2 Answers 2

1

I just happened upon this question while trying to remember how to solve the same problem. In this case, Drupal simply doesn't do what you're asking for by default. There are two ways to approach it, one by accepting Drupal's default behaviour, and one by overriding that default.

Default behaviour

As has already been pointed out here, Drupal's template suggestions for fields don't consider the field type (though they do accommodate the content type).

Living with Drupal's default behaviour

This is the simplest solution, though it's a bit inflexible. Since your goal is to use only one template, what you can do is this:

  1. Create a field template in our theme according to the field--field-name--content-type.tpl.php pattern. This will customize one of your fields.
  2. Create symlinks to your original template containing the names of the other fields you need to customize.

For example, given fields with names lorem, ipsum, dolor and sit, and the content type contenttype, you'd create one template:

  • field--lorem--contenttype.tpl.php

Then, you'd create the following symlinks to that template:

  • field--ipsum--contenttype.tpl.php,
  • field--dolor--contenttype.tpl.php,
  • field--sit--contenttype.tpl.php

Drupal will see these as four different but identical files.

Overriding Drupal's default behaviour

The previous solution is quick and simple, but slightly unsatisfying. If Drupal's so flexible, we should be able to handle this problem using the Drupal API, right?

Well, it turns out we can. It's possible for us to add our own template suggestions.

We do this like this:

1. Implement template_preprocess_field()

/**
 * Implements template_preprocess_field().
 */
function THEMENAME_preprocess_field(&$variables) {
  if ($variables['element']['#bundle'] === 'contenttype' && $variables['element']['#field_type'] === 'text_long') {
    $variables['theme_hook_suggestions'][] = 'field__contenttype__text_long';
  }
}

2. Copy field.tpl.php to your theme

Copy field.tpl.php from modules/field/theme/field.tpl.php to sites/all/THEMENAME/field--contenttype--text-long.tpl.php and then customize to suit.


Notes

  • The logic in the template_preprocess_field() implementation could be changed in several ways, e.g. to match only by field name in a given content type instead of all examples of a field type in that content type.
  • The process is almost the same when doing this task in a module except that you'll also need an implementation of hook_theme() in the module to make it work.
  • It would be possible to to this using only theme_field(), but theme functions are deprecated in Drupal 8, so this is the method that we'll all be using going forward.
0

UPDATE:

Try:

field--text_long.tpl.php

The naming above will tackle all fields that are created as "Long Text"

Tested field--text_long.tpl.php a moment ago with the following. Note, I wrapped everything with H1 markup to test.

<h1><div class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if (!$label_hidden): ?>
<div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</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></h1>

Removed all previous to clear confusion

Helpful links:

Field Types and Settings - Provides widget detail among others

D7 Template Suggestions - Provides naming conventions among others

8
  • @webhouse My field is not named "long_text". "Long text" is the name of the widget I am using (It is a text field)
    – big_smile
    Commented Mar 28, 2015 at 19:08
  • Type of widget doesn't matter. As long as you're referencing the field name properly, you'll be able to use the field template.
    – webhorse
    Commented Mar 28, 2015 at 19:12
  • @webhouse I want to use the same template file for multiple differently named fields. The only thing they have in common is the widget type.
    – big_smile
    Commented Mar 28, 2015 at 19:26
  • @big_smile I updated the answer to provide more
    – webhorse
    Commented Mar 28, 2015 at 20:12
  • Thanks for your continued help. I have 10 fields on my content type. I want them to all use the same template file (so I don't have 10 tpl files to maintain). Is this possible? The only thing these fields have in common is that they are text fields and use the long text widget. I tried field--field_long_text--journal_page.tpl.php and cleaned the cache, but it doesn't work. (The fields are not called long-text, but I don't want to use the actual names of the 10 fields in the tpl file name, because then I will end up with 10 different tpl files).
    – big_smile
    Commented Mar 28, 2015 at 21:48

Your Answer

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

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