【问题标题】:How to get tid on behalf of node id (nid) in Drupal 8?如何在 Drupal 8 中代表节点 id (nid) 获取 tid?
【发布时间】:2016-07-22 07:17:58
【问题描述】:

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

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)

【问题讨论】:

    标签: drupal-modules drupal-theming drupal-8 drupal-taxonomy


    【解决方案1】:

    可以通过使用target_id而不是value来获取引用字段的值。如果需要,您可以使用 target_id 加载整个术语。

    $tid = $node->field_category->target_id;
    

    【讨论】:

      猜你喜欢
      • 2013-09-25
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      相关资源
      最近更新 更多