You are here

menu_token.module in Menu Token 5

File

menu_token.module
View source
<?php

/**
 * Implementation of hook_form_alter().
 */
function menu_token_form_alter($form_id, &$form) {
  if ($form_id == 'menu_edit_item_form') {

    // Move 'Path' field  into a fieldset
    $path_field = $form['path'];
    $form['path'] = array(
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
    );
    $form['path']['path'] = $path_field;
    $token_enabled = variable_get('menu_token_enabled', array());
    $form['path']['token_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use tokens in @path', array(
        '@path' => t('Path'),
      )),
      '#description' => theme('token_help', 'menu_token'),
      '#default_value' => isset($token_enabled[$form['mid']['#value']]),
    );

    // Replace submit handler taking care on sort
    $form_submit = array();
    foreach ($form['#submit'] as $function => $params) {
      $function = $function == 'menu_edit_item_form_submit' ? 'menu_token_edit_item_form_submit' : $function;
      $form_submit[$function] = $params;
      unset($function);
      unset($params);
    }
    $form['#submit'] =& $form_submit;
  }
  elseif ($form_id == 'menu_item_delete_form') {
    $form['#submit']['menu_token_item_delete_form_submit'] = array();
  }
}

/**
 * Process menu and menu item add/edit form submissions.
 *
 * Overrides menu_token_edit_item_form_submit().
 */
function menu_token_edit_item_form_submit($form_id, $form_values) {
  $form_values['mid'] = menu_edit_item_save($form_values);
  $token_enabled = variable_get('menu_token_enabled', array());
  if (isset($form_values['token_enabled']) ? $form_values['token_enabled'] : FALSE) {
    $token_enabled[$form_values['mid']] = $form_values['path'];
  }
  else {
    unset($token_enabled[$form_values['mid']]);
  }
  variable_set('menu_token_enabled', $token_enabled);
  return 'admin/build/menu';
}

/**
 * Extends menu_token_item_delete_form_submit().
 */
function menu_token_item_delete_form_submit($form_id, $form_values) {
  $token_enabled = variable_get('menu_token_enabled', array());
  if (isset($token_enabled[$form_values['mid']])) {
    unset($token_enabled[$form_values['mid']]);
  }
  variable_set('menu_token_enabled', $token_enabled);
}

/**
 * Implementation of hook_footer().
 */
function menu_token_footer() {
  menu_token_replace();
}

/**
 * Implementation of hook_menu().
 */
function menu_token_menu($may_cache) {
  menu_token_replace();
  return array();
}

/**
 * Make token replacements in menu items.
 */
function menu_token_replace() {
  global $_menu;
  $token_enabled = variable_get('menu_token_enabled', array());

  // Override paths
  foreach ($token_enabled as $mid => $path) {
    $_menu['items'][$mid]['path'] = token_replace_multiple($path);

    // Force drupal to refresh menu tree with tokenized version,
    menu_get_item($mid, NULL, TRUE);
  }
}

Functions

Namesort descending Description
menu_token_edit_item_form_submit Process menu and menu item add/edit form submissions.
menu_token_footer Implementation of hook_footer().
menu_token_form_alter Implementation of hook_form_alter().
menu_token_item_delete_form_submit Extends menu_token_item_delete_form_submit().
menu_token_menu Implementation of hook_menu().
menu_token_replace Make token replacements in menu items.