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
(
)