Created
August 18, 2015 13:56
-
-
Save mootari/2febfa8854580697e904 to your computer and use it in GitHub Desktop.
Reformat menu options. #drupal #menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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