menu_lock-1.0.0-rc1/menu_lock.module
menu_lock.module
<?php
/**
* @file
* Hook implementations for module menu_lock.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter().
*
* Limit menu link 'Parent link' to the current menu when using the Menu UI at
* /admin/structure/menu/item/%mlid/edit. This prevents users from moving items
* from one menu (e.g. footer) to another (e.g. main menu).
*
* @see https://www.drupal.org/project/drupal/issues/3110371
* @see https://www.drupal.org/node/3250632
*
* @todo deprecate when supported by Drupal core
*/
function menu_lock_form_menu_link_content_menu_link_content_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// Retrieve current user account + menu link object.
$account = \Drupal::currentUser()->getAccount();
/** @var Drupal\menu_link_content\Form\MenuLinkContentForm */
$form_object = $form_state->getFormObject();
/** @var Drupal\menu_link_content\Entity\MenuLinkContent */
$menu_link = $form_object->getEntity();
// Limit options in 'Parent link' field.
if (!empty($form['menu_parent']['#options']) && !$account->hasPermission('move menu links across menus')) {
foreach ($form['menu_parent']['#options'] as $parent => $menu_link_title) {
$parent_parts = explode(':', $parent);
$parent_menu_name = $parent_parts[0];
if ($parent_menu_name != $menu_link->getMenuName()) {
unset($form['menu_parent']['#options'][$parent]);
}
}
}
}
