Drupal core 8.8.3 Version

Using a preprocessor function as below

function subtheme_bs_preprocess_book_tree(&$variables) {
  $variables['page_id'] = code here to read the node.nid variable required;

I can see the node.nid value I need using the kint function on the page.html.twig file, but cannot fathom how to call the variable into my book_tree preprocess function.

I have tried many combinations, and have no idea how to do this.

Image of the 'nid' I would like to access.

For this current page, it's 115.

The node nid - "115" = the current page

  • 1
    A book tree is a list of all the book pages, which are nodes. Which node ID are you looking for?
    – apaderno
    Commented Mar 22, 2020 at 19:04
  • The nid of the current page Commented Mar 22, 2020 at 22:46
  • Short answer: drupal.stackexchange.com/a/145826/89581 Commented Mar 22, 2020 at 23:45
  • Thanks @steve for the link. function subtheme_bs_preprocess_book_tree(&$variables) { $node = \Drupal::routeMatch()->getParameter('node'); $variables['page_id'] = $node->id(); } was the answer to my problem. I will definitely add that page to my favorites. Commented Mar 23, 2020 at 0:24


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.