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 "Show as expanded" 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 &$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.