I have the following snippet.

function <themename>_preprocess_node(&$vars) {
  if ($vars['teaser'] && !$vars['is_front']) {
    $vars['theme_hook_suggestions'][] = 'node__teaser';
  elseif ($vars['teaser'] && $vars['is_front']) {
    $vars['theme_hook_suggestions'][] = 'node__teaser__front';

  if (!$vars['teaser'] && count($vars['field_related']) > 2) {
    $vars['theme_hook_suggestions'][] = 'node__alt';
    foreach (element_children($vars['content']['field_related']) as $k) {
      $vars['content']['field_related'][$k]['#attributes']['class'][] = 'grid_1';

Basically, I have this field_related in my node that holds from 0 to 3 values. If it holds more than 2 values, I change the template suggestion (node__alt) and I'd like to attach a class (grid_1) to each of the values. I've spent the last 4 hours researching but nothing got me even near to where I want to go.

Do you have any idea?

EDIT In the template file I did like this:

<aside id="related">
  <?php foreach(element_children($content['field_related']) as $k) : ?>
    <div class="grid_1">
      <?php echo render($content['field_related'][$k]) ?>
  <?php endforeach ?>

It works, but it is far from being clean.

  • It did work for me, I was hoping for a cleaner solution. This seems just a dirty hack to me.
    – Morpheu5
    Commented Jan 6, 2012 at 21:17

1 Answer 1


Make a copy of your_drupal_site/modules/field/theme/field.tpl.php in your theme directory and name it field--field_related.tpl.php In this file You can attach the class (grid_1) to each field as follows:

<?php foreach ($items as $delta => $item) : ?>        
<div class="grid_1"><?php print render($item); ?></div>
 <?php endforeach; ?>

All the available variables are listed in the comments of this file so you will be able to customize as you like.

On any given node you can check how many fields have been created and theme according to that. You can change the above code to count how many fields are there on any node, the revised code is as follows:

<?php foreach ($items as $delta => $item) : ?> 
    if (count($items) > 2) {
        print '<div class="grid_1">' . render($item) . '</div>';
    } else {
        print '<div class="two_or_less">' . render($item) . '</div>';
<?php endforeach; ?>

This will add class "grid_1" to any node with 3 or more fields of field_related and add class "two_or_less" to the nodes with 1 and two fields.

  • This would work, except I need to attach the class only on certain cases, depending on some conditions on the fields (in this case, how much values are in the field_related field). Your solution attaches the class (well… wrap the field in another div with a single class but that's my non-solution too) to the items regardless of any condition.
    – Morpheu5
    Commented Jan 8, 2012 at 9:45
  • I edited my answer to include the condition to check number of fileds. Thanks.
    – 15dk51
    Commented Jan 8, 2012 at 20:44
  • Ok, that was minor, though, since I have other conditions that may be harder to meet. I need to trigger this behaviour depending on the node template (e.g. I have node.tpl.php and node--alt.tpl.php that I set via suggestions) and still the field is wrapped, giving one unnecessary div.
    – Morpheu5
    Commented Jan 10, 2012 at 11:23
  • This was an example to show you could add div or not add based on how many fields are attached to the node. Your question was "If it holds more than 2 values, I change the template suggestion (node__alt) and I'd like to attach a class (grid_1) to each of the values." So in this file you can check if it holds more that 2 values you can add the div class then it would go to your new template as you have setup. If not in the else you can just print render($item) and it will print the filed without any div. Thanks
    – 15dk51
    Commented Jan 10, 2012 at 14:54

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.