function simple_megamenu_form_menu_link_edit_alter in Simple Mega Menu 2.0.x
Same name and namespace in other branches
- 8 simple_megamenu.module \simple_megamenu_form_menu_link_edit_alter()
Implements hook_form_FORM_ID_alter().
For routing menu link item added by modules, we need to alter the form differently than the menu link content form. The menu link item is here a Plugin.
File
- ./
simple_megamenu.module, line 188 - Contains simple_megamenu.module.
Code
function simple_megamenu_form_menu_link_edit_alter(&$form, FormStateInterface $form_state, $form_id) {
// Currently we can't save options on Menu link plugin because
// \Drupal\Core\Menu\MenuLinkDefault doesn't allow to override options (see
// $overrideAllowed variables).
// And \Drupal\Core\Menu\StaticMenuLinkOverrides::saveOverride too.
// Once https://www.drupal.org/node/2656534 was in, we could save the Plugin
// options definition.
// @see https://www.drupal.org/node/2656534
return;
// phpcs:disable
$build_info = $form_state
->getBuildInfo();
/** @var \Drupal\Core\Menu\MenuLinkDefault $menu_link_default */
$menu_link_default = $build_info['args'][0];
$menu_link_options = $menu_link_default
->getOptions();
$menu_name = $menu_link_default
->getMenuName();
/* @var \Drupal\simple_megamenu\SimpleMegaMenuHelperInterface $simple_megamenu_helper */
$simple_megamenu_helper = \Drupal::service('simple_megamenu.helper');
$allowed_simple_mega_menu_types = $simple_megamenu_helper
->getMegaMenuTypeWhichTargetMenu($menu_name);
$allowed_simple_mega_menu_types = array_keys($allowed_simple_mega_menu_types);
if ($allowed_simple_mega_menu_types) {
// The default value.
$simple_mega_menu = NULL;
// The default description for the entity reference form.
$description = t('Select a simple mega menu entity to use on this menu link.');
$simple_mega_menu_id = isset($menu_link_options['attributes']['data-simple-mega-menu']) ? $menu_link_options['attributes']['data-simple-mega-menu'] : NULL;
if ($simple_mega_menu_id) {
$simple_mega_menu = $simple_megamenu_helper
->getSimpleMegaMenu($simple_mega_menu_id);
if ($simple_mega_menu instanceof SimpleMegaMenuInterface) {
$destination = \Drupal::destination()
->getAsArray();
$edit_path = Url::fromRoute('entity.simple_mega_menu.edit_form', [
'simple_mega_menu' => $simple_mega_menu
->id(),
], [
'query' => $destination,
])
->toString();
$description = t('Select a simple mega menu entity to use on this menu link. Edit the entity referenced <a target="_blank" href="@edit_path">here</a>.', [
'@edit_path' => $edit_path,
]);
}
}
$form['simple_mega_menu'] = [
'#title' => t('Simple Mega Menu'),
'#description' => $description,
'#type' => 'entity_autocomplete',
'#default_value' => $simple_mega_menu,
'#target_type' => 'simple_mega_menu',
'#selection_handler' => 'default',
'#selection_settings' => [
'target_bundles' => $allowed_simple_mega_menu_types,
],
];
$form['#submit'][] = 'simple_megamenu_menu_link_default_form_submit';
}
// phpcs:enable
}