2016-07-22 209 views
1

我是Drupal 8的全新产品。我创建了一个节点category (taxonomy)。现在我不明白如何通过使用nid来获得tid。我正在使用$node->body->value获取正文字段值并使用{{ body }}。但如果我使用$node->field_category->value来获得类别ID,它将在调试模式下显示Null如何在Drupal 8中代表节点ID(nid)获取tid?

function THEME_preprocess_node(&$variables) { 
    $node = \Drupal::routeMatch()->getParameter('node'); 
    $variables['body'] = $node->body->value; // Working fine 
    $cat_id = $node->field_category->value; // Its showing null 
    kint($cat_id); 
} 

因此,谁能告诉我怎样才能得到一个节点的category id (tid)

回答

0

它的值的引用字段可以通过使用target_id而不是值来获得。使用target_id,如果需要,可以加载全部术语。

$tid = $node->field_category->target_id; 
相关问题