私は_Drupal 8
_の新人です。 category (taxonomy)
でノードを作成しました。 tid
を使用してnid
を取得する方法がわかりません。私は_$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)
を取得するにはどうすればよいですか?
参照フィールドには、プロパティvalue
がありません。 entity
を使用して用語をロードするか、target_id
用語IDを取得するには:
$tid = $node->field_tags->target_id;
次のようにアイテムをループできます:
foreach ($node->field_category as $item) {
$tids[] = $item->target_id;
}
または、次のようなエンティティを取得します。
$terms = $node->get('field_category')->referencedEntities();