-2

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
    (
    )
2
  • 1
    I'm not sure what you are asking. Can you give your use case?
    – Jaypan
    Mar 28 at 13:48
  • @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] => example.com } }
    – New Dev
    Mar 28 at 16:05

1 Answer 1

0

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();
2
  • $url = $menu_content->get('link')->first()->getUrl(); is not working. Please check the output in the question
    – New Dev
    Mar 29 at 9:36
  • 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.
    – 4uk4
    Mar 29 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.