How can I extend the functionality to include the parent item in the menu?

If my Primary Links menu is

-Foo1
--Bar1
--Bar2

etc. then setting the menu to Primary Links, the item to and starting level to 2 then when I go to Foo1 the menu is

Bar1
Bar3

when I'd like

Foo1
Bar1
Bar2

This seems to be the way the Druaplcon site works at http://paris2009.drupalcon.org/aboutus - do I have to manually add in an entry for Foo1 to the menu so it's

-Foo1
--Foo1
--Bar1
--Bar2

or is there a better way?

Comments

hefox’s picture

StatusFileSize
new1.58 KB

I couldn't see any way to do that either without setting the block title, which I see as error prone.

Attached is a patch with my solution

Cool module

marcushenningsen’s picture

Sorry for the newbie question: To which file is the patch supposed to be applied?

cvaughn13’s picture

hefox,

I tried to implement your patch, but could not get it to work. Instead,

when at

Parent

of

Parent
--Child1
--Child2

all I saw was

--Child1
--Child2

Is there a setting that needs to be set so that only

Parent
--Child1
--Child2

can be seen? I don't want to see all the other Parent links, jut the Parent link that is active, and its children in a sidebar.

Thanks for your help.

snorkers’s picture

Status:Active» Needs review

I got this patch to work fine (rolled against the current release 6.x-2.2), although needed to check the Expand children: > Expand all the sub-menus of this tree option to get it to work as planned; I also set starting level to '1st with parent' (which is the option the patch adds) and Maximum depth to 2.

This is a really useful addition to menu block and I have suggested that this code should be reviewed. Hopefully this functionality will make it into a future release of the module.

Thanks @hefox

abelb’s picture

Thanks @hefox

i got this to work perfectly for one of my menus. exactly what i was looking for.

it would be great to be able to choose 2nd level with parent!
anyone know how to do that?

cvaughn13’s picture

hefox, et. al,

The patch does work, sorry for the confusion. Though, I did misinterpret what I though the patch was going to do.

I am looking for what alburton I think is suggesting by "2nd level with parent."

That is, show a second level link and only the parent it is connected to, not all the other menu items at the parent's level.

For example

If I click on Parent1 (or any of its children), I see:

Parent1
--child1
--child2
--child3

and not

Parent1
--child1
--child2
--child3
Parent2
Parent3
Parent4

guix’s picture

Maybe you could use the cool new features in -dev : #345419: Add option for menu title as link.

Since John committed the solution above, I don't think patches in that issue could be committed.

Danny Englander’s picture

subscribing

edit: the patch worked for me, this is exactly what I needed. It messed with my CSS a little but that can be fixed.

The other thing I just learned about is to use the command line to apply the patch, in my case Terminal in OS X. I took the patch and put it in the same directory as the files to be patched and then used the command:

patch -b < menu_block_show_parent.patch

This saves lots of time. If your site is online and you don't have ssh access you could download the folder with the module to be patched via FTP, make a temp folder on your hard drive and then do the patching and reupload.

snorkers’s picture

Version:6.x-2.2» 6.x-2.3
Status:Needs review» Reviewed & tested by the community
StatusFileSize
new846 bytes
new523 bytes

Just hacked around and re-rolled the patch for 6.x-2.3 (#1 won't work against he latest release). Uses the same code that @hefox worked out and seems to work fine...

I'm not a great patch roller... so it's 2 separate files. (off topic: can the text from 2 sep patches just be merged to create a single file?)

hefox’s picture

(@snorkeys, yes I've done it a few times (combine patches) XP.

jtidal’s picture

For me, this patch breaks menu items more than 2 levels deep. If I include the parent, the block doesn't show up at all.

Any fixes?

FreddieK’s picture

Status:Reviewed & tested by the community» Needs work

The patch didn't work for me, and a good solution should be to make an 'add parent to menu links' checkbox available on all levels, not just first.

garryi’s picture

Has there been any progress with this? This seems to be the right direction, but the patch in #9 is not working (for me at least).

jim22’s picture

This would be a great feature.

stockliasteroid’s picture

StatusFileSize
new5.36 KB

Here's a patch against 2.3. Instead of the arbitrary depth limit of 1 in the previous patch, this should support unlimited depth below the parent menu item using the existing level selection dropdown. It just provides a checkbox on the block settings called "Include Parent"...

hefox’s picture

Status:Needs work» Needs review

setting it to needs review

christiaanb’s picture

StatusFileSize
new4.74 KB

I've modified stockliasteroids patch a bit to also work for sub menu blocks that don't have a parent menu item selected but only a menu.

knalstaaf’s picture

Believe it or not, but all it took was highrockmedia's reply to start patching for the first time. Other tutorial pages seemed a little overwhelming to me.

Thank you, highrockmedia, for this valuable entry.

knalstaaf’s picture

Just what I was looking for - thanks. This option should be available by default.

Ronino’s picture

I ported the patch provided in comment #17 (thank you!) to Drupal 7 including some minor improvement, see #1045352: Including parent item in the menu

arrrgh’s picture

Version:6.x-2.3» 6.x-2.4
Category:support» feature
Status:Needs review» Reviewed & tested by the community

I've just rolled the patch at #17 against 6.x-2.4 and works (with a few offsets). The patch certainly works, and it seems that there are at least 2 others that have tested it => RTBC.

dgsiegel’s picture

StatusFileSize
new4.74 KB

i can confirm that the patch is working with 6.x-2.4.

attached you can find a re-roll of the patch in #17 which just removes the offset warnings.

safetypin’s picture

Version:6.x-2.4» 7.x-2.x-dev
Status:Reviewed & tested by the community» Needs review
StatusFileSize
new4.56 KB

I've manually applied the patch from #22 to 7.x version files, and everything seems to be functioning as expected.

arrrgh’s picture

Nice to see that it works in 7.x. But was the 6.x-2.4 patch committed? If not can it be left at RTBC and a separate issue for D7 started?

Ronino’s picture

StatusFileSize
new6.62 KB

Some time ago, I adapted the patch from comment #17 for D7 with some minor additions (work for menus of arbitrary levels and do not add the parent item as the only item in the menu), see #1045352: Including parent item in the menu. I was asked to to post the patch here, so this is what I do.

mmcintosh’s picture

Just tried this patch and it fails test -- menu_block version info listed below as is the fail

Any ideas ??

Thanks

patching file b/sites/all/modules/menu_block/menu_block.admin.inc
Hunk #1 FAILED at 246.
Hunk #2 FAILED at 356.
2 out of 2 hunks FAILED -- saving rejects to file b/sites/all/modules/menu_block/menu_block.admin.inc.rej
patching file b/sites/all/modules/menu_block/menu_block.module
Hunk #1 FAILED at 180.
Hunk #2 FAILED at 196.
Hunk #3 FAILED at 317.
Hunk #4 FAILED at 505.
Hunk #5 FAILED at 516.
Hunk #6 FAILED at 538.
6 out of 6 hunks FAILED -- saving rejects to file b/sites/all/modules/menu_block/menu_block.module.rej

menu_block version:

Project : menu_block
Type : module
Title : Menu Block
Description : Provides configurable blocks of menu items.
Version : 6.x-2.4
Package : Other
Core : 6.x
PHP : 4.3.5
Status : enabled
Path : sites/all/modules/menu_block
Schema version : 6201
Requires : block, menu
Required by : none

FreddieK’s picture

Works like a charm!

mmcintosh’s picture

I reattempted patch and it worked fine this time.

Regards

Mark

mjpg’s picture

Does anyone have a patched version of the D7 menu block module that I can use?

menu_block_include_parent_d7.patch

as I am not able to apply the patch easily myself.

Thanks

Ronino’s picture

StatusFileSize
new43.45 KB

mjpg, attached you find the 7.x-2.2 version, patched with my patch from comment #25

mjpg’s picture

Many thanks - still not quite working but I suspect that it may be the dual language versions of the pages with two menu entries causing an issue.

rbishop’s picture

thank you for this patch! working great.

Yuri’s picture

for #30 to work, I had to set the maximum depth to 2 or more (i had it on 1) otherwise you only see the parent item.

knalstaaf’s picture

Indeed Yuri, the maximum depth should be set to "2", which leads to the menu being built up in a list within a list.

Is there a way this behaviour doesn't occur? In other words: is it possible to add both the parent item and the subitems in one list?

Here's what I mean:

Currently this is the case:

<ul>
  <li>Parent item
    <ul>
        <li>Subitem</li>
        <li>Subitem</li>
        <li>Subitem</li>
    </ul>
  </li>
</ul> 

Here's what I'd prefer:

<ul>
  <li>Parent item</li>
  <li>Subitem</li>
  <li>Subitem</li>
  <li>Subitem</li>
</ul>

If this isn't possible or if it's bad practice (confusing for SEO), then it's still possible to solve that through CSS layout-wise.

mlecha’s picture

Due to issues from the Drupal 7.12 upgrade it seems that we're forced to upgrade to Menu Block 2.3.
#1425342 Drupal core upgrade from 7.10 to 7.12 causes menu block to fail

I was happily using Ronino's patch at #25

I'm not sure if the Menu Block 2.2 patch will work on Menu Block 2.3?

It would be so great to get the "Include parent" functionality included in Menu Block that this patch provides.

rbishop’s picture

i re-applied Ronion's patch at #25 to Menu Block 2.3 post drupal 7.12 upgrade and all appear to work as expected for me

be_design’s picture

Menus disappear when you set the "Fixed parent item" to something more than one level deep
(If I start one level deep things are fine)
I'm on drupal 7.12 and menu_block 2.3 and have applied the #25 patch without errors

Any ideas??

e.g. of what I'm after

PARENT ITEM
- PARENT ITEM TWO
-- PARENT ITEM THREE ( I want to start here)
-- ITEM
-- ITEM
-- ITEM
-- ITEM

I'd like to create a menu block that looks like
-- PARENT ITEM THREE
-- ITEM
-- ITEM
-- ITEM
-- ITEM

Anonymous’s picture

I'm on drupal 7.14 w/ menu_block 2.3 and the patch from #25 seems to work perfectly for me.

Looks like this issue has been going on for a while - any way we can get this feature into a release? Is there anything blocking its inclusion?

alexverb’s picture

Status:Needs review» Reviewed & tested by the community

I'm on 7.15 and Menu Block 2.3 and also confirm this patch is working great for me. It would be nice indeed to have this feature in the future release.

hkirsman’s picture

Patch #25 worked for me (using 2.3). This should be included in the module!

Matthew OMalley’s picture

+1 please approve and release

rylowry’s picture

#25 worked for me using menu_block 7.x-2.3.

Katy J’s picture

Thank you. Patch in #25 worked perfectly for me with 7.x-2.3 once I remembered how to apply a patch using the cmd line rather than cut and paste!

hkirsman’s picture

Will this be included into the module?

gmclelland’s picture

How is everyone applying this patch from #25?

I have tried many different variations:
patch < menu_block_include_parent_d7_0.patch
patch -p1 < menu_block_include_parent_d7_0.patch
patch -p0 < menu_block_include_parent_d7_0.patch
git apply -v menu_block_include_parent_d7_0.patch

Nothing seems to work

Ronino’s picture

gmclelland, from what directory did you try to apply the patch?

gmclelland’s picture

StatusFileSize
new7.33 KB

@Ronino - Thanks for the reply. I tried running those commands from the root of the module.

Here is a untested manual reroll of #25 that should apply against MB 2.3.

Hope it helps someone.

othermachines’s picture

StatusFileSize
new7.78 KB

Here's a new patch rerolled against 7.x-2.x-dev and tested on Drupal 7.23.

Anyone who (like me) is eager to have this feature committed should install 7.x-2.x-dev, test this patch and report back. It's a good feature.

Re comment #37 - also tested on a menu block starting on third leve; works fine.

Thanks everyone.

othermachines’s picture

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

After patching this I am getting this Notice:

Notice: Undefined index: include_parent in menu_tree_block_data() (line 284 of /profiles/panopoly/modules/contrib/menu_block/menu_block.module).

othermachines’s picture

@tchopshop

Sounds like it may not have applied properly to menu_block.admin.inc. Which patch and which version of menu_block?

tchopshop’s picture

the patch from #48 and the current dev. I applied the patch manually.

othermachines’s picture

@tchopshop -

If you've applied the patch manually, it's difficult for me to troubleshoot. As you can see from the error, $config['include_parent'] is undefined. If the patch was applied properly, this index should be defined in the function menu_block_get_config(). The only other thing I might suggest is that, if you already have a menu block defined, then you may need to resave your block configuration. Let us know if you make any progress.

caschbre’s picture

Issue summary:View changes
Status:Needs review» Needs work

We're running into the same issue as #37. If our parent is at the top of the menu then it works great. However it our parent (as designated in the menu block config) has a parent menu item of it's own, then it returns nothing.

The issue seems to be stemming from the logic inside menu_tree_prune_tree...

+          if (!empty($include_parent)) {
+            $tree = array($key => $tree[$key]);
+          } else {
+            $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array();
+          }

If I were to comment out the if condition and just use...

$tree = $tree[$key]['below'] ? $tree[$key]['below'] : array();

... the menu block renders a menu, though without the parent. It's when we reset the $tree to the parent item instead of the children ('below') that the logic check of

if ($tree[$key]['link']['mlid'] == $plid) {

fails and we eventually hit the !$found_active_trail condition which wipes out any results.

I'm still debugging to figure out why... but i've narrowed it down to that part of the patch that seems to be the cause of the issue.

caschbre’s picture

Status:Needs work» Needs review
StatusFileSize
new7.39 KB

Attached is a new patch which is just a small modification to the patch in #48. I've tested this against 7.x-2.x and 7.x-2.3 locally. I did have to manually apply the patch to 7.x-2.3.

Difference between the two patches is small...

#48: if (!empty($include_parent)) {
#55: if (!empty($include_parent) && $plid != $parent_item['plid']) {

This solved the issue we were experiencing noted in comment #37 above.

othermachines’s picture

Brilliant. The patch is good and solves the problem described in #54.

I SO want to mark this as reviewed and tested but I suppose we should wait for one or two others to chime in.

Thanks, @caschbre!

wooody’s picture

Issue summary:View changes
wooody’s picture

Issue summary:View changes
wooody’s picture

Hi,
How can I make the same with Better Jump Menus module...
https://drupal.org/project/jump_menu

Thanks..

othermachines’s picture

@wooody

Go to the Better Jump Menus issue queue and post your question there.

4kant’s picture

Hi there,
I needed a special background color for a menu block when it´s parent item is active (li.active-trail).
With the patch from #55 I could do that.
In CSS i had to set diplay:none for the parent li-item and for it´s sub-items I set diplay:block.
But with the presence of the selector "li.active-trail" of the parent item, I can now style the block.
Thanks!

othermachines’s picture

Status:Needs review» Reviewed & tested by the community

Thanks @4kant. I think this qualifies as reviewed and tested. I checked and no re-roll is required.

jessehs’s picture

The patch in #55 is working for me. It does not apply against 2.3, but it does apply against Dev. (I tested against revision 8dd19f03.)

phai’s picture

The patch #55 is working for me too (REMEMBER: it applies only on DEV).
Thanks

ZenDoodles’s picture

This patch works for me. Testing in a fresh install, using the following steps:

  1. Install as normal. Ensure menu_block is enabled.
  2. Enable devel and devel_generate. (This makes it easier to create a menu tree without manually creating a bunch of menu items: "drush en devel devel_generate"
  3. Generate some content: "drush generate-content 50" to generate 50 pieces of content.
  4. Generate menu items: "drush generate-menus 1 20 9" (Parameters are weird, but that's 1 menu, 20 links, and a max-depth of 9)
  5. Add and configure menu block for the generated menu at admin/structure/block
  6. Observe the block in it's natural habitat.

A couple of minor code comments:

  1. +++ b/menu_block.module
    @@ -199,17 +199,18 @@ function menu_block_get_all_menus() {
    +    'delta'          => $delta,
    +    'menu_name'      => 'main-menu',
    +    'parent_mlid'    => 0,
    +    'parent'         => '',
    +    'title_link'     => 0,
    +    'admin_title'    => '',
    +    'include_parent' => 0,
    +    'level'          => 1,
    +    'follow'         => 0,
    +    'depth'          => 0,
    +    'expanded'       => 0,
    +    'sort'           => 0,
       );

    This also needs to be updated in hook_menu_block_blocks() in menu_block.api.php

  2. +++ b/menu_block.module
    @@ -592,8 +596,12 @@ function menu_tree_prune_tree(&$tree, $level, $parent_item = FALSE) {
    +          } else {

    Sorry this is so nitpicky... We prefer closing braces on their own lines.
    @see https://drupal.org/coding-standards

  3. +++ b/menu_block.module
    @@ -616,8 +624,14 @@ function menu_tree_prune_tree(&$tree, $level, $parent_item = FALSE) {
    +        } else {

    Here too. (Still nitpicky)

othermachines’s picture

Nothing wrong with nitpicky. Here's another go.

phai’s picture

Ehmmm.... but it's just a my theory... or updating module to 7.x-2.4 (released today) this behaviour is still unfixed?

othermachines’s picture

Darn. Yeah, looks that way.

jackhutton’s picture

just updating to 7.x-2.4 and notice there's no toggle to 'show parent'.
I reran the patch in #66 which brought back my parent item onto the menu blocks

othermachines’s picture

@jackhutton - If you apply a patch, you have to keep applying it with every update until the patch is committed.

janchojnacki’s picture

Thanks for the patch, othermachines

There is part to remove the only item from the tree if it is the parent item configured to be included. But in my case it was needed to include the only item in the tree, so I a bit extended your patch to make removal of the only item optional.

brad.bulger’s picture

StatusFileSize
new5.52 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es).
[ View ]

this is a reroll of the patch from #66 against the current 2.x branch. i didn't include the extra feature from #71 only because i haven't used it.

othermachines’s picture

Re-rolled patch in #72 tests OK and applies cleanly. I think it's ready to commit. Thanks @brad.bulger.

@janchojnacki - Please apply the patch in #72 to latest -dev, then create a new patch with only your changes. Post it as a separate issue and indicate that it is dependent on #72 patch being applied/committed first. Thanks!

Pol’s picture

This patch is working perfectly on 7.x-2.x. Can't wait to see this in.

othermachines’s picture

@janchojnacki - Might be a good fit for your patch? Issue #2368805: Show always fixed parent item

caxy4’s picture

+1 for getting this included in 7.x-2.x. Please include ASAP!!!

perlgal’s picture

Hi

I applied the patch in #72 against the latest dev version and it works, but I have the same issue as mentioned in #37 and then revisited in #55 with a patch, that does not work on the latest dev with patch in #72.

Any suggestions will be helpful.

Thanks!

othermachines’s picture

Hi, @perlgal.

I have the same issue as mentioned in #37...

Can you be more explicit about your problem so that we can try to reproduce? I've confirmed that the fix introduced in #55 is included in #72 patch.

Please include your menu block settings.

perlgal’s picture

@othermachines Thank you for the quick reply!

Menu block for Submenu B
The starting level is 1st level primary
Include parent is checked.
Maximum Depth - Unlimited
Expand all children of this tree is checked.
Fixed parent item is

Works for
-Primary Item A
--Submenu B -> Works for submenu B

Does not work for
-Primary Item A
--Sub Menu A2
---Sub Menu B (under A2) -> Does not work. No menu displayed at all.
I hope this explains my issue. Thank you!

othermachines’s picture

@perlgal - It helps, thanks! To clarify, in both examples "Submenu B" is the fixed parent item?

perlgal’s picture

Yes. Submenu B is the fixed parent item.

othermachines’s picture

Status:Reviewed & tested by the community» Needs work

Thanks for catching that, @perlgal. I was able to reproduce the problem with your settings.

othermachines’s picture

So here's the problem...

When iterating over a fixed item's ancestors (p1, p2, p3...), if the fixed item starts at level 3 (or greater) it will always pass this condition on first iteration:

if (!empty($include_parent) && $plid != $parent_item['plid']) {
  $tree = array($key => $tree[$key]);
}

This is because the fixed item's immediate parent will never be equal to its first ancestor ('p1').

So if our tree looks like this:

1.1 <-- p1
.. 2.1 <-- p2
.. .. 3.1 <-- fixed item
.. .. .. 4.1
.. .. .. 4.2

The fixed item's immediate parent is 2.1. Let's iterate through its ancestors...

1.1 <-- p1

This doesn't match fixed item's immediate parent, so start the tree here. Wrong, wrong, wrong. Next iteration...

.. 2.1 <-- p2

It matches! But damn, already too late. We've messed up the trail. And what happens when we mess up the trail?

if (!$found_active_trail) {
  $tree = array();
  break;
}

Yep, that means no menu.

I ran out of time to implement a fix, but maybe someone else will get to it before I do.

othermachines’s picture

Status:Needs work» Needs review
StatusFileSize
new6.46 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es).
[ View ]
new1.31 KB

This patch also checks against the depth of the fixed item.

Melissamcewen’s picture

Patch from #84 works great for me