Per #2757143: [Meta] Add missing features to Drupal 8 menu_block this feature hasn't been implemented yet.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

aaronbauman created an issue. See original summary.

JohnAlbin’s picture

JohnAlbin’s picture

Title: port "Make the starting level follow the active menu item" feature to d8 » Add option to make the starting level follow the active menu item
nod_’s picture

Status: Active » Needs review
FileSize
2.47 KB

Seems like this is working.

nod_’s picture

extra space

manuel.adan’s picture

I got an empty menu. Tracing it, I found that root menu item id in parameters->activeTrail was empty string, probably as menu itself. The new patch also doesn't alter the activeTrail array.

matsbla’s picture

I think the patch that came in this issue includes this:
https://www.drupal.org/node/2811337

matsbla’s picture

Tom.Camp’s picture

The patch from #6 was throwing an Undefined index error for existing blocks. I added 'follow' to the defaultConfiguration array and that fixed the issue.

AaronBauman’s picture

Status: Needs review » Needs work

Still not quite there.

Given the following menu structure:
A
- B
-- C
--- D
and given placing a menu block with level = 1, depth = 2

Then when I navigate to node A
Expected behavior:
Menu block shows items A and B
Observed behavior:
Menu block shows only item B

Then when I navigate to node C
Expected behavior:
Menu block shows items C and D
Observed behavior:
Menu block shows items B and C

AaronBauman’s picture

The more closely I'm looking at this, the more I think it's superseded by #2631468: Menu subtrees in menu blocks show all subitems regardless of the active menu item

If I'm mis-interpreting the intent of the "follow" option, then can someone please spell it out more clearly?
Keep in mind that the core Menu Block works differently than Drupal's old core menu blocks, so "make it work like in D7" is no longer sufficient.

Tom.Camp’s picture

Aaron, I've attached a patch that adds a checkbox for *Current page as top level*. If checked this will show the page that you are on as the top menu item. So in your example if you are on page C, it will show C and D. If you are on B, it will show B, C and D. If you only choose to show a depth of 2, it would show B and C and then wen you went to C, it would show C and D.

AaronBauman’s picture

Thanks Tom, I think this clearly differentiates the "follow" and "current" behavior.
The "follow" behavior, best I can tell, is what's in #2631468

So, the ways forward are:

  1. patch menu_block now with follow/current behavior; potentially re-patch after #2631468 hits
  2. wait until #2631468 lands to re-open this issue
  3. build a patch which depends on #2631468

i'm not sure the best way to go -- I'll defer to maintainers there.
Patches thus far represent option number 1

lpeabody’s picture

I've been testing this out, and I cannot get it to do what I expect this to do.

Given the following menu structure:
- Root
-- A
----1
------a
--------iv
--------vi
------b
----2
-- B
-- C

And the following selected:

  • Expand all menu links
  • Follow active trail
  • Current page as top level

Initial menu level is 1.
Maximum number of menu levels to display is 2.
Fixed parent item is Main navigation.

When I'm on menu item 1, I would expect to see the following menu block:
------a
--------iv
--------vi
------b
But I get:
----1
------a
--------iv
--------vi
------b

Which is counter intuitive to me. I already know I'm on the current page, why add the current page as the root in the block?

My expectation is that "Current page as top level" essentially overrides "Fixed parent item", but that doesn't appear to be the case.

Maybe there needs to be an option for setting starting depth? Or an option to hide the parent element?

cman9090’s picture

Patch #8 is good. However is it possible to make sure if you are on the last level and their no more children, to show that current level and its parent? For instance:

1
--2
---3
----4
----a
----b

if im on 4, it only shows 4 but I want it to show:
3
-4
-a
-b

cman9090’s picture

Also for some reason the first item in a menu does not show as active trail if it is the page that is active, but any other menu item that is not the first will show as active if you click on it? edit. fixed this with the menu trail by paths module.

Gertjan.k’s picture

I've extended patch #8 with the functionality described in #15. I've added a checkbox. When it's active and the current page doesn't have children then the root of the menublock is changed to the parent of the current page.

Gertjan.k’s picture

I've rerolled #17 to work in combination with patch #11 of #2809699-12: Add configuration options for dynamic block titles

Gertjan.k’s picture

Patches #17 and #18 don't work properly. I reverted back to #7 and used a difference approach to my usecase.

rutiolma’s picture

Adding the feature, like on D7, "Make the starting level follow the active menu item." seems to be fixed by comment #13 here

stefan.r’s picture

Status: Postponed » Needs review
FileSize
4.9 KB
AaronBauman’s picture

Status: Needs review » Needs work

stefan.r can you explain your intent in cross-posting the patch?
If you believe there is functionality from 2811337 which needs to be included into the current thread, you should explain the delta and provide a new merged patch.

Also:
I still think this entire thread will change drastically based on #2631468
Is it worth pursuing this patch before 2631468 lands?

smaz’s picture

I'm not having much luck with this patch either. Two issues:

1) If a menu item has no sub items, it displays all items from the current level rather than not displaying anything. I fixed this by removing the line:

end($parameters->activeTrail);

This seemed to set it to an empty item, which would fully load the menu tree when it gets to:

    // Load the tree if we haven't already.
    if (!isset($tree)) {
      $tree = $this->menuTree->load($menu_name, $parameters);
    }

2) If an node is not in the menu, it would display the top level menu items.

Again, I think this is because of the above snippet about loading the tree. After some brief testing, it appears that if a node is not in the menu the active trail will just contain a single, empty item. If it is in the menu, there will be two keys, with one of them containing content.

By adding the following, it will not continue if the active trail is empty:

    if (empty(array_filter($parameters->activeTrail))) {
      return [];
    }

I add this above the following line:

if ($follow) {
Gertjan.k’s picture

I've rerolled #9 (The best working patch for me so far) to work in combination with #2811337-26: 2+ level menu block not limited to active parent. Setting a starting level also didn't work so I fixed that too.

rutiolma’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
56.89 KB

@aaronbauman the patch stefan.r cross posted at #21 solves the problem as described at this issue by adding an option to make the starting level follow the active menu item - check screenshot attached.
Menu Block D7 "follow active trail" is supposed to work in the same way as described at #14 and not as described at #10 or #15. Basically this option will allow to dynamically change the "parent" to the active page instead of keeping it fixed, making it possible to create a navigation with a breadcrumb (ancestors) and menu block (children).

Working example using the D7 version of this functionality: https://www.belgium.be/en/economy
Ancestors in the menu: Root > Economy
Current page: Business
Children menu trail items: Creation (plus a 2nd level), Regulated occupations (plus a 2nd level), Taxation and accounting (plus a 2nd level)

As far as I understand, this issue is not the same as #2631468 or #2811337.
None of the other patches posted in this issue queue worked for me to fix the described problem, only the one from comment #21 which should be considered.

Changing this to RTBC for patch at #21, but it's important to have more testers.

maseyuk’s picture

Edit: sorry what I said wasn't valid and I cant delete my comment, this is causing the issue I was experiencing I think

https://www.drupal.org/node/2642816

polynya’s picture

I have rerolled patch #24 to be compatible with patch #29 for #2811337: 2+ level menu block not limited to active parent. I have also changed variable $maxDepth to snake_case $max_depth for consistency.

devil2005’s picture

Is it possible to publish a dev version with all patches ?

Thanks

devil2005’s picture

Is it possible to publish a dev version with all patches ?

Thanks

caiobianchi’s picture

+1 for a patched dev version.

adammalone’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
3.28 KB
438 bytes

I might be wrong here but installing menu_block from scratch and applying the patch in the other thread, then the one in this thread didn't allow me to place blocks. My reroll is based on the work of #27.

Super basic patch here as I only added in the follow configuration to defaults. Should be an easy RTBC

sherakama’s picture

Patch in #31 did not apply to dev or latest stable (1.4) so I am re-rolling it here with a couple of updates.

The menu starting level and depth should still apply to the root item and should be set to 1 and 1 respectively if you want only the sibling items of the active path.

sam.spinoy@gmail.com’s picture

#32 does not work for me. I have a menu like so:

- A
- - B
- - - C

Config is set to show only one level of depth and to follow the active trail, so I only want the direct children of the current page I am on. But no matter what page I am on, it always shows menu item B.

devil2005’s picture

same for #32 broken...
any one know what's the right order to apply patches ? since stable or dev version ?

thanks a lot

adammalone’s picture

The patch in #32 will apply in the following conditions:

devil2005’s picture

@Typhonius : i applied patches as you explained but nothing happened, no now parameters in module or block...

wheretoplaygames’s picture

typhonius: There is no patch on #30 on that page.

Anyonne have a comprehensive patch to apply to the latest dev or stable?

dieppon’s picture

@wheretoplaygames what @typhonius means is #44 is the patch that ends with 30 on its name.

mithenks’s picture

Hello,
I've added an option to show the "direct parent" when following the active trail of a menu.
This can be useful because in this way the user can go backward.
The patch must be applied after the one for #2811337, as stated in #38.

Maybe this can be useful for everyone. If not, please ignore this version.

Regards,

Eyal Shalev’s picture

The patch in #39 conflicted with the patch for Menu parent as block title because they both edited the configuration form for the block.

This patch moves the positioning of the added lines to avoid conflict.

Eyal Shalev’s picture

FileSize
4.61 KB

This patch moves the positioning of the added lines to avoid conflict with Menu parent as block title (see #40), and adds the option to follow the parent all the way until the root of the menu (not including the root), and hide a menu if the current page does not appear in it.

dddbbb’s picture

Status: Needs review » Needs work

The patch in #41 works for me with the exception that pages that should not render the menu block now render an empty menu block.

pookmish’s picture

Just re-rolling #41 since it was failing to patch due to recent changes on the main branch.

dddbbb’s picture

Just tested the patch in #43 with the 8x-1.5 release and it applies cleanly and works as expected (thanks).

Concerns raised in #42 still apply though so leaving status set to "Needs work".

rutiolma’s picture

This is another approach to fix this issue and was wrongly being developed on issue #2811337.

The UI of #43 is more user friendly and that may be the way to go, but I'm attaching a patch based on #2811337 - comment #60 which may be useful to someone, like me, that was using it and doesn't have the time to make a migration path to apply patch #43.

pookmish’s picture

Status: Needs work » Needs review
FileSize
4.72 KB

The attached patch takes #43 and uses the blockAccess() method to simply say the block is forbidden if there are no items. This prevents the block from rendering an empty block.

dddbbb’s picture

Status: Needs review » Needs work

The patch in #46 almost works for me but seems to have a quirk...

If the block config contains a visibility rule (I tested "Hide for the listed pages" and added a single path) the menu block appears where I'd expect to:

- shows when there's menu items,
- hides when there's no menu items,
- respects the block visibility rule.

If I remove all visibility rules on the same block it will not render anywhere.

pookmish’s picture

Hmm. @danbohea, I'm not able to reproduce that quirkiness. I repeated your steps and tried to add and remove various conditions like path, role, and content type. I experienced the correct desired outcome.

Scenario 1:
* added block with visibility to show only on path `/node/3` (child node)
- block only shows on /node/3
* removed visibility condition
- block shows on all pages with links.

Scenario 2:
* added block to all pages
- block is visible on pages with links.
* added condition to only show for anonymous roles
- block is visible when logged out
* removed condition
- block visible to all roles again on pages with links.

dddbbb’s picture

Perhaps the issue I'm seeing relates to another part of the project I tested on.

¯\_(ツ)_/¯

johnny5th’s picture

Given this structure:

-Root
--A
---a
----1
-----one
-----two
-----three
----2
----3
---b
---c
--B
--C

with initial visibility level: 2, number of levels to display: 2, Fixed parent on Main navigation, Follow active trail, following one parent.

When on page "a" the result I get is:
-1
-2
-3

when I expect
-a
--1
--2
--3
-b
-c

Yet, when I visit "1" the result I get is:
-1
--one
--two
--three
-2
-3

which is expected.

By tweaking the level calculation, it seems to make both scenarios work the same way.

-      if ($follow_parent == '1' && (($level - 1) > $min_depth)) {
+      if ($follow_parent == '1' && (($level - 1) >= $min_depth)) {

Does that logic work?

byrond’s picture

We were having the same issue as in #50, and the logic there works for us. There is also an issue when the level is > 1, and depth is set to unlimited (0). In that case, no children were being displayed, because the original depth wasn't being tested. This patch updates the line that tests for an unlimited depth.

KarlShea’s picture

#51 worked for me.

2pha’s picture

#51, why does it remove $suggestion = $this->configuration['suggestion']; ?

Should these new config items be added to the schema.yml ?

rmccreary’s picture

So for my needs i needed only the child items of the current page...

Given this structure:

-Root
--A
---a
----1
-----one
-----two
-----three
----2
----3
---b
---c
--B
--C

When on page "a" the result I want is:
-1
-2
-3

and, when I visit "1" the result I get is:
-one
-two
-three

drclaw’s picture

Status: Needs work » Needs review
FileSize
8.63 KB
8.09 KB

Here's an updated patch with a few changes:

  • Updated the form element labels and descriptions to be more... descriptive. Used the D7 module as a starting point.
  • Changed the follow_parent option to work like the D7 module version. The result is essentially the same as the "Follow one parent" and "Do not follow parent" options in the patch. I couldn't figure out what the value of the "Follow all parents (excluding the root)" option was supposed to do... If you're following all parents, you're no longer following the active menu item and as far as I could tell it produced the same result as just creating a static menu block with initial visibility level 2.
  • The patch from #2811337: 2+ level menu block not limited to active parent that was committed already has the code we need for setting the menu root, we just need to update the $level variable based on our position in the active menu trail. I updated some of the menu root setting logic to just set the level and let the aforementioned code do its thing. This has the added bonus of being able to follow the active menu item on a menu block with the starting level set to 1 (which wasn't working properly before as far as I could tell).
  • Lastly, I added support for following the active menu trail when a fixed parent item other than the root is set. Before, this option would essentially override any follow settings.

There's just one thing left I'm curious how to handle and that is what to do if you're following the active menu item, have the starting level set to 1, but you're on a page that's not in that menu. Currently we just fall back to the default functionality which is to "Use level 1 to always display this menu.", but I feel like if you're following the active menu item the menu should only show if the page you're on is part of that menu... Or maybe it could be an additional option?

Anyway, patch described above is attached with an interdiff.

Thanks!

johnny5th’s picture

I can confirm #55 works, and solved some issues with the prior patches. It also makes a heck of a lot more sense.

KarlShea’s picture

Also confirming #55 works, and it does make way more sense.

dddbbb’s picture

Yep, liking #55 too. I had to tweak a couple things in config to get the same behaviour I had with a previous patch but it was possible, in fact the behaviour has improved and yes it makes much more sense reading back through the config now. Thanks @drclaw!

steveoriol’s picture

#55 works for me too, thanks !

charly71’s picture

There is a plan to include patch #55 in next dev release?
Thanks

dddbbb’s picture

Status: Needs review » Reviewed & tested by the community
Kasey_MK’s picture

#55 is great, thanks!

I'd love to see some action on https://www.drupal.org/project/menu_block/issues/2756675 - sorting by the active item - presumably that could build upon this work.

  • joelpittet committed 79c1eef on 8.x-1.x authored by drclaw
    Issue #2756675 by Gertjan.k, pookmish, Tom.Camp, Eyal Shalev, nod_,...
joelpittet’s picture

Status: Reviewed & tested by the community » Fixed

I've committed #55 to the -dev branch. Please try it out if you are after this feature.

Status: Fixed » Closed (fixed)

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

idebr’s picture

This issue introduced new configuration options, but these options are not defined in menu_block.schema.yml. I have created #3022011: Implement config schema for block.settings.menu_block.*.{follow, follow_parent} to fix this.

Also created #3022012: Add automated tests to catch these issues when uploading patches in the issue queue.

Joshua_Lim’s picture

i cannot apply this patch??

it says

error: config/schema/menu_block.schema.yml: no such file or directory

why?? i put the patch file in the schema folder and also tried to put it in the menu_block folder and apply the patch. it will not work

sorry i'm a beginner

it also says if its in the directory outside

error: menu_block.install: no such file or directory

please help badly needed! i'm using drupal 8.6.4

alison’s picture

Sorry for the difficulty, @Joshua_Lim, did you figure it out? Here's some documentation that might help:
https://www.drupal.org/patch/apply

(Are you using composer, or drush, or manually applying things, or?)

---------------------
Meanwhile, the patch from #55 works awesome for me, thank you!

natew’s picture

We were using menu block 1.4.0 and an old patch (#32). I just updated to 1.6 and I don't think this patch is needed as most of the code appears to be there?