Set MenuLinkContent getEntity to public visibility

Created on 6 September 2018, about 6 years ago
Updated 17 July 2023, over 1 year ago

Problem/Motivation

When dealing with Menu Link plugins programmatically, a developer might need to retrieve the content entity to access their fields or translations, at the moment the current best way to do this, seems to load the the entity using the uuid.

Steps to reproduce

N/A

Proposed resolution

Make the getEntity method public so it can be used from the \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent plugin.

Remaining tasks

- Approve the change
- Find a suitable test.
- Create a Change Record.

User interface changes

N/A

API changes

Visibility of MenuLinkContent::getEntity changed to public.

Data model changes

N/A

Release notes snippet

Before the change (extracted from) https://drupal.stackexchange.com/questions/235754/get-menu-link-item-fro...

if ($link instanceof \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent) {
  $uuid = $link->getDerivativeId();
  $entity = \Drupal::service('entity.repository')
    ->loadEntityByUuid('menu_link_content', $uuid);
  $field_value = $entity->field_example->value;
}

After:

if ($link instanceof \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent) {
  $entity = $link->getEntity();
  $field_value = $entity->field_example->value;
}

Original report by Grimreaper

Hello,

When manipulating menu, it would be convenient to be able to access the menu link content entity easily.

Setting the visibility of the getEntity method would help.

I will upload a patch.

Feature request
Status

Fixed

Version

11.0 🔥

Component
Menu link content 

Last updated 4 months ago

Created by

🇫🇷France Grimreaper France 🇫🇷

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