machine_name); // If no possible parent menu items were found, there is nothing to display. if (empty($options)) { return; } $link = $term->menu; $form['menu'] = array( '#type' => 'fieldset', '#title' => t('Menu settings'), '#access' => user_access('administer menu'), '#collapsible' => TRUE, '#collapsed' => !$link['link_title'], // if menu item is set, set collapsed to true... '#group' => 'additional_settings', '#attached' => array( 'js' => array(drupal_get_path('module', 'taxonomy_menu_form') . '/menu.js'), ), '#tree' => TRUE, '#weight' => -2, '#attributes' => array('class' => array('menu-link-form')), ); $form['menu']['enabled'] = array( '#type' => 'checkbox', '#title' => t('Provide a menu link'), '#default_value' => (int) (bool) $link['mlid'], //if the terms has any menu item attached to it. set 1 else 0 ); $form['menu']['link'] = array( '#type' => 'container', '#parents' => array('menu'), '#states' => array( 'invisible' => array( 'input[name="menu[enabled]"]' => array('checked' => FALSE), ), ), ); // Populate the element with the link data. foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) { $form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]); } $form['menu']['link']['link_title'] = array( '#type' => 'textfield', '#title' => t('Menu link title'), '#default_value' => $link['link_title'], ); $form['menu']['link']['description'] = array( '#type' => 'textarea', '#title' => t('Description'), '#default_value' => isset($link['options']['attributes']['title']) ? $link['options']['attributes']['title'] : '', '#rows' => 1, '#description' => t('Shown when hovering over the menu link.'), ); $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $vocabulary->machine_name, 'main-menu:0')); if (!isset($options[$default])) { $default = 'navigation:0'; } $form['menu']['link']['parent'] = array( '#type' => 'select', '#title' => t('Parent item'), '#default_value' => $default, '#options' => $options, '#attributes' => array('class' => array('menu-parent-select')), ); $form['menu']['link']['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), '#delta' => 50, '#default_value' => $link['weight'], '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'), ); // Make sure that our own submit handler is run first before taxonomys own. array_unshift($form['#submit'], 'taxonomy_menu_form_taxonomy_form_term_submit'); } /** * . * */ function taxonomy_menu_form_taxonomy_form_term_submit(&$form, &$form_state) { // Decompose the selected menu parent option into 'menu_name' and 'plid', if // the form used the default parent selection widget. if (!empty($form_state['values']['menu']['parent'])) { list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']); } } /** * Implements hook_form_FORM_ID_alter(). * * Adds menu options to the taxonomy vocabulary form. */ function taxonomy_menu_form_form_taxonomy_form_vocabulary_alter(&$form, $form_state) { $menu_options = menu_get_menus(); $vocabulary = $form['#vocabulary']; $form['menu'] = array( '#type' => 'fieldset', '#title' => t('Menu settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#attached' => array( 'js' => array(drupal_get_path('module', 'menu') . '/menu.admin.js'), ), '#group' => 'additional_settings', ); $form['menu']['menu_options'] = array( '#type' => 'checkboxes', '#title' => t('Available menus'), '#default_value' => variable_get('menu_options_' . $vocabulary->machine_name, array('main-menu')), '#options' => $menu_options, '#description' => t('The menus available to place links in for this content type.'), ); // To avoid an 'illegal option' error after saving the form we have to load // all available menu items. // Otherwise it is not possible to dynamically add options to the list. // @todo Convert menu_parent_options() into a #process callback. $options = menu_parent_options(menu_get_menus(), array('mlid' => 0)); $form['menu']['menu_parent'] = array( '#type' => 'select', '#title' => t('Default parent item'), '#default_value' => variable_get('menu_parent_' . $vocabulary->machine_name, 'main-menu:0'), '#options' => $options, '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'), '#attributes' => array('class' => array('menu-title-select')), ); $form['#submit'][] = 'taxonomy_menu_form_taxonomy_vocabulary_form_submit'; // Call Drupal.menu_update_parent_list() to filter the list of // available default parent menu items based on the selected menus. drupal_add_js( '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);', array('scope' => 'footer', 'type' => 'inline') ); } function taxonomy_menu_form_taxonomy_vocabulary_form_submit(&$form, &$form_state) { $menu_options = $form_state['values']['menu_options']; $menu_parent = $form_state['values']['menu_parent']; $machine_name = $form_state['values']['machine_name']; variable_set('menu_options_' . $machine_name, $menu_options); variable_set('menu_parent_' . $machine_name, $menu_parent); menu_rebuild(); } /** * Implements hook_taxonomy_term_insert(). */ function taxonomy_menu_form_taxonomy_term_insert($term) { taxonomy_menu_form_taxonomy_term_save($term); } /** * Implements hook_taxonomy_term_update(). */ function taxonomy_menu_form_taxonomy_term_update($term) { taxonomy_menu_form_taxonomy_term_save($term); } /** * Helper for hook_taxonomy_term_insert() and hook_taxonomy_term_update(). */ function taxonomy_menu_form_taxonomy_term_save($term) { if (isset($term->menu)) { $link = &$term->menu; if (empty($link['enabled'])) { if (!empty($link['mlid'])) { menu_link_delete($link['mlid']); } } elseif (trim($link['link_title'])) { $link['link_title'] = trim($link['link_title']); $link['link_path'] = "taxonomy/term/$term->tid"; if (trim($link['description'])) { $link['options']['attributes']['title'] = trim($link['description']); } else { // If the description field was left empty, remove the title attribute // from the menu link. unset($link['options']['attributes']['title']); } if (!menu_link_save($link)) { drupal_set_message(t('There was an error saving the menu link.'), 'error'); } } } } /** * Implements hook_taxonomy_term_delete(). */ function taxonomy_menu_form_taxonomy_term_delete($term) { // Delete all menu module links that point to this term. $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu'", array(':path' => 'taxonomy/term/' . $term->tid), array('fetch' => PDO::FETCH_ASSOC)); foreach ($result as $m) { menu_link_delete($m['mlid']); } } /** * Implements hook_taxonomy_term_prepare(). */ function taxonomy_menu_form_taxonomy_term_prepare(&$term) { if (empty($term->menu)) { // Prepare the term for the edit form so that $term->menu always exists. $menu_name = strtok(variable_get('menu_parent_' . $term->vocabulary_machine_name, 'main-menu:0'), ':'); $item = array(); if (isset($term->tid)) { $mlid = FALSE; // Give priority to the default menu $type_menus = variable_get('menu_options_' . $term->vocabulary_machine_name, array('main-menu' => 'main-menu')); if (in_array($menu_name, $type_menus)) { $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array( ':path' => 'taxonomy/term/' . $term->tid, ':menu_name' => $menu_name, ))->fetchField(); } // Check all allowed menus if a link does not exist in the default menu. if (!$mlid && !empty($type_menus)) { $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' AND menu_name IN (:type_menus) ORDER BY mlid ASC", 0, 1, array( ':path' => 'taxonomy/term/' . $term->tid, ':type_menus' => array_values($type_menus), ))->fetchField(); } if ($mlid) { $item = menu_link_load($mlid); } } // Set default values. $term->menu = $item + array( 'link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0, 'customized' => 0, ); } // Find the depth limit for the parent select. if (!isset($term->menu['parent_depth_limit'])) { $term->menu['parent_depth_limit'] = _menu_parent_depth_limit($term->menu); } }