I need to get the id of the node category.
I try this:
$variables['tid'] = $node->field_category->target_id;
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 community
I wrote this answer thinking the question was for Drupal 7. This is not accurate for Drupal 8.
If you debug the $node object, you will see that it's more complicated than that. It has a language and probably a value index. So, you could access the TID this way:
$variables['tid'] = $node->field_category[LANGUAGE_NONE]['target_id'];
But, don't do it that way. It would be better to use EntityMetadataWrapper. The code should look something like this:
$wrapper = entity_metadata_wrapper('node', $node); $variables['tid'] = $wrapper->field_category->raw();
If you want the whole term object, you can do this:
$variables['term'] = $wrapper->field_category->value();