A nontrivial task for the Node Reference Drupal field

    One of the most useful fields that the CCK module provides is the Node Reference . Its task is trivial and understandable - to connect the site content with relationships, which is easy to understand with examples:
    • A company that should have links to Workers in the content
    • Football player, with a tie on his Statistics node

    The task that I had to face is how to show this field:

    • Recipes, which consist of various ingredients (1 chicken, 2 oranges, 50g spices)

    By default, CCK itself draws up multiple or single node reference selection as a list or just a link to the corresponding node. The task of finding a recipe assumes that the recipe and ingredient are nodes.

    Accordingly, when rendering content with Views, we get:

    Bulgarian Chicken (Recipe)

    1. Chicken (Ingredient)
    2. Orange (Ingredient)
    3. Spices (Ingredient)

    Instead:

    Bulgarian Chicken (Recipe)

    1. 1 chicken (Ingredient)
    2. 2 orange (Ingredient)
    3. 50gr of spices (Ingredient)

    Unfortunately, I did not find a solution to this problem on Google and Drupal.org, and so I had to get out, and it turned out pretty simple and beautiful.

    For the Product node, add another Text multipole, which we put in the Fields views after the Node Reference.

    And in the views-view-fields.tpl.php template (or a specific view as you need), we write the magic: This will render the view rendered by changing the title of the Node Reference field. Do you have a solution for this problem?

    $nids = array();
    ?>
    $field): ?>
    if($id == 'field_product_nid') {
    $nids = array_shift($field->handler->field_values);
    continue;
    }

    if($id == 'field_product_title_value') {
    $items = array();
    $index = 0;
    $titles = array_shift($field->handler->field_values);
    foreach($titles as $title) {
    $items[] = l($title['value'], 'node/' . $nids[$index++]['nid']);
    }
    // TODO: theming here
    $field->content = theme('item_list', $items);
    }
    ?>
    separator)): ?>
    separator; ?>


    <inline_html;?> class="views-field-class; ?>">
    label): ?>
    label; ?>:


    // $field->element_type is either SPAN or DIV depending upon whether or not
    // the field is a 'block' element type or 'inline' element type.
    ?>
    <element_type; ?> class="field-content">content; ?>element_type; ?>>
    inline_html;?>>




    Also popular now: