Skip to content
Snippets Groups Projects
Commit c48c9cc5 authored by Yurii Panchuk's avatar Yurii Panchuk Committed by Peter Törnstrand
Browse files

Issue #3490857 by panchuk: Default parent link field is visible even if no menus are selected

parent 3dbd468b
Branches
Tags
1 merge request!4Issue #3490857: Default parent link field is visible even if no menus are selected
/**
* @file
* Menu UI Async Widget admin behaviors.
*/
(function ($, Drupal) {
/**
*
* @type {Drupal~behavior}
*/
Drupal.behaviors.menuUiAsyncWidgetChangeParentItems = {
attach(context, settings) {
const menu = once('menu-ui-async-parent', '#edit-menu-async');
if (menu.length) {
const $menu = $(menu);
// Update the list of available parent menu items to match the initial
// available menus.
Drupal.menuUiAsyncWidgetUpdateParentList();
// Update list of available parent menu items.
$menu.on('change', 'input', Drupal.menuUiAsyncWidgetUpdateParentList);
}
},
};
/**
* Function to set the options of the menu parent item dropdown.
*/
Drupal.menuUiAsyncWidgetUpdateParentList = function () {
const $menu = $('#edit-menu-async');
const values = [];
$menu.find('input:checked').each(function () {
// Get the names of all checked menus.
values.push(Drupal.checkPlain(this.value));
});
$.ajax({
url: `${window.location.protocol}//${window.location.host}${Drupal.url(
'admin/structure/menu/parents',
)}`,
type: 'POST',
data: { 'menus[]': values },
dataType: 'json',
success(options) {
const $select = $('#edit-menu-async-parent');
// Save key of last selected element.
const selected = $select[0].value;
// Remove all existing options from dropdown.
$select.children().remove();
// Add new options to dropdown. Keep a count of options for testing later.
let totalOptions = 0;
Object.keys(options || {}).forEach((machineName) => {
const selectContents = document.createElement('option');
selectContents.selected = machineName === selected;
selectContents.value = machineName;
selectContents.textContent = options[machineName];
$select.append(selectContents);
totalOptions++;
});
// Hide the parent options if there are no options for it.
$select
.closest('div')
.toggle(totalOptions > 0)
.attr('hidden', totalOptions === 0);
},
});
};
})(jQuery, Drupal);
menu_ui_async_widget.admin:
js:
js/menu_ui_async_widget.admin.js: {}
dependencies:
- core/jquery
- core/drupal
......@@ -279,7 +279,7 @@ function menu_ui_async_widget_form_node_type_form_alter(&$form, FormStateInterfa
'#type' => 'details',
'#title' => t('Menu UI Async settings'),
'#attached' => [
'library' => ['menu_ui/drupal.menu_ui.admin'],
'library' => ['menu_ui_async_widget/menu_ui_async_widget.admin'],
],
'#group' => 'additional_settings',
];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment