1

I have a content type with several fields, some of them are node reference fields displaying multiple values. I've configured the display of one node reference field like teaser showing two fields "code, description". The problem is when the node reference has more than one value, keeps adding lines to the bottom. I want to display this node reference field like "code1/code2 description1/description2" in one line.

How can I achieve this? What template should I override, field.tpl.php or node.tpl.php?

2
  • 1
    Presumably you've tried both and have found one/both do not work? Please elaborate on what happened when you tried both template files yourself, we're not here to do your testing for you :)
    – Clive
    Commented Feb 18, 2013 at 10:16
  • 1
    Hi! Of course not! :) yes, i have tried field.tpl.php but i didn't find out how to control each field of the node reference field. i just can print the whole output of a line next to each other.. Also i think this is not the best way of doing it, i would like to know if there is a better way. In node.tpl.php i can't access the fields..
    – Jose A.
    Commented Feb 18, 2013 at 10:29

1 Answer 1

1

After a lot of research i finally solved my problem. I don't know if this is the best way of doing it but it works for me.

I added a THEME_field() function to my template.php of the desired field.

Inside my template function i removed the even/odd css classes because i want all in a single line. Probably exists a better way to access field values, this was the solution i found. Digging the $item array i was able to get my code and color description values.

If someone knows a better way of doing this please help me improve it. There goes the code i hope it helps someone else. Don't forget to replace THEME by your theme name and the FIELDNAME.

function THEME_field__FIELDNAME($variables) {
$output = '';

// Render the label, if it's not hidden.
if (!$variables['label_hidden']) {
  $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
}

// Render the items.
$output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
$codes = "";
$colors ="";
foreach ($variables['items'] as $delta => $item) {

    $code = $item['field_color_description']['#object']->title_field['und'][0]['value'];
    $color = $item['field_color_description']['#object']->field_color_description['und'][0]['node']->title;

    $codes .= $code."/";
        //avoid repeated color descriptions
        if (strpos($colors,$color) === false)
        {
        $colors .= $color."/";
        } 
     }
     $classes = 'field-item even';

     $codeAndColors = rtrim($codes,"/") . " ". rtrim($colors,"/");
     $output .= '<div class="' . $classes . '"' . ' title="'. $codeAndColors.'" '. $variables['item_attributes'][$delta] . '>' . $codeAndColors . '</div>';
     $output .= '</div>';

     // Render the top-level DIV.
     $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';

     return $output;

}

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.