3 * Menu UI admin behaviors.
6 (function ($, Drupal) {
9 * @type {Drupal~behavior}
11 Drupal.behaviors.menuUiChangeParentItems = {
12 attach(context, settings) {
13 const $menu = $('#edit-menu').once('menu-parent');
15 // Update the list of available parent menu items to match the initial
17 Drupal.menuUiUpdateParentList();
19 // Update list of available parent menu items.
20 $menu.on('change', 'input', Drupal.menuUiUpdateParentList);
26 * Function to set the options of the menu parent item dropdown.
28 Drupal.menuUiUpdateParentList = function () {
29 const $menu = $('#edit-menu');
32 $menu.find('input:checked').each(function () {
33 // Get the names of all checked menus.
34 values.push(Drupal.checkPlain($.trim($(this).val())));
38 url: `${location.protocol}//${location.host}${Drupal.url('admin/structure/menu/parents')}`,
40 data: { 'menus[]': values },
43 const $select = $('#edit-menu-parent');
44 // Save key of last selected element.
45 const selected = $select.val();
46 // Remove all existing options from dropdown.
47 $select.children().remove();
48 // Add new options to dropdown. Keep a count of options for testing later.
50 Object.keys(options || {}).forEach((machineName) => {
52 $(`<option ${machineName === selected ? ' selected="selected"' : ''}></option>`).val(machineName).text(options[machineName]),
57 // Hide the parent options if there are no options for it.
58 $select.closest('div').toggle(totalOptions > 0).attr('hidden', totalOptions === 0);