Commit 36ce9c9e authored by ygerasimov's avatar ygerasimov
Browse files

#363742 by ygerasimov: Initial commit for 6.x version.

parent 32f75692
; $Id$
name = Menu Token
description = "Provides tokens in menu items(links)."
dependencies = token
dependencies[] = token
core = 6.x
<?php
// $Id$
/**
* Implementation of hook_install().
* @file
* Install file for menu_token module.
*/
function menu_token_install() {
// Set the module weight to -2, so that the menu_token.module's hook_menu()
// implementation get's called before core node's implementation ...
db_query("UPDATE {system} SET weight = -10 WHERE name = 'menu_token'");
}
/**
* Implementation of hook_uninstall().
......
......@@ -2,96 +2,108 @@
// $Id$
/**
* Implementation of hook_form_alter().
* Implementation of hook_form_FORM_ID_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']]),
);
function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
// Set the weight of Title field to put Path fieldset underneeth.
$form['menu']['link_title']['#weight'] = -2;
// 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();
$form['menu']['path'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#weight' => -1,
);
$form['menu']['path']['link_path'] = $form['menu']['link_path'];
unset($form['menu']['link_path']);
// Load saved token pathes.
$menu_token_enabled = variable_get('menu_token_enabled', array());
// Replace fake path (<front) with user inputed one.
if (isset($menu_token_enabled[$form['menu']['mlid']['#value']])) {
$form['menu']['path']['link_path']['#default_value'] = $menu_token_enabled[$form['menu']['mlid']['#value']];
}
}
/**
* 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';
}
$form['menu']['path']['token_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Use tokens in path.'),
'#default_value' => isset($menu_token_enabled[$form['menu']['mlid']['#value']]),
);
/**
* 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);
// Token substitutions help.
$form['menu']['path']['token_help'] = array(
'#title' => t('Tokens list can be used.'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['menu']['path']['token_help']['help'] = array(
'#value' => theme('token_help', 'global'),
);
// Add custom validation and submit functions.
array_unshift($form['#validate'], 'menu_token_form_menu_edit_item_validate');
$form['#submit'][] = 'menu_token_form_menu_edit_item_submit';
}
/**
* Implementation of hook_footer().
* Custom validation for form menu_edit_item.
*/
function menu_token_footer() {
menu_token_replace();
function menu_token_form_menu_edit_item_validate($form, &$form_state) {
// If token replacing enabled.
if ($form_state['values']['menu']['path']['token_enabled']) {
// Substitute link_path with fake well known '<front>'.
$form_state['values']['menu']['link_path'] = '<front>';
// Set 'alter' option to use hook_translated_menu_link().
$form_state['values']['menu']['options']['alter'] = TRUE;
$path = $form_state['values']['menu']['path']['link_path'];
// Check access rights for current author to creat this menu item.
if ($menu_item = menu_get_item(token_replace($path))) {
if ($menu_item['access']) {
return;
}
}
form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $path)));
}
else {
$form_state['values']['menu']['link_path'] = $form_state['values']['menu']['path']['link_path'];
}
}
/**
* Implementation of hook_menu().
* Custom submit function for form menu_edit_form.
*/
function menu_token_menu($may_cache) {
menu_token_replace();
return array();
function menu_token_form_menu_edit_item_submit($form, &$form_state) {
if (!$form_state['values']['menu']['path']['token_enabled']) {
return;
}
$menu_token_enabled = variable_get('menu_token_enabled', array());
$mlid = $form_state['values']['menu']['mlid'];
$menu_token_enabled[$mlid] = $form_state['values']['menu']['path']['link_path'];
variable_set('menu_token_enabled', $menu_token_enabled);
}
/**
* Make token replacements in menu items.
* Implementation of hook_translated_menu_link().
*/
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);
function menu_token_translated_menu_link_alter(&$item, $map) {
$menu_token_enabled = variable_get('menu_token_enabled', array());
$mlid = $item['mlid'];
// Check whether we should replace the path.
if (isset($menu_token_enabled[$mlid])) {
// Replace with tokens.
$item['link_path'] = token_replace($menu_token_enabled[$mlid], 'global');
$item['href'] = $item['link_path'];
// Load menu_item and check access.
if ($menu_item = menu_get_item($item['link_path'])) {
$item['access'] = $menu_item['access'];
return;
}
// Force drupal to refresh menu tree with tokenized version,
menu_get_item($mid, NULL, TRUE);
$item['access'] = FALSE;
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment