Home Closet A non-trivial task for the Drupal Node Referencefield

A non-trivial task for the Drupal Node Referencefield

by admin

One of the most useful fields that the CCK module provides is Node Reference Its task is trivial and straightforward – to relate the content of the site with relationships, which is easy to understand from the examples :

  • A company that must have references to Workers in its content
  • Soccer player, with links to his Stats node

The problem I have had to face is how to show this field :

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

The CCK by default handles multiple or single node reference selections by itself, either as a list or just a reference to the corresponding node. The recipe output task assumes that the recipe and ingredient are nodes.
Accordingly, when rendering the content with Views` we get :

Bulgarian Chicken(Recipe)

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

Instead of :

Bulgarian Chicken(Recipe)

  1. 1 chicken(Ingredient)
  2. 2 orange(Ingredient)
  3. 50g spices(Ingredient)

Unfortunately I found no solution to this problem on Google and Drupal.org, so I had to twist, and it turned out rather simple and beautiful.
For the Product node, we add another multi-field Text, which we put after Node Reference in Fields of the view.
And in the template views-view-fields.tpl.php (or a specific view as you want) write the magic :
$nids = array();
<?phpforeach ($fields as $id => $field): ?>
if($id == 'field_product_nid') {
$nids = array_shift($field-> handler-> field_values);
if($id == 'field_product_title_value') {
$items = array();
$index = 0;
A non-trivial task for the Drupal Node Referencefields = array_shift($field-> handler-> field_values);
foreach(A non-trivial task for the Drupal Node Referencefields as A non-trivial task for the Drupal Node Referencefield) {
$items[] = l(A non-trivial task for the Drupal Node Referencefield['value'], 'node/' . $nids[$index++]['nid']);
// TODO: theming here
$field-> content = theme('item_list', $items);
<?php if (!empty($field-> separator)): ?>
<?php print $field-> separator; ?>
<?php endif; ?>
<<?php print $field-> inline_html;?>class="views-field-<?php print $field-> class; ?> ">
<?php if ($field-> label): ?>
<?php print $field-> label; ?> :
<?php endif; ?>
// $field-> element_type is either SPAN or DIV depending upon whether or not
// the field is a 'block' element type or 'inline' element type.
<<?php print $field-> element_type; ?> class="field-content"> <?php print $field-> content; ?> </<?php print $field-> element_type; ?> >
</<?php print $field-> inline_html;?> >
<?php endforeach; ?>

This will allow you to render the view by substituting the Node Reference field title. Do you have a solution for this problem?

You may also like