1

I'm trying to use drupal_block("book_navigation") in a twig template (with the twig_tweak module installed) to display a list of chapters for a book page. But I'm getting a list of all books and their chapters.

Is there a way to use drupal_block("book_navigation") to produce a list of chapters for just the one book, maybe by passing the id of the book node as a second parameter? Or some other way?

Compare to the user interface /admin/structure/block. There, when you place a Book Navigation block in a region, you can choose the configuration option "Show block only on book pages". This causes the block to produce a list of chapters for only the book whose page the list appears on. So it's clearly possible to do it using the book navigation block. Just need to know how.

0

2 Answers 2

1

After digging into the Book module code, I discovered that in a twig template (with the twig_tweak module installed) I can use

    {{ drupal_block("book_navigation", {block_mode: "book pages"}) }}

to create a list of chapters for only the current book (which is what I need). This is equivalent to the configuration option "Show block only on book pages". Probably in documentation somewhere, but much googling didn't find it for me.

0

In your twig file:

{{ drupal_block('my_block', {tab_data: "Results"}) }}

In your Myblock.php file

public function defaultConfiguration() {
  return [
    'tab_data' => NULL,
  ];
}

public function build() {

  return [
    '#theme' => 'my_theme',
    '#tab_data' => $this->configuration['tab_data'],
  ];
}

we can access the tab data in the build function of the block through the configuration variable.

Your Answer

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

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