Problem/Motivation
As a user I want to add fields to my menu using the Menu Item Extras module.
Currently this is not possible as the Menu Items Extras module removes the menu_link_content--menu_link_content resource and replaces it with menu_link_content--MENU_ID resources.
Steps to reproduce
Install JSON:API Menu Items and Menu Link Extras and try to access a JSON:API Menu Items endpoint.
Proposed resolution
1. Stop the modules breaking when both enabled.
2. Ensure JSON:API Menu Items output field data.
Remaining tasks
1. Stop the modules breaking when both enabled.
2. Ensure JSON:API Menu Items output field data.
User interface changes
N/A
API changes
Data model changes
Comments
Comment #2
decipheredThis is related to #3217476, but I'm not sure that that issue was caused by Menu Item Extras.
I have created a patch that resolves the Null resource type error when Menu Item Extras is installed, but it might be better to look at a generic solution as well, removing the dependency on the
menu_link_contentresource type and providing an alternative fallback resource type.The patch doesn't yet expose the Menu Item Extras fields, so more work is required either way.
Comment #3
cweagansUpdated the patch above to add support for displaying field values (+ use DI for various services).
Comment #5
cweagansComment #6
cweagansComment #7
agiraud commentedThanks for the patch in #5 @cweagans !
I was implementing custom icon fields for my Drupal menu (with menu item extras) and I wanted to display it on my nextjs frontend, your patch is working great (Drupal core 9.4).
Comment #8
eglawWith addition of the menu_item_extras module (the one that adds the possibility to extend simple menu_link_contents with Field UI, and basically transforming it in a fully usable content entity, it also introduces it to the View Modes. The previous patch (although completely functioning and awesome, it lacks the including the info of the view_mode.
I've modified it a little to include the missing info.
Comment #9
switcode commentedI've added event dispatcher AdditionalFieldsEvent to add extra fields the response
I've added EventSubscriber MenuItemExtraSubscriber to add extra Menu items fields to the response
Comment #10
rakesh.gectcr#9 works fine with Drupal 10!
Comment #11
bojan_dev commentedRerolled #7 + added some coding standards fixes
Comment #14
arlina commentedIn #13: created a PR from patch #11, and it also adds caching to the getRouteResourceTypes() method, as that was causing memory issues on sites with a large number of menus.
Comment #15
rdoepner commentedAdd language support.
Comment #16
mglamanI'll pick this up to review and get merged!
Comment #17
mglamanHiding some patches. Need to see what #15 provides that isn't in the MR. No interdiff, but language support is a good thing. Or it should be done in #3192576: Add langcode & fix content caching
Comment #18
mglamanWrote a test and it looks like this MR broke when a menu has plugin-based menu links, working on a fix. See https://git.drupalcode.org/project/jsonapi_menu_items/-/merge_requests/1...
Comment #19
mglamanThe MR addresses #15 by leveraging `\Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::getEntity` which loads the entity from context for the proper translation of entity fields.
Comment #20
mglamanOkay, I think this is all set for review now! Thanks everyone for the work. I made it a bit more generic so that if anyone made menu_link_content fieldable without menu_item_extras it also works.
Comment #21
mglamanMarking issue credit
Comment #22
mglamanThis passed several rounds on internal QA on our side, I'm going to merge so we can unblock #3192576: Add langcode & fix content caching
Comment #24
mglaman