0

How do I access the nid of the current node in a field.html.twig template?

1

I found myself a solution for this. Maybe not the perfect way to go, but it worked very well for my needs:

in .theme-file:

function MYTHEME_preprocess_field(&$variables, $hook) {
   $variables['nodenid'] = \Drupal::routeMatch()->getRawParameter('node');
}

and in .twig:

{{ nodenid }}

of course you might want to filter the preprocess hook to only add this variable to a specific field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.