How to check "Show as expanded" by default on the Add menu item page I need the 'Show as Expanded' checkbox on the Add menu item page to be selected by default.

I created a module, 'expand_menu_items', and tried a hook_form_alter function to get the id, apparently it's 'menu_link_content_menu_link_content_form'.

I know I'm looking for #default_value, there are a couple and tried setting them to 'TRUE' but it's not having an effect on the page (xdebug shows the value as true). What could I be doing wrong?

Similar questions exist here:


Drupal 8 Menu always show as expanded - Though I may use the sql query to fix existing menu items, I'm more concerned with new menu items.
How to set the default &quot;Show as expanded&quot; to true in a custom menu item - This seemed promising, although in this case, all the menu items were added by end users.


Here's the function code:

function expand_menu_items_form_alter($form, FormStateInterface &amp;$form_state, $form_id) {
if ($form_id == 'menu_link_content_menu_link_content_form') {
    // $form['expanded']['#default_value'] = true; // Doesn't work.
    $form['expanded']['widget']['value']['#default_value'] = true; // Doesn't work either.

}


}

Attached below is an image to the form page in question.
To set a default value for the menu link entity use hook_ENTITY_TYPE_create:

use Drupal\menu_link_content\MenuLinkContentInterface;

/**
 * Implements hook_ENTITY_TYPE_create() for menu_link_content.
 */
function mymodule_menu_link_content_create(MenuLinkContentInterface $menu_link_content) {
  $menu_link_content-&gt;expanded-&gt;value = 1;
}