2

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:

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.

enter image description here

2 Answers 2

4

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->expanded->value = 1;
}
0

The "Show as expanded - always" module might be the answer?

https://www.drupal.org/project/show_as_expanded_always

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.