Skip to content

Instantly share code, notes, and snippets.

@mootari
Created August 18, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mootari/2febfa8854580697e904 to your computer and use it in GitHub Desktop.
Save mootari/2febfa8854580697e904 to your computer and use it in GitHub Desktop.
Reformat menu options. #drupal #menu
<?php
function MODULE_form_node_form_after_build($form) {
$menu_form = &$form['menu'];
$menu_form = MODULE_node_form_reformat_menus($menu_form);
}
/**
* Reformats parent menu selects.
*/
function MODULE_node_form_reformat_menus($element) {
if(isset($element['link']['parent'])) {
MODULE_menu_parent_reformat_option_labels($element['link']['parent']);
}
if(isset($element['nodesymlinks']['items'])) {
foreach(element_children($element['nodesymlinks']['items']) as $key) {
MODULE_menu_parent_reformat_option_labels($element['nodesymlinks']['items'][$key]['parents']);
}
}
return $element;
}
/**
* Converts <menu> options in unselectable option groups and reformats indents.
*/
function MODULE_menu_parent_reformat_option_labels(&$element) {
$current_group = null;
$options = array();
foreach($element['#options'] as $key => $label) {
if($label[0] === '<') {
$group_label = str_replace(array('<', '>'), '', $label);
$options[$group_label] = array();
$current_group = &$options[$group_label];
continue;
}
$label = str_replace('-- ', '', $label);
$label = str_replace('--', '- ', $label);
if(!is_null($current_group)) {
$current_group[$key] = $label;
continue;
}
$options[$key] = $label;
}
$element['#options'] = $options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment