如何从引用节点drupal 8添加字段

时间:2016-06-19 23:17:18

标签: drupal drupal-theming drupal-8 drupal-entities

我的内容类型包含一个对另一个节点的引用的字段。我正在尝试在主节点的页面中包含引用节点的字段,但我无法弄清楚如何添加它。以下是我在主题中获得价值的方式:

function mytheme_preprocess_node(array &$variables) {

  $node = $variables['node'];
      if ( $node->get('field_testimonial') ) {
          $referenced_nodes = $node->get('field_testimonial')->referencedEntities();
          if ( count($referenced_nodes) > 0 ){
            $referenced_node = $referenced_nodes[0];
            //this is providing the value I want.  how can I add that back to my page?
            error_log($referenced_node->body->value);
          }
      }
}

请帮助我将该值添加回我的变量,以便我可以在我的主题中使用它!谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

你为什么要使用代码?它可以配置表单管理员 转到admin / structure / types / manage / article / display并管理您希望的格式以显示参考节点。 谢谢

答案 1 :(得分:1)

只需执行以下操作

$variables['referenced_body'] = $referenced_node->body->value;

在你的Twig模板中,你可以这样做:

{{ referenced_body }}