0

I have two types of nodes: metals and prices.

Metals has two fields:

field_gold

and

field_silver

How to render this fields on node--prices.html.twig (not node--metals.html.twig)

Please, help me!

9
  • what's the relationship between metals and prices, one of them refer to another ??
    – berramou
    Commented Nov 13, 2018 at 10:27
  • No, I did not make any relationship. Commented Nov 13, 2018 at 10:29
  • 2
    I think should be a relationship between the two nodes, otherwise will not know the fields of which metals node should show on which prices.
    – berramou
    Commented Nov 13, 2018 at 10:31
  • Theoretically, if I establish a relationship between them, then it will be possible to display the field of one node in the TWIG(!!!) template of another? Commented Nov 13, 2018 at 10:37
  • Better configure in UI how to display the referenced node. For TWIG see drupal.stackexchange.com/questions/198694/…
    – 4uk4
    Commented Nov 13, 2018 at 10:52

1 Answer 1

1

After you made reference between prices and metals implement hook_preprocess_node like the following:

use Drupal\node\Entity\Node;
/**
 * Implements hook_preprocess_node().
 */
function MyTemplate_preprocess_node(&$variables) {
  if ($variables['node']->bundle() == 'prices') {
    $price_node = $variables['node'];
    //- Get reference target id
    /**
     * @TODO: change field_reference_metals by the referenced field.
     */
    $metals_node_target_id = $price_node->field_reference_metals->getValue()[0]['target_id'];

    //- Load metals referred node
    $metals_node = Node::load($metals_node_target_id);

    //- Set field_gold from metals node
    $variables['field_gold'] = $metals_node->field_gold->value;
    //- Set field_silver from metals node
    $variables['field_silver'] = $metals_node->field_silver->value;
  }
}

In node--prices.html.twig template add:

   {{ field_gold }}
   {{ field_silver }}
  1. Change MyTemplate by your custom theme/module name.
  2. Change field_reference_metals by the field from your prices content type referred to metals.
  3. clear cache.
7
  • Thank you for your time. I did everything as you indicated, but I get an error The website encountered an unexpected error. Please try again later. Error: Class 'Node' not found in unctema_preprocess_node() (line 59 of themes\unctema\unctema.theme). Commented Nov 15, 2018 at 9:29
  • $metal_prices_node = Node::load($metal_prices_node_target_id); Commented Nov 15, 2018 at 9:32
  • 1
    add this use Drupal\node\Entity\Node; on top of your file unctema.theme
    – berramou
    Commented Nov 15, 2018 at 9:32
  • Now site works, but I see error under my node - User error: "value" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core\lib\Drupal\Core\Render\Element.php). and I do not see the value from metal fields... Commented Nov 15, 2018 at 9:36
  • Try with the edit use ->value instead of ->getValue() ?
    – berramou
    Commented Nov 15, 2018 at 9:39

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.