diff --git a/menu_attributes.install b/menu_attributes.install index 14c80fe..47d85a4 100644 --- a/menu_attributes.install +++ b/menu_attributes.install @@ -28,6 +28,7 @@ function menu_attributes_uninstall() { variable_del("menu_attributes_{$attribute}_enable"); variable_del("menu_attributes_{$attribute}_default"); } + variable_del("menu_attributes_menus"); } /** diff --git a/menu_attributes.module b/menu_attributes.module index dabaf3c..1799880 100644 --- a/menu_attributes.module +++ b/menu_attributes.module @@ -132,6 +132,60 @@ function menu_attributes_get_menu_attribute_info() { /** * Implements hook_form_FORM_ID_alter(). * + * Adds menu attribute options to the edit menu form. + */ +function menu_attributes_form_menu_edit_menu_alter(&$form, $form_state) { + $menu_name = $form['menu_name']['#default_value']; + $attrs = variable_get('menu_attributes_menus', array()); + + // Set default attributes. + $default_attrs = !empty($attrs[$menu_name]) ? $attrs[$menu_name] : array('id' => '', 'class' => ''); + + $form['attributes'] = array( + '#type' => 'fieldset', + '#title' => t('Attributes'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#tree' => TRUE, + ); + $form['attributes']['id'] = array( + '#type' => 'textfield', + '#title' => 'ID', + '#default_value' => $default_attrs['id'], + ); + $form['attributes']['class'] = array( + '#type' => 'textfield', + '#title' => 'Classes', + '#default_value' => $default_attrs['class'], + ); + + $form['#submit'][] = '_menu_attributes_menu_form_submit'; +} + +/** + * Form submit handler for menu. + * + * Save menu attributes in variable. + */ +function _menu_attributes_menu_form_submit($form, &$form_state) { + $values = $form_state['values']; + + // Adds 'menu-' prefix if menu has been created. + $menu_name = !empty($values['old_name']) ? $values['menu_name'] : 'menu-' . $values['menu_name']; + + $attrs = variable_get('menu_attributes_menus', array()); + + // Store attributes in variable. + $attrs[$menu_name] = array( + 'id' => $values['attributes']['id'], + 'class' => $values['attributes']['class'] + ); + variable_set('menu_attributes_menus', $attrs); +} + +/** + * Implements hook_form_FORM_ID_alter(). + * * Adds menu attribute options to the edit menu item form. * * @see menu_edit_item() @@ -351,3 +405,34 @@ function menu_attributes_preprocess_menu_link(&$variables) { unset($options['item_attributes']); } } + +/** + * Implements hook_theme_registry_alter(). + */ +function menu_attributes_theme_registry_alter(&$registry) { + array_unshift($registry['menu_tree']['preprocess functions'], 'menu_attributes_prepreprocess_menu_tree'); + $registry['menu_tree']['function'] = 'menu_attributes_menu_tree'; +} + +/** + * Pre-preprocess function for template_preprocess_menu_tree(). + * + * Adds menu name to $vars. Gets menu name from first link item. + */ +function menu_attributes_prepreprocess_menu_tree(&$vars) { + $first_element_key = current(element_children($vars['tree'])); + $first_element = $vars['tree'][$first_element_key]; + $vars['menu_name'] = $first_element['#original_link']['menu_name']; +} + +/** + * Override theme_menu_tree(). + * + * Output menu attributes. + */ +function menu_attributes_menu_tree($variables) { + $menus = variable_get('menu_attributes_menus', array()); + $attributes = !empty($menus[$variables['menu_name']]) ? $menus[$variables['menu_name']] : array(); + + return '