Mar 14, 2017 in Drupal 8, Snippets

With this simple snippet, menu items are saved as expanded when you save your node. In some cases, this is a necessary functionality.

<?php
use Drupal\Core\Entity\EntityInterface;

 /**
  * Implements hook_ENTITY_TYPE_presave().
  */
function MYMODULE_menu_link_content_presave(EntityInterface $entity) {
    if ($entity->menu_name->value == 'main') {
        $entity->expanded = 1;
    }
}