Skip to content

Commit

Permalink
Issue #3231522 by enrico.sato: Limit menus to show inside select field
Browse files Browse the repository at this point in the history
  • Loading branch information
git authored and joe-mcpherson committed Sep 8, 2021
1 parent c162861 commit 7805ee2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
9 changes: 9 additions & 0 deletions config/schema/field_menu.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
field_menu.settings:
type: mapping
label: 'Field Menu settings'
mapping:
menu_type_checkbox:
type: sequence
label: 'Available menus'
sequence:
type: string
29 changes: 29 additions & 0 deletions src/Plugin/Field/FieldType/MenuItemId.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of the 'field_menu' field type.
Expand Down Expand Up @@ -72,4 +73,32 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
return $properties;
}

/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
return [
'menu_type_checkbox' => [],
] + parent::defaultFieldSettings();
}

/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {

$element = [];
$menu_options = menu_ui_get_menus();
$default_value = !empty($this->getSetting('menu_type_checkbox')) ? $this->getSetting('menu_type_checkbox') : [];
$element['menu_type_checkbox'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Available menus'),
'#options' => $menu_options,
'#default_value' => $default_value,
'#description' => $this->t('Select menu to show on select. Leave empty to show all.'),
];

return $element;
}

}
10 changes: 9 additions & 1 deletion src/Plugin/Field/FieldWidget/TreeWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
* has access to with a unique key
* (uses the same fuctionality as when a user adds a menu link to a node)
*/
$menu_item_key_field = $this->menuParentSelector->parentSelectElement($menu_parent, $menu_link);

// Limit menu list from field settings.
$menus = NULL;
if (!empty($items->getSetting('menu_type_checkbox'))) {
$menu_selected = array_diff($items->getSetting('menu_type_checkbox'), [0]);
$menus = empty($menu_selected) ? NULL : $menu_selected;
}

$menu_item_key_field = $this->menuParentSelector->parentSelectElement($menu_parent, $menu_link, $menus);
$menu_item_key_field['#default_value'] = $menu_key_value;
$menu_item_key_field['#description'] = $this->t('Select a menu root item from the available menu links');
$menu_item_key_field += [
Expand Down

0 comments on commit 7805ee2

Please sign in to comment.