0

I would like to create a node with a menu link programmatically. When the operation is complete, I would like the user to see the relationship between the node and the menu link under "Menu Settings" of the node.

I am aware of this post:

Programmatically create a node with a menu link

This shows the mechanics of creating a node and creating a menu link. It does not show how to build the relationship between the node and menu link so it is available in "Menu Settings" of the node for a user working with the Drupal GUI. How can I build this relationship?

1

To create a menu link for a node programmatically and to see it in the node edit form you have to connect the link uri to the node entity:

$item = \Drupal\menu_link_content\Entity\MenuLinkContent::create([
  'link' => ['uri' => 'entity:node/' . $node->id()],
  'title' => 'Menu item for node ' . $node->id(),
  'menu_name' => 'main',
]);
$item->save();
  • This is moving in the direction -- thank you 4k4! I have tested this and it works with 'menu_name' = 'main'. In my case I have three custom menus that I created in code. One each for: English, Spanish and Chinese. When I try to use one of these menu names, no entry exists in the node edit. I looked at the configuration differences between 'main' and my three menus. I noticed that 'locked: true' for 'main'. My three menus have 'locked: false'. Could this have something to do with why I am having problems? – Greg Sims Feb 23 '18 at 22:26
  • Please see my comment above @4k4. – Greg Sims Feb 23 '18 at 22:35
  • I tested this again and set 'locked: true' when creating each of my three menus. This was not the solution I hoped for. The three menus are created as before. There is still no data in "Menu Settings" of the associated nodes. @4k4 – Greg Sims Feb 23 '18 at 23:05
  • While there was a straightforward answer to your first question for the relationship, the follow-up about translation is difficult to answer in one post. There are different strategies to translate menus. The simple one is to translate the menu items. Different menus is a more advanced approach. One thing to check first, if you want to see the menu items of different menus you have to allow them in the configuration of the content type. And for the first approach, translating menu items in the node edit form, there is still an open issue drupal.org/project/drupal/issues/2648750 – 4k4 Feb 24 '18 at 9:17
  • You are correct @4k4. I added my custom menus to the content type and English is now working well -- both the menu itself and the node edit menu settings. Spanish and Chinese are still an issue with node edit. When I set 'menu_name' => 'spanish' and 'langcode' => 'es', the menu_link_content is created correctly in the spanish menu. The node edit menu settings exists but always points to the english menu. Could this be the issue you pointed out above? I can't tell for sure. – Greg Sims Feb 24 '18 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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