_menu_ui_node_save(NodeInterface $node, array $values)
Helper function to create or update a menu link for a node.
Parameters
\Drupal\node\NodeInterface $node: Node entity.
array $values: Values for the menu link.
File
- core/modules/menu_ui/menu_ui.module, line 137
- Allows administrators to customize the site's navigation menus.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function _menu_ui_node_save(NodeInterface $node , array $values ) { /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */ if (! empty ( $values [ 'entity_id' ])) { $entity = MenuLinkContent::load( $values [ 'entity_id' ]); if ( $entity ->isTranslatable()) { if (! $entity ->hasTranslation( $node ->language()->getId())) { $entity = $entity ->addTranslation( $node ->language()->getId(), $entity ->toArray()); } else { $entity = $entity ->getTranslation( $node ->language()->getId()); } } } else { // Create a new menu_link_content entity. $entity = MenuLinkContent::create( array ( 'link' => [ 'uri' => 'entity:node/' . $node ->id()], 'langcode' => $node ->language()->getId(), )); $entity ->enabled->value = 1; } $entity ->title->value = trim( $values [ 'title' ]); $entity ->description->value = trim( $values [ 'description' ]); $entity ->menu_name->value = $values [ 'menu_name' ]; $entity ->parent->value = $values [ 'parent' ]; $entity ->weight->value = isset( $values [ 'weight' ]) ? $values [ 'weight' ] : 0; $entity ->save(); } |
Please login to continue.