Add alter for simplifiedLink

Created on 4 October 2021, about 3 years ago
Updated 15 August 2024, 3 months ago

We are using Menu Item Extras to add fields to the menu items. These get lost with Simplified Menu. I tried finding a generic way to have the fields be kept but didn't have any luck, so I settled on adding an alter which can then be used to add in the items in your own code.

Here is a sample of custom code to use with this alter hook to add fields added with Menu Item Extras:

function MYMODULE_simplify_menu_simplified_link_alter(array &$simplifiedLink, MenuLinkInterface $link) {
  $entity = \Drupal::service('menu_item_extras.menu_link_tree_handler')->getMenuLinkItemEntity($link);

  // If the menu item has a card reference, add its render array.
  if ($entity->hasField('field_card') && !$entity->get('field_card')->isEmpty()) {
    $simplifiedLink['card'] = $entity->get('field_card')->view('menu_block_card');
  }

  // If the menu item has an icon, add it.
  if ($entity->hasField('field_icon') && !$entity->field_icon->isEmpty()) {
    $simplifiedLink['asset'] = $entity->field_icon->entity->field_image->view('svg');
  }
}
✨ Feature request
Status

Fixed

Version

3.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States michelle Wisconsin, USA

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