Dynamic book menu/book menu default configuration options

Created on 16 February 2022, almost 3 years ago
Updated 4 January 2024, about 1 year ago

Problem/Motivation

I want to start by saying thank you for creating this module. It really helped our organization. But, we needed a few things that the module didn't have. Since we are setting this block on the content type, using Layout Builder, we wanted to have the option to only show the book menu for the current book page. Our organization could potentially have 5 or 6 different books, and we didn't want them showing up in the menu all the time.

We also needed to have an option to display the book as the default configuration. The un-expanded view wasn't going to fit our needs.

Proposed resolution

I added both of these options to the attached patch.

I created a new option called "book_menu_default", which when it is selected, the menu will display as the default book menu.
I also created a new option to "target_book" called dynamic. If Dynamic is selected, the block will detect the book menu to be displayed.

I haven't tested every possibility with these new options, so adjustments may be needed.

Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇦Canada joelseguin Ontario, Canada

    I've tested with a Drupal 10 install. The new "dynamic" option works great! I believe it's definitely a feature that is worthwhile to incorporate in the module as it is a common scenario. I plan on using it moving foward as it will save from having to select a specific book and help automate that process.

    I've tested out the "book_menu_default" option in the patch and I'm getting the following error when saving the option as well as on a book page:
    Warning: Undefined array key "#book_title" in template_preprocess_book_all_books_block() (line 374 of core/modules/book/book.module).

  • First commit to issue fork.
  • Status changed to Needs work over 1 year ago
  • Status changed to Postponed: needs info over 1 year ago
  • 🇩🇪Germany simonbaese Berlin

    Thanks for providing these new features. I tagged a new release for this module and wanted to reroll your patch. But while working on it, I wonder if I fully understand what you are intending to do. Can you please explain in more detail.

    1. What is the difference between dynamic and the block_mode option in core (all pages and book pages). See the description for the core block mode:

    If Show block on all pages is selected, the block will contain the automatically generated menus for all of the site's books. If Show block only on book pages is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The Page specific visibility settings or other visibility settings can be used in addition to selectively display this block.

    2. Would it be enough to rely on the parent build() if the new book_menu_default is selected? Something like the following:

    public function build() {
        if ($this->configuration['book_menu_default']) {
          return parent::build();
        }
        // Continue with custom book navigation.
        // ...
    }
    

    Thanks for your feedback.

  • Hello! Thanks for your questions!

    1.) The reason the Dynamic option was added is that we needed a way to only show the menu for the current book. For example, our website has three books (book1, book2, and book3) with their own menu structure. When I navigate to book1, I only need to see the menu for book1. When the block_mode was selected with book pages, the menu for book1, book2, and book3 was present on book1.

    Also, since we use layout builder for our content types, it was nice to add the block once and have the block choose the current $bid instead of manually selecting the book menu to display.

    2.) I believe I added this so the menu would retrieve nested data under the selected book page. For example, Book1 has three book pages nested: BookPageTutorial1, BookPageTutorial2, and BookPageTutorial3. All the book pages have three menu items nested as well. When on BookPageTutorial1, update the menu to display the three menu items nested.

    Structure on BookPageTutorial1:

    Book1
    BookPageTutorial1
    Tutorial1
    Tutorial2
    Tutorial3
    BookPageTutorial2
    BookPageTutorial3

    Structure on BookPageTutorial2:

    Book1
    BookPageTutorial1
    BookPageTutorial2
    Tutorial1
    Tutorial2
    Tutorial3
    BookPageTutorial3

    I am not sure if adding the return parent::build(); would give the desired result, but it might. Please let me know if you have any other questions!

  • Status changed to Needs work about 1 year ago
  • I just put together a new patch that adds the dynamic option for the module's RC2 version.

    I also had some issues using the in_active_trail variable within my template. I noticed it was being overwritten in the buildItems() function. So, I also made some adjustments to ensure the in_active_trail variable was set correctly.

    It looks like the book_menu_default option wasn't really needed. Deselecting Always expand the menu provides the desired result.

    The patch still needs to be tested.

  • Status changed to Needs review about 1 year ago
  • 🇩🇪Germany simonbaese Berlin

    @anthonygray47 Thanks for rerolling the patch. I opened a merge request to continue on this issue.

    1. Please test on your page.
    2. Can you please open a separate issue to resolve the problems with the active trail. I can certainly see that the code should be improved there. It would be nice to have a specific bug description though.
    3. I understand the use case for the "Dynamic" option now. Certainly, we are not covering all cases here. I think the confusion comes from how core handles the options "Show on all pages" and "Show on book pages only". We can add "Dynamic" for now and work on a more flexible solution in another issue. My idea is to separate the logic of "where" to display and "what" to display (both are coupled right now).
  • 🇩🇪Germany simonbaese Berlin

    Merging this since I would like to publish a new release soon. Please continue the discussion here if there are further issues. I will open a new issue for the active trail suggestion.

    • simonbaese committed f3b7c984 on 1.0.x
      Issue #3264851 by simonbaese, anthonygray47, balagan, joelseguin:...
  • Issue was unassigned.
  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024