Store mega menu reference in a property or field instead on menu_link instead of an option

Created on 21 July 2022, over 2 years ago
Updated 9 September 2024, 2 months ago

Problem/Motivation

This is closely related to #3298751: Expose the Mega Menu widget as field (widget?) on the menu item but goes one step further. If this is implemented, that issue will presumably also be solved.

Having a look at the current implementation in the .module file, the reference from the menu_link to the mega menu entity is saved in the menu links "options":

/**
 * Submit function for menu add / edit form.
 */
function simple_megamenu_menu_link_content_form_submit($form, FormStateInterface $form_state) {
  $menu_link = $form_state->getFormObject()->getEntity();
  $menu_link_options = $menu_link->link->first()->options;

  if ($form_state->getValue('simple_mega_menu')) {
    $data_simple_mega_menu = [
      'attributes' => [
        'data-simple-mega-menu' => $form_state->getValue('simple_mega_menu'),
      ],
    ];
    $menu_link->link->first()->options = array_merge_recursive($menu_link_options, $data_simple_mega_menu);
  }

  else {
    if (isset($menu_link_options['attributes']['data-simple-mega-menu'])) {
      unset($menu_link_options['attributes']['data-simple-mega-menu']);
    }
    $menu_link->link->first()->options = $menu_link_options;
  }

  $menu_link->save();
}

This seems a bit like a workaround, as it's just put in as data attribute. Instead, it would be nice to implement the reference as property or field.
Also this would make make the widget and formatter more flexible and Drupal-like. For example the position could be changed without a hard coded weight.

If the data-attribute is required or helpful for the frontend, of course it could still be used additionally, but not for storage.

Steps to reproduce

Proposed resolution

Remaining tasks

  1. Create a 3.x branch
  2. Implement
  3. Migrate values over from options using an update hook
  4. Test
  5. Release

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024