You are here

function _micon_menu_menu_ui_node_save in Micon 2.x

Same name and namespace in other branches
  1. 8 micon_menu/micon_menu.module \_micon_menu_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.

See also

_menu_ui_node_save()

1 call to _micon_menu_menu_ui_node_save()
micon_menu_menu_ui_form_node_form_submit in micon_menu/micon_menu.module
Form submission handler for menu item field on the node form.

File

micon_menu/micon_menu.module, line 147
Contains micon_menu.module.

Code

function _micon_menu_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([
      'link' => [
        'uri' => 'entity:node/' . $node
          ->id(),
      ],
      'langcode' => $node
        ->language()
        ->getId(),
    ]);
    $entity->enabled->value = 1;
  }

  // Clean up options.
  $values['options']['attributes'] = array_filter($values['options']['attributes']);
  $entity->title->value = trim($values['title']);
  $entity->description->value = trim($values['description']);
  $entity->link->options = $values['options'];
  $entity->menu_name->value = $values['menu_name'];
  $entity->parent->value = $values['parent'];
  $entity->weight->value = isset($values['weight']) ? $values['weight'] : 0;
  $entity
    ->save();
}