diff --git a/menu_attributes.install b/menu_attributes.install index 14c80fe..2917a11 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 6b0cdde..bce4adf 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']; + $attributes = variable_get('menu_attributes_menus', array()); + + // Set default attributes. + $default_attributes = !empty($attributes[$menu_name]) ? $attributes[$menu_name] : array('id' => '', 'class' => 'menu'); + + $form['attributes'] = array( + '#type' => 'fieldset', + '#title' => t('Attributes'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#tree' => TRUE, + ); + $form['attributes']['id'] = array( + '#type' => 'textfield', + '#title' => t('ID'), + '#default_value' => $default_attributes['id'], + ); + $form['attributes']['class'] = array( + '#type' => 'textfield', + '#title' => t('Classes'), + '#default_value' => $default_attributes['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']; + + $attributes = variable_get('menu_attributes_menus', array()); + + // Store attributes in variable. + $attributes[$menu_name] = array( + 'id' => $values['attributes']['id'], + 'class' => $values['attributes']['class'], + ); + variable_set('menu_attributes_menus', $attributes); +} + +/** + * 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'; +} + +/** + * Prepares variables for theme_menu_tree(). + */ +function menu_attributes_prepreprocess_menu_tree(&$variables) { + $first_element_key = current(element_children($variables['tree'])); + $first_element = $variables['tree'][$first_element_key]; + $variables['menu_name'] = $first_element['#original_link']['menu_name']; + $variables['depth'] = $first_element['#original_link']['depth']; +} + +/** + * Implements theme_menu_tree(). + */ +function menu_attributes_menu_tree($variables) { + $menus = variable_get('menu_attributes_menus', array()); + $attributes = array('class' => 'menu'); + if (!empty($menus[$variables['menu_name']]) && $variables['depth'] == 1) { + $attributes = array_filter($menus[$variables['menu_name']]); + } + + return '' . $variables['tree'] . ''; +}