📜 ⬆️ ⬇️

Nontrivial task for Node Reference Drupal fields

One of the most useful fields that the CCK module provides is the Node Reference . Its task is trivial and clear - to link the content of the site with relationships, which is easy to understand with examples:

The task I had to deal with is how to show this field:


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

Accordingly, when rendering content by 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. 50g Spices (Ingredient)

Unfortunately, I did not find the solution to this task on Google and Drupal.org, and therefore I had to get out, and it turned out to be quite simple and beautiful.

For the Product node, we add another multipole Text, which we set 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 magic:

<?php
$nids = array();
?>
<?php foreach ($fields as $id => $field): ?>
<?php
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);
}
?>
<?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; ?>
<?php
// $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 replacing the title of the Node Reference field. Do you have a solution for this problem?

Source: https://habr.com/ru/post/82471/


All Articles