New Drupal module: Menu item content fields
I like Drupal for its little building blocks that let you add on top of other foundations and reuse the components in ways that were never imagined initially.
Recently I discovered that menu items (one of those building blocks) had the potential to be a little more flexible so I created a Drupal module to enable fields functionality in menu items.
It was a surprise to discover that custom menu items (the ones that are created using the "add link" button) are already "content entities" just like nodes, users or taxonomy terms. The only missing piece is to have a user interface to add the fields and then change the way the menu link entity is rendered to show all the fields configured for the entity.
Motivation
The best moment to develop a quick contributed module is when your scope is limited so you don't need to overcomplicate the implementation and end up never publishing it. This one of the examples on how to develop software that is upstreamable or shareable with the community.
In my case I just wanted to show a little icon next to the menu link and I felt the most Drupaly way of doing it was to have the icon as a field with the Font Awesome Fields module. You can see it in action at bottom of this page (or at least at the beginning of 2020).
This module allows to do so much more than icons, for example you can architect a so called "megamenu". This is why I didn't want list modules that add icons to menus as similar since it might look like this module is only useful for icons. It is up to the creativity of the site builder.
Extra features
Since making the entity fieldable also introduces the concept of view modes for menu items a new type of block is available. This blocks allow the site builder to choose the view mode a menu is going to use for rendering. On top of that you can also define a field in the menu item that will store the view mode a particular item will use for rendering overriding the one chosen when configuring the block or the default.
Room for improvement
As mentioned above only custom menu items are supported but a menu can contain other kinds of links like those created by the Views module or defined in a static YAML file. How to achieve more menu items other than the custom one to have content from fields is something that needs more thought but it will probably involve some serialization to be able to store the field data in the configuration.
Alternatives
Menu item extras is the most similar module and in fact is the first module I tried to achieve my goal. I even shared a fix while doing the initial tests but soon I noticed that it was a bit overcomplicated and realized I could start a fresh approach for sites that have simpler needs.
Plan to integrate it into Drupal core
Menu link content fields performs some workarounds that could be simplified with just some lines of code in Drupal core and there is no harm in having an entity fieldable if you don't add fields to it. For those reasons and some more I proposed this plan to include the functionality in Drupal core. If it succeeds it will add more features to Drupal core and it will also simplify other modules like Menu Link Extras and maybe it will encourage other modules that add functionality to menu items to do it in way that is based on configurable fields.
The ideal situation would be for Menu Item Content Fields not to exist and have it included in core but in the meantime I'm glad to announce that is compatible with Drupal 9 that is scheduled to come this year!
This is so far the introduction to this simple Drupal module, you can try it and give your opinion on the plan to include in Drupal core.
Main photo by Ales Krivec on Unsplash