Score:-2

Get MenuLinkContent object values

np flag

get title and link of a menu link object in. I am trying to get title and link of a MenuLinkContent object and below is my code. I want to get title and link value. How I can get the title and Link url without foreach. something like- menu_content[]->title and menu_content[]->link

Defined name spaces

use Drupal\menu_link_content\Plugin\Menu;
use Drupal\menu_link_content\Entity\MenuLinkContent;

Code in

 function build(){

       $menu_content = current(\Drupal::entityManager()->getStorage('menu_link_content')->loadByProperties(array('id' => 123)));
  $menu_content_title =  $menu_content->values;
  \Drupal::logger('module_name')->notice('<pre><code>' . print_r($menu_content, TRUE) . '</code></pre>' );
    }

output:

Drupal\menu_link_content\Entity\MenuLinkContent Object
(
[insidePlugin:protected] => 
[values:protected] => Array
    (
        [id] => Array
            (
                [x-default] => 123
            )

        [revision_id] => Array
            (
                [x-default] => 124
            )

        [bundle] => Array
            (
                [x-default] => menu_link_content
            )

        [uuid] => Array
            (
                [x-default] => 8930a948-921e-4fasddb-be34-c98bcd6ae4c3
            )

        [langcode] => Array
            (
                [x-default] => en
            )

        [revision_user] => Array
            (
                [x-default] => 
            )

        [revision_created] => Array
            (
                [x-default] => 1626345445
            )

        [revision_log_message] => Array
            (
                [x-default] => 
            )

        [revision_default] => Array
            (
                [x-default] => 1
            )

        [isDefaultRevision] => Array
            (
                [x-default] => 1
            )

        [enabled] => Array
            (
                [x-default] => 1
            )

        [title] => Array
            (
                [x-default] => Example title
            )

        [description] => Array
            (
                [x-default] => 
            )

        [link] => Array
            (
                [x-default] => Array
                    (
                        [uri] => https://www.example.com
                        [title] => 
                        [options] => Array
                            (
                                [fa_icon] => 
                                [fa_icon_prefix] => fa
                                [fa_icon_tag] => i
                                [fa_icon_appearance] => before
                                [attributes] => Array
                                    (
                                        [class] => Array
                                            (
                                            )

                                        [target] => _blank
                                    )

                            )

                    )

            )

Got Title $title = $menu_content->getTitle(); but Url not getting After apply code: $url = $menu_content->get('link')->first()->getUrl();

Drupal\Core\Url Object
(
[urlGenerator:protected] => 
[urlAssembler:protected] => 
[accessManager:protected] => 
[routeName:protected] => 
[routeParameters:protected] => Array
    (
    )

[options:protected] => Array
    (
        [fa_icon] => 
        [fa_icon_prefix] => fa
        [fa_icon_tag] => i
        [fa_icon_appearance] => before
        [attributes] => Array
            (
                [class] => Array
                    (
                    )

                [target] => _blank
            )

        [external] => 1
    )

[external:protected] => 1
[unrouted:protected] => 1
[uri:protected] => https://www.example.com
[internalPath:protected] => 
[_serviceIds:protected] => Array
    (
    )

[_entityStorages:protected] => Array
    (
    )
Jaypan avatar
de flag
I'm not sure what you are asking. Can you give your use case?
New Dev avatar
np flag
@Jaypan I written code in file and static title and urls but I want to call the title and url from the menu. So [title] => Array ( [x-default] => Example title ) [link] => Array ( [x-default] => Array ( [uri] => https://www.example.com } }
Score:0
cn flag

You didn't tell us why you've loaded the entity. You usually can get this information directly from the menu link plugin via getUrlObject(), without knowing whether there is an underlying entity or the menu link is defined differently, for example in a YAML file.

The entity has a Link field with the name link:

$url = $menu_content->get('link')->first()->getUrl();

See How to get the valid URL of a Link field from within a Twig template?.

The title is available for both, plugin or entity, by the same method:

$title = $menu_content->getTitle();
New Dev avatar
np flag
$url = $menu_content->get('link')->first()->getUrl(); is not working. Please check the output in the question
4uk4 avatar
cn flag
The output looks OK to me. You can use the URL object as it is, to build a link and render it in a Twig template. Convert it to a string with `$url->toString()`, but only if you need it now and don't want to render it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.