1

In the Drupal 8 screen for editing a node, we have these two sections:

URL Path Settings > URL alias

Menu Settings (Menu link title, Description, Parent item, Weight)

How, given the URL alias of a node, can we get those Menu Settings, particularly the Menu link title?

This may break down into two separate problems:

  1. finding the node ID from the URL alias
  2. finding the Menu Settings from the node ID

and in fact I would be thrilled at a solution to either one, even as a high-level explanation.

I would go into detail about how I've attempted to answer this question already, and the services & classes that appear to be relevant, but I don't want to post a complicated-looking question. If data is that easy to put into those basic fields in the UI, it should at least be possible for some module code to get it back out.

2
  • Maybe use entity query on the menu_link_content and check for link__uri(maybe only uri will work) condition for entity:node/1
    – user21641
    Commented Mar 11, 2016 at 9:09
  • thank you, I think there may be a complete solution by combining this with the url_alias table lookup answer. Will test both parts soon (for breadcrumb application) & post results.
    – rphair
    Commented Mar 13, 2016 at 11:42

2 Answers 2

2

Comments in my question getting URL from Route: breaks in localhost subdirectory have clarified that the solution of @4k4 is absolutely the right way to get the alias from the current path. But the alias is not as useful as I thought in applying suggestion (@Ivan Jaros) to use entityQuery to take it the rest of the way, since the current path is able to match the link.uri field of menu_link_content, except for a leading slash in the pathname and the entity: protocol:

$current_path = \Drupal::service('path.current')->getPath();
$system_uri = 'entity:' . trim($current_path, '/');

$query = \Drupal::entityQuery('menu_link_content')
  ->condition('link.uri', $system_uri)
  ->condition('menu_name', 'main');

$result = $query->execute();
$menu_link_id = (!empty($result)) ? reset($result) : FALSE;

if ($menu_link_id) {
  $menu_link = MenuLinkContent::load($menu_link_id);
  $menu_link_title = $menu_link->getTitle();
} else {
  drupal_set_message(t('no Menu Link Content for ID that we found...'), 'error');
}
1
  • If you are trying to get a menu link for an entity uri besides the one at the current path it may be helpful to know the format for the uri is entity:node/123
    – ummdorian
    Commented Jul 5, 2017 at 14:37
1

I only can answer the first part of the question.

The path to the node is not a basic field, as it appears to be because it is in the node form beside the basic fields.

The url aliases are stored in the table "url_alias".

You can access the data directly or use the alias manager to access the alias by providing the node id or vice versa to get the path which then contains the node id:

$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$nid);
$path = \Drupal::service('path.alias_manager')->getPathByAlias($alias);

Your Answer

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

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