function _menu_ui_node_save

Same name and namespace in other branches
  1. 9 core/modules/menu_ui/menu_ui.module \_menu_ui_node_save()
  2. 8.9.x core/modules/menu_ui/menu_ui.module \_menu_ui_node_save()
  3. 10 core/modules/menu_ui/menu_ui.module \_menu_ui_node_save()

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.

2 calls to _menu_ui_node_save()
LayoutBuilderContentModerationIntegrationTest::testLayoutModeration in core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php
Tests that Layout changes are respected by Content Moderation.
menu_ui_form_node_form_submit in core/modules/menu_ui/menu_ui.module
Form submission handler for menu item field on the node form.

File

core/modules/menu_ui/menu_ui.module, line 94

Code

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() && $node->isTranslatable()) {
            if (!$entity->hasTranslation($node->language()
                ->getId())) {
                $entity = $entity->addTranslation($node->language()
                    ->getId(), $entity->toArray());
            }
            else {
                $entity = $entity->getTranslation($node->language()
                    ->getId());
            }
        }
        else {
            // Ensure the entity matches the node language.
            $entity = $entity->getUntranslated();
            $entity->set($entity->getEntityType()
                ->getKey('langcode'), $node->language()
                ->getId());
        }
    }
    else {
        // Create a new menu_link_content entity.
        $entity = MenuLinkContent::create([
            '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 = $values['weight'] ?? 0;
    $entity->isDefaultRevision($node->isDefaultRevision());
    $entity->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.