Today's Deals (in


7 - Node reference field display
  • Jose A.

    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?

  •  Answers:

  • Jose A.

    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;