commit 4195ae33560c41dd78cdd6002640af06aa019a5f Author: ijortengab Date: Wed Jul 2 11:21:00 2014 +0700 menambah fungsi untuk ul diff --git a/menu_attributes.module b/menu_attributes.module index c3f1f9a..c069c9f 100644 --- a/menu_attributes.module +++ b/menu_attributes.module @@ -8,6 +8,7 @@ define('MENU_ATTRIBUTES_LINK', 'attributes'); define('MENU_ATTRIBUTES_ITEM', 'item_attributes'); +define('MENU_ATTRIBUTES', 'menu_attributes'); /** * Implements hook_permission(). @@ -56,7 +57,7 @@ function menu_attributes_menu_attribute_info() { $info['id'] = array( 'label' => t('ID'), 'description' => t('Specifies a unique ID for the link.'), - 'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM), + 'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM, MENU_ATTRIBUTES), ); $info['name'] = array( 'label' => t('Name'), @@ -70,12 +71,12 @@ function menu_attributes_menu_attribute_info() { $info['class'] = array( 'label' => t('Classes'), 'description' => t('Enter additional classes to be added to the link.'), - 'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM), + 'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM, MENU_ATTRIBUTES), ); $info['style'] = array( 'label' => t('Style'), 'description' => t('Enter additional styles to be applied to the link.'), - 'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM), + 'scope' => array(MENU_ATTRIBUTES_LINK, MENU_ATTRIBUTES_ITEM, MENU_ATTRIBUTES), ); $info['target'] = array( 'label' => t('Target'), @@ -328,6 +329,10 @@ function menu_attributes_form_menu_configure_alter(&$form, $form_state) { * @see theme_menu_link() */ function menu_attributes_preprocess_menu_link(&$variables) { + + $menu_name = $variables['element']['#original_link']['menu_name']; + $_SESSION['menu_attributes']['menu_name'] = $menu_name; // save to session + $options = &$variables['element']['#localized_options']; $attributes = &$variables['element']['#attributes']; @@ -350,3 +355,85 @@ function menu_attributes_preprocess_menu_link(&$variables) { unset($options['item_attributes']); } } + +/** + * Implements hook_form_FORM_ID_alter(). + * + */ +function menu_attributes_form_menu_overview_form_alter(&$form, $form_state) { + $menu_name = $form_state['build_info']['args']['0']['menu_name']; + $default_values = variable_get('menu_attributes_' . $menu_name); + $form[MENU_ATTRIBUTES] = array( + '#type' => 'fieldset', + '#title' => t('Menu Attributes'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + '#tree' => TRUE, + ); + $attributes = menu_attributes_get_menu_attribute_info(); + foreach ($attributes as $attribute => $info) { + if(in_array(MENU_ATTRIBUTES,$info['scope'])){ + $form[MENU_ATTRIBUTES][$attribute] = $info['form'] + array( + '#access' => $info['enabled'], + ); + if(!empty($default_values)){ // for editing + $form[MENU_ATTRIBUTES][$attribute]['#default_value'] = $default_values[$attribute]; + } + } + } + $form['#submit'][] = '_menu_attributes_form_submit_2'; +} + +function _menu_attributes_form_submit_2($form, &$form_state) { + $menu_name = $form_state['build_info']['args']['0']['menu_name']; + $vars = variable_get('menu_attributes_' . $menu_name); + $menu_attributes = $form_state['values']['menu_attributes']; + $delete = true; + foreach ($menu_attributes as $attribute => $value) { + if(!empty($value)){ + $delete = false; + $has_value = true; + break; + } + } + if(!empty($vars) && $delete) + variable_del('menu_attributes_' . $menu_name); + + if(isset($has_value)) + variable_set('menu_attributes_' . $menu_name, $menu_attributes); +} +/** + * Implements hook_theme(). + * + * Override default theme_menu_tree() + * + */ +function menu_attributes_theme($existing, $type, $theme, $path){ + return array( + 'menu_tree' => array( + 'render element' => 'tree', + 'function' => 'menu_attributes_menu_tree', + ), + ); +} + +function menu_attributes_menu_tree($variables) { + $menu_name = $_SESSION['menu_attributes']['menu_name']; // get from session + unset($_SESSION['menu_attributes']);// clear session + $attributes = variable_get('menu_attributes_' . $menu_name); + if(!empty($attributes)){ + foreach ($attributes as $attribute => $value) { + if (!empty($value)) { + // Class get's special treatment, as it's an array and it should not replace existing values. + if ($attribute == 'class') { + $value = explode(' ', $value); + } + // Override the attribute. + $attributes[$attribute] = $value; + } + } + } + else $attributes = array(); + $attributes['class'][] = 'menu'; // default from theme_menu_tree + return '' . $variables['tree'] . ''; +}