0

I'm using the Book module to maintain the parent-child relationship between content. I want to see all child pages expanded when I see a parent node details page. How can I achieve it?

I tried with THEMENAME_preprocess_book_tree(), but so far I have not been able to achieve that I wanted.
Is this the right hook to use in my case?

3
  • Do you mean just list child pages titles? Isn't there a block with Book navigation that already does it? Just place it in a region that makes sense to you, below main content for example. – prkos Jan 8 '19 at 0:07
  • @prkos I want all contents inside child pages, not just title. – arun Jan 8 '19 at 4:06
  • 1
    Does it have to be a code solution? You can use Views to create a Block with full child content listed on parent book pages. You also don't have to use the Book module for parent-child relationship, a more general way is to use Entity reference field. – prkos Jan 8 '19 at 17:35
3

I managed to find a solution. I would like to post it here if somebody looking for similar solutions.

Make a view for the parent page with all needed configurations. In my case, I have the main chapter page as the parent, the content type is 'chapter'. And when I see the view in a page I want to see all subchapters coming under it. The further steps are followed:

  1. Browse to /admin/structure/views and click on "Add view".
  2. Enter a view name, Select content type from view settings
  3. Check the "Create a block" checkbox only. Then click on "Save & edit".
  4. On the right-hand side, click on "Advanced" so that the advanced menu appears.
  5. Click on "add" next to "Relationships" to add a relationship.
  6. Select "1st parent" (the parent book node) and click on "Apply and configure relationships".
  7. Check the "Require this relationship" checkbox and click on "Apply".
  8. Click on "add" next to "Contextual filters" to add a contextual filter.
  9. Select "ID" (The node ID) and click on "Add and configure contextual filters". Choose relationship to "Book 1st Parent"
  10. In the "when the filter value is not available" section, select "Provide default value" and in the "Type" drop-down menu, select "Content ID from URL"
  11. click on "Apply" & Save the view
  12. Place the just created block in the page by visiting structure > block layout
2
  • This was exactly what I needed - in my case, a View that shows the child nodes of a Book when the View itself is displayed on a Book parent page. It's a shame it's this convoluted now though - in D7, there was a simple contextual filter called 'Book: All sub nodes of this book page.' Much easier! – Timmah Apr 14 at 0:19
  • Thanks for sharing. I had to replace the core functionality of book tree navigation (the one under book nodes) with a view block to place it in a Layout Builder section and make a custom layout for book pages. I followed your instructions, but to make it work, in my case I chose "Parent" instead of "Book 1st Parent" to show all children of the passed parent page. Thank you very much! – Cheope Apr 22 at 11:31

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.