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!
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityAfter 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 }}
MyTemplate
by your custom theme/module name.field_reference_metals
by the field from your prices content type referred to metals.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).
Nov 15, 2018 at 9:29
use Drupal\node\Entity\Node;
on top of your file unctema.theme
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...
Nov 15, 2018 at 9:36
metals
andprices
, one of them refer to another ??metals
node should show on whichprices
.