public function MenuLinkContentService::nodeSave in Menu Item Extras 8.2
Helper function to create or update a menu link for a node.
Parameters
\Drupal\Core\Entity\EntityInterface $node: Node entity.
array $values: Values for the menu link.
Return value
\Drupal\menu_link_content\MenuLinkContentInterface Menu item.
See also
1 call to MenuLinkContentService::nodeSave()
- MenuLinkContentService::nodeSubmit in src/
Service/ MenuLinkContentService.php - Form submission handler for menu item field on the node form.
File
- src/
Service/ MenuLinkContentService.php, line 279
Class
- MenuLinkContentService
- Class MenuLinkContentHelper.
Namespace
Drupal\menu_item_extras\ServiceCode
public function nodeSave(EntityInterface $node, array $values) {
$menu_link_content_storage = $this->entityTypeManager
->getStorage('menu_link_content');
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
if (!empty($values['entity_id'])) {
$entity = $menu_link_content_storage
->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 = $menu_link_content_storage
->create([
'link' => [
'uri' => 'entity:node/' . $node
->id(),
],
'langcode' => $node
->language()
->getId(),
]);
$entity
->set('enabled', 1);
}
$entity
->set('title', trim($values['title']));
$entity
->set('description', trim($values['description']));
$entity
->set('menu_name', $values['menu_name']);
$entity
->set('bundle', $values['menu_name']);
$entity
->set('parent', $values['parent']);
$entity
->set('weight', isset($values['weight']) ? $values['weight'] : 0);
$entity
->save();
return $entity;
}