Problem/Motivation

Currently, when rendering the book tree, an in_active_trail value is included in the book tree for each item; however, no is_active value is included. As a result, it's not possible to know if a particular item is active. This is useful for applying active states in book navigation.

Proposed resolution

When rending items in the book tree, check if the item belongs to the current page. Make this information available in the template layer.

Remaining tasks

Submit patch

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

None.

Issue fork book-3056149

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Chris Burge created an issue. See original summary.

chris burge’s picture

Status: Active » Needs review
StatusFileSize
new1.83 KB

Patch is attached.

Status: Needs review » Needs work

The last submitted patch, 2: drupal-book_tree_active_item-3056149-2.patch, failed testing. View results

chris burge’s picture

Status: Needs work » Needs review
StatusFileSize
new1.84 KB

This patch accounts for situations where the current route doesn't belong to a node.

Status: Needs review » Needs work

The last submitted patch, 4: drupal-book_tree_active_item-3056149-4.patch, failed testing. View results

chris burge’s picture

Status: Needs work » Needs review
StatusFileSize
new1.9 KB

This patch doesn't assume in BookManager::buildItems() that $data['link']['is_active'] is set.

neeravbm’s picture

Looks good. The patch applies cleanly. Let's see if it passes automated tests.

Status: Needs review » Needs work

The last submitted patch, 6: drupal-book_tree_active_item-3056149-6.patch, failed testing. View results

chris burge’s picture

Status: Needs work » Needs review
StatusFileSize
new3.49 KB

Patch attached.

chris burge’s picture

Ok. It's actually ready for review now.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

pcate’s picture

Patch applied fine to 8.9.10 and added the active class.

Looking at the failed tests it seems to be related to the Classy theme templates that are being copied to other themes. I believe that's being done for some compatibility in Drupal 9, but don't know for sure.

The fix might be as simple as also updating the template files in those themes. I think the doc block in the all the templates files will be needed to updated with the - is_active: TRUE if the link is for the current page. text anyway.

Because there are different core themes for D8 and D9, there might need to be separate patches made for both versions.

Also, I assume a test will need to be added for this new functionality?

pcate’s picture

Status: Needs review » Needs work

pameeela credited franz.

jesss’s picture

Patch applied cleanly, but I didn't see any changes at first because the twig template for the Stable theme isn't included in the patch. Even though that template doesn't add any classes, the docblock at the top should be updated to note that the variables are available for use.

franz’s picture

StatusFileSize
new3.62 KB
new777 bytes

In the project I was using, I called the BookManager service to rebuild the outline when a revision was reverted. That particular route doesn't autoload the node object, causing an error. This might happen in other cases so I added an ugly fix on my patch (I only tested in 8.9.x but should work with other branches too).

franz’s picture

StatusFileSize
new3.62 KB

Just fixing typo

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kasey_mk’s picture

#9 worked for me once I used the 'is_active' variable to add the 'is-active' class in my theme's twig template:

{% set item_classes = [
  item.is_active ? 'is-active',
] %}

{{ link(item.title, item.url, item.attributes.addClass(item_classes)) }}

Thanks!

demma10’s picture

StatusFileSize
new3.68 KB

The patch in 20 assumes that all routes that bookOutlineRecursive is called from have a node parameter that contains either the node itself or a node id.

On my site I need to show a partial book outline on the node/add page form for child pages. The node/add page doesn't have a node parameter in its route and because of this the patch causes an array_flip() warning. The warning doesn't stop the form from saving, it just adds extra clutter to the logs.

I've attached an update to the patch which adds an extra null check to the bookOutlineRecursive function, to handle the case where the route doesn't have a node parameter at all. When there is no node parameter in the route the value of $node will remain NULL and the is_active flag will not be added to any object in the outline (which makes sense for the node/add page as when that page is called no node exists yet).

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

scotwith1t’s picture

Just curious, and maybe this is a dumb question, but why aren't the template changes being added to core/modules/book/templates/book-tree.html.twig instead of the one in classy theme? That is the template that is used if classy isn't enabled (pretty common, i would think).

franz’s picture

@scotwith1t it is being added to that template as well, but the default template doesn't really add classes so it is just documented as an available variable. Many themes extend classy to make use of the helpful default classes so it would be very common that classy's template be used ore copied over.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ericras’s picture

StatusFileSize
new4.28 KB

10.1.x update

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Postponed

This extension is being deprecated, see #3376070: [Meta] Tasks to deprecate Book module. It will be removed from core and moved to a contrib project, #3376101: [11.x] [Meta] Tasks to remove Book.

This is now Postponed. The status is set according to two policies. The Remove a core extension and move it to a contributed project and the Extensions approved for removal policies.

This issue may be re-opened if it can be considered critical, If unsure, re-open the issue and ask in a comment.

smustgrave’s picture

Project: Drupal core » Book
Version: 11.x-dev » 1.0.x-dev
Component: book.module » Code
Status: Postponed » Needs work
smustgrave’s picture

Version: 1.0.x-dev » 2.0.x-dev
Issue tags: +Novice, +Needs reroll

Needs reroll for contrib only.

kalpanajaiswal’s picture

Rerolled the patch for contrib module only.

kalpanajaiswal’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Should be an MR

kalpanajaiswal’s picture

Created an MR, ready for review.
Thank you!

kalpanajaiswal’s picture

Status: Needs work » Needs review
ankitv18’s picture

Status: Needs review » Needs work

@kalpanajaiswal I've left some comments and Tests are also failing please check.

akashpj made their first commit to this issue’s fork.

akashpj’s picture

Status: Needs work » Needs review

Hi, I've addressed the feedback which is mentioned in MR !10.

  1. Refactored the $element['is_active'] assignment to use a ternary operator.
  2. Replaced the direct \Drupal::routeMatch() call with RouteMatchInterface by injecting it into the BookManager constructor.
  3. Updated the service definition to include @current_route_match.

Please review the changes and let me know if any further changes are required.

smustgrave’s picture

Status: Needs review » Needs work

MR is unmergable. Probably could add a simple assertion somewhere too.

mdranove’s picture

Assigned: Unassigned » mdranove

smustgrave’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.