How can I get the tid (term id) using the id of my node in .theme I need to get the id of the node category.

I try this:

$variables['tid'] = $node-&gt;field_category-&gt;target_id;
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-&gt;field_category[LANGUAGE_NONE][0]['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-&gt;field_category-&gt;raw();


If you want the whole term object, you can do this:

$variables['term'] = $wrapper-&gt;field_category-&gt;value();