We have repeatedly had a requirement for projects to add a sub-menu to a content area of a page (for which menu_block automatically becomes a default choice), but these menu items are regularly required to be displayed as view modes of the content they represent (more often than not as a teaser). So we have created a patch to allow us to manage this from within the block menu. I thought I'd share this as potentially useful functionality for this module, or alternatively we could look at building a new module to do this - should the feeling be that this functionality does not fit within the intention of this module.

This code has not yet been thoroughly tested or used within a yet active project.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andywhale created an issue. See original summary.

andy_w’s picture

Assigned: andy_w » Unassigned
JohnAlbin’s picture

+    foreach ($build['#items'] as &$item) {
+      $params = $item['url']->getRouteParameters();
+      $ids[] = $item['entity_id'] = $params['node'];
+    }

$params['node'] won't exist for menu links that point at pages that aren't node pages.

This is a very interesting feature, but it seems like it would only work in certain, specific menu set ups. Menu block's options are designed to work together and on any menu.

Perhaps Menu Block could add an "module_additions" key to its config so that 3rd party module's could store form-altered settings inside the Menu Block's config? Menu block would need to add an alter hook for its build.

andy_w’s picture

Status: Active » Needs review
FileSize
1.87 KB

Thanks JohnAlbin.

I have now created a sandbox module to handle menu items as view modes, but this requires some method of hooking into and storing settings as per comments, so the patch is attached.

andy_w’s picture

FileSize
1.79 KB

Patched to work in line with the latest release plus 2757215 which looks like it should be approved in the form its in.

andy_w’s picture

Fixed against latest release

Chris Matthews’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
Checking patch config/schema/menu_block.schema.yml...
Hunk #1 succeeded at 29 (offset 12 lines).
Checking patch src/Plugin/Block/MenuBlock.php...
Hunk #1 succeeded at 174 (offset 73 lines).
error: while searching for:
    $this->configuration['expand'] = $form_state->getValue('expand');
    $this->configuration['parent'] = $form_state->getValue('parent');
    $this->configuration['suggestion'] = $form_state->getValue('suggestion');
  }

  /**

error: patch failed: src/Plugin/Block/MenuBlock.php:134
error: src/Plugin/Block/MenuBlock.php: patch does not apply
komalk’s picture

Status: Needs work » Needs review
FileSize
1.8 KB

Worked on #7.
Re-rolled the patch.

Status: Needs review » Needs work

The last submitted patch, 8: 2779465-8.patch, failed testing. View results

oli-chowdhury’s picture

Hi,

Is there any update on the D9 compatible version?

@andy_w just wonder if you have worked on any further on this patch?

andy_w’s picture

Hi @oli-chowdhury,

I have just pushed up a Drupal 9 compatible version of the menu block view modes sandbox module (https://git.drupalcode.org/sandbox/andywhale-2796357/-/tree/2.x).

But I am told that the patch above would require the 1.x-dev@dev version of menu_block, which should work.

Dave Reid’s picture

Status: Needs work » Postponed (maintainer needs more info)

Don't blocks already support third_party_settings? For example: https://www.drupal.org/project/block_titlelink/issues/2929570 and https://www.drupal.org/project/2345855/issues/2425675 are two examples of block-related modules adding third party settings to block forms to store data. I think whatever is needed for this can possibly use the same system and this change here wouldn't be necessary.

andy_w’s picture

I had overlooked this @dave-reid, thank you :-)

The patch will work with the version of my sandbox module, but you're right at some point I will try and refactor to make use of third_party_settings (a much better approach).