You are here

function taxonomy_menu_form_alter in Taxonomy menu 7

Same name and namespace in other branches
  1. 6.2 taxonomy_menu.module \taxonomy_menu_form_alter()

Implements hook_form_alter().

Modify the form at admin/content/taxonomy/edit/vocabulary/xx. We add our taxonomy_menu options in here on a per-vocab basis.

File

./taxonomy_menu.module, line 33
Adds links to taxonomy terms into a menu.

Code

function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'taxonomy_form_vocabulary') {

    // Do not alter on deletion.
    if (isset($form_state['confirm_delete']) && isset($form_state['values']['vid'])) {
      return;
    }

    // Choose a menu to add link items to.
    $menus = menu_get_menus();
    array_unshift($menus, t('- Disabled -'));

    // Options for path if tokens are not enabled.
    $paths = _taxonomy_menu_get_paths();
    $form['taxonomy_menu'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#title' => t('Taxonomy menu'),
      '#weight' => 10,
      '#tree' => TRUE,
    );

    // This turns the vocab terms into menu items.
    $item['mlid'] = 0;
    $menu_items = menu_parent_options(menu_get_menus(), $item);
    array_unshift($menu_items, '= DISABLED =');

    // The vid isn't set when a new vocabulary is being created.
    if (isset($form['vid']['#value'])) {
      $default = variable_get(_taxonomy_menu_build_variable('vocab_menu', $form['vid']['#value']), NULL) . ':' . variable_get(_taxonomy_menu_build_variable('vocab_parent', $form['vid']['#value']), NULL);
      if (!isset($menu_items[$default])) {
        $default = 0;
      }
    }
    else {
      $default = 0;
    }
    $form['taxonomy_menu']['vocab_parent'] = array(
      '#type' => 'select',
      '#title' => t('Menu location'),
      '#default_value' => $default,
      '#options' => $menu_items,
      '#description' => t('Taxonomy menu items will be inserted below the item selected here.'),
      '#attributes' => array(
        'class' => array(
          'menu-title-select',
        ),
      ),
    );
    $form['taxonomy_menu']['path'] = array(
      '#type' => 'select',
      '#title' => t('Menu path type'),
      '#default_value' => isset($form['vid']['#value']) ? variable_get(_taxonomy_menu_build_variable('path', $form['vid']['#value']), 0) : 0,
      '#options' => $paths,
      '#description' => t('The path will be taxonomy/term/tid if <em>Default</em> has been selected.<br />The menu path will be passed through drupal_get_path_alias() function so all aliases will be applied.'),
    );

    // Get taxonomy menu form options.
    if (isset($form['vid']) && $form['vid']['#value']) {
      $vid = $form['vid']['#value'];
    }
    else {
      $vid = 0;
    }
    $form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($vid);

    // Rebuild the menu.
    $form['taxonomy_menu']['options']['rebuild'] = array(
      '#type' => 'checkbox',
      '#title' => t('Rebuild the menu on submit'),
      '#default_value' => 0,
      '#weight' => 20,
      '#description' => t('<strong>Warning</strong>: This will delete then re-create all of the menu items. Only use this option if you are experiencing issues like missing menu items or other inconsistencies.'),
    );

    // Move the buttons to the bottom of the form.
    $form['submit']['#weight'] = 49;
    $form['delete']['#weight'] = 50;

    // Add an extra submit handler to save these settings.
    $form['#submit'][] = 'taxonomy_menu_vocab_submit';
  }
  elseif ($form_id == "taxonomy_overview_terms") {

    // Add an extra submit handler to sync the rearranged terms with menu.
    // @ TODO: using hook_taxonomy_vocabulary_update is nicer then callback,
    // But gives less info and does not always fire.
    $form['#submit'][] = 'taxonomy_menu_overview_submit';
  }
}