
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.
Comment | File | Size | Author |
---|---|---|---|
#34 | book-tree-active-item-patch-rerolled-3056149-34.patch | 2.86 KB | kalpanajaiswal |
#29 | book_tree_active_item-3056149-29.patch | 4.28 KB | ericras |
#23 | book_tree_active_item-3056149-23.patch | 3.68 KB | demma10 |
#20 | drupal-book_tree_active_item-3056149-20.patch | 3.62 KB | franz |
#19 | interdiff.txt | 777 bytes | franz |
Issue fork book-3056149
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
Comment #2
chris burge commentedPatch is attached.
Comment #4
chris burge commentedThis patch accounts for situations where the current route doesn't belong to a node.
Comment #6
chris burge commentedThis patch doesn't assume in BookManager::buildItems() that
$data['link']['is_active']
is set.Comment #7
neeravbm commentedLooks good. The patch applies cleanly. Let's see if it passes automated tests.
Comment #9
chris burge commentedPatch attached.
Comment #10
chris burge commentedOk. It's actually ready for review now.
Comment #14
pcate commentedPatch 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?
Comment #15
pcate commentedComment #17
pameeela commentedClosed #3182994: BookManager generates links that don't get is-active class so added credit here.
Comment #18
jesss commentedPatch 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.
Comment #19
franzIn 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).
Comment #20
franzJust fixing typo
Comment #22
kasey_mk commented#9 worked for me once I used the 'is_active' variable to add the 'is-active' class in my theme's twig template:
{{ link(item.title, item.url, item.attributes.addClass(item_classes)) }}
Thanks!
Comment #23
demma10 commentedThe 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).
Comment #25
scotwith1tJust 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).
Comment #26
franz@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.
Comment #29
ericras commented10.1.x update
Comment #31
quietone commentedThis 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.
Comment #32
smustgrave commentedComment #33
smustgrave commentedNeeds reroll for contrib only.
Comment #34
kalpanajaiswal commentedRerolled the patch for contrib module only.
Comment #35
kalpanajaiswal commentedComment #36
smustgrave commentedShould be an MR
Comment #38
kalpanajaiswal commentedCreated an MR, ready for review.
Thank you!
Comment #39
kalpanajaiswal commentedComment #40
ankitv18 commented@kalpanajaiswal I've left some comments and Tests are also failing please check.
Comment #42
akashpj commentedHi, I've addressed the feedback which is mentioned in MR !10.
$element['is_active']
assignment to use a ternary operator.\Drupal::routeMatch()
call withRouteMatchInterface
by injecting it into theBookManager
constructor.@current_route_match
.Please review the changes and let me know if any further changes are required.
Comment #43
smustgrave commentedMR is unmergable. Probably could add a simple assertion somewhere too.
Comment #44
mdranove commentedComment #47
smustgrave commented