Pass additional variable into existing twig template (Book module) I use the Drupal 8 Book module for books where each chapter has a different author. 

The Book Navigation block uses book-tree.html.twig (core/modules/book/templates) to generate a book tree showing chapter titles. I want to show author names also.

I can subclass and edit book-tree.html.twig, but I don't know how to get the author name passed in so I can use it. How do I go about this? Only the title, the url, and some flags are passed in now.

As an alternative, I can get the id for each chapter node from its url as a string. Can I use this id to load the chapter node from within the twig template? If so, I can get the author name from the node directly.
Drupal offers preprocess functions where additional variables for templates can be set up.

The naming convention is THEMENAME_preprocess_THEMEHOOK(). In your case, you want to add a variable for book-tree.html.twig. The machine name for this is the first part of the template, with underscores replacing hyphens. In this case it would be book_tree. So your function would be THEMENAME_preprocess_book_tree().

Preprocessors receive a single argument, an array. The keys of this array become the variables available in your twig template.

THEMENAME_preprocess_book_tree(&amp;$vars) {
  $vars['book_author'] = some_function_that_gets_the_author();
}


Now the variable book_author will be available to your template.