Problem/Motivation
Now that menu links are content entities, it would be nice if we could work with them in views, and perform the same kind of operations with them that are available for taxonomy terms and term references, so we can do things such as:
- https://stackoverflow.com/questions/28648532/drupal-views-filter-content...
- https://drupal.stackexchange.com/questions/2264/how-can-i-create-a-view-...
In Drupal 7, this might have involved using the Taxonomy Menu or Menu to Taxonomy modules (synchronizing a vocabulary to a menu, and any menu links to the relevant term references), but in Drupal 8 this should no longer be necessary.
Some work was also done in the Menu Node Views module (which is used on 7000+ sites, so there seems to be at least some demand for this).
Proposed resolution
Allow for menu link content entities to work with views, including filters and relationships linking them to nodes.
Remaining tasks
Decide whether we want this
Further clarify the kind of functionality related to menu links we want
Write patch
Review
User interface changes
N/A
API changes
N/A
Data model changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#23 | drupal-2777749-menu_link_content-views-support-23.patch | 1.93 KB | Steven Jones |
#7 | drupal-fix_notice_adding_menu_link_content_views_data_handler-2777749-7.patch | 660 bytes | SylvainM |
Issue fork drupal-2777749
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:
- 2777749-add-menu-link-content-support-10.2.x changes, plain diff MR !6933
- 2777749-add-menu-link-content-support-11.x changes, plain diff MR !6932
- 2777749-add-menu-link-content-support changes, plain diff MR !3150
- 10.2.x compare
- 11.x compare
- 2429321-verify-that-the compare
- 3312477-sa-core-2022-016-cherry-pick-82a7d4dd compare
Comments
Comment #2
stefan.r CreditAttribution: stefan.r commentedFor linking back menu links to nodes, #2315773: Create a menu link field type/widget/formatter would be perfect.
Comment #3
stefan.r CreditAttribution: stefan.r commentedComment #7
SylvainM CreditAttribution: SylvainM at Axess Open Web Services commentedYou can easily add the support by implementing hook_entity_type_alter():
But, there is a notice when editing view:
Notice: Undefined index: title in Drupal\views\Plugin\views\HandlerBase->adminLabel() (line 158 of core/modules/views/src/Plugin/views/HandlerBase.php).
It can be fixed by attached patch.
Comment #10
AnybodyHi @stefan.r,
I think #2804335: Provide menu item integration for views is closely related and seems to have the same objective. Would you like to read through it, perhaps merge both issues and add your guess about using views_menu_children_filter module as testspace for further implementation and features before writing a patch for core?
We should also discuss the implications from #2315773: Create a menu link field type/widget/formatter if that will ever be done...
What do you think?
Comment #11
AnybodyUsing the patch from #7 plus the hook:
works to enable views to display menu item entities standalone, BUT the problem is, that entity relationships don't exist with it and that's the main criteria for typical use cases. So menu items don't reference entities and also entities can't reference menu items.
So this is just a very very early starting point or for very simple use cases.
I guess there are two ways to go now:
a) Decide if #2315773: Create a menu link field type/widget/formatter (https://www.drupal.org/project/menu_link in contrib) will happen and if it will, we'd just have to extend what comes from the reference for better usability (see my comment #177 in that issue)
or
b) If the current logic is kept, I'd suggest to extend views_menu_children_filter including #2996145: Support non-content entities to not only support nodes, which wouldn't be enough in the long term view. Based on that module a core patch could be written if the module works well.
How do we proceed and how may decide on #2315773: Create a menu link field type/widget/formatter? Can we ask important core maintainers about that?
In the meantime a solution like #7 can help as quickfix?
Comment #17
somersoft CreditAttribution: somersoft commentedWith a strong hint from the code in core/modules/block_content/src/BlockContentViewsData.php, using hook_views_data_alter() gives the ability to create a views relationship to the information held in menu_link_content_data.
Thank you to the person who wrote that code. alexpott ?
Comment #18
somersoft CreditAttribution: somersoft commentedComment #22
AnybodyComment #23
Steven Jones CreditAttribution: Steven Jones at ComputerMinds commentedHere's a patch that builds on the work from #7 essentially doing away with needing the hook implementation.
Comment #24
AnybodyThanks @Steven Jones, looks good, and I think it's okay to set this Needs Review, but please keep in mind that at least in a follow-up issue we need to add relations as written in #11.
I guess this will also needs tests, but didn't check yet, if there are tests for all other views enabled entity types in Core? But I think so...
Comment #26
AnybodyCreated a MR to proceed from #23. Please do NOT create further patches, but instead work on the MR.
Comment #32
hswong3i CreditAttribution: hswong3i at PantaRei Design Limited (Hong Kong) commented#26 missing the additional
core/modules/menu_link_content/src/MenuLinkContentViewsData.php
Additional MR for 10.2.x and 11.x had been created.
Comment #33
hswong3i CreditAttribution: hswong3i at PantaRei Design Limited (Hong Kong) commentedComment #34
smustgrave CreditAttribution: smustgrave at Mobomo commentedSounds interesting!
Will need some test coverage.
MR should be against 11.x too.