3 * Menu UI admin behaviors.
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: `${window.location.protocol}//${window.location.host}${Drupal.url(
39 'admin/structure/menu/parents',
42 data: { 'menus[]': values },
45 const $select = $('#edit-menu-parent');
46 // Save key of last selected element.
47 const selected = $select.val();
48 // Remove all existing options from dropdown.
49 $select.children().remove();
50 // Add new options to dropdown. Keep a count of options for testing later.
52 Object.keys(options || {}).forEach(machineName => {
56 machineName === selected ? ' selected="selected"' : ''
60 .text(options[machineName]),
65 // Hide the parent options if there are no options for it.
68 .toggle(totalOptions > 0)
69 .attr('hidden', totalOptions === 0);