Commit 3ae78b69 authored by Vadim Mirgorod's avatar Vadim Mirgorod
Browse files

feature request #974742 by dealancer,ygerasimov: ability to use tokens in menu...

feature request #974742 by dealancer,ygerasimov: ability to use tokens in menu titles, prevent replacing tokens in menu admin area, additional theming
parent 18da1ee8
......@@ -5,38 +5,38 @@
* Implementation of hook_form_FORM_ID_alter().
*/
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;
$form['menu']['path'] = array(
// Create new fieldset
$form['menu']['token'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#weight' => -1,
);
$form['menu']['path']['link_path'] = $form['menu']['link_path'];
$form['menu']['token']['link_title'] = $form['menu']['link_title'];
unset($form['menu']['link_title']);
$form['menu']['token']['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.
// 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']];
$form['menu']['token']['link_path']['#default_value'] = $menu_token_enabled[$form['menu']['mlid']['#value']];
}
$form['menu']['path']['token_enabled'] = array(
$form['menu']['token']['token_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Use tokens in path.'),
'#title' => t('Use tokens in title and in path.'),
'#default_value' => isset($menu_token_enabled[$form['menu']['mlid']['#value']]),
);
// Token substitutions help.
$form['menu']['path']['token_help'] = array(
$form['menu']['token']['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['menu']['path']['token_help']['help'] = array(
$form['menu']['token']['token_help']['help'] = array(
'#value' => theme('token_help', 'global'),
);
......@@ -51,13 +51,14 @@ function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
*/
function menu_token_form_menu_edit_item_validate($form, &$form_state) {
// If token replacing enabled.
if ($form_state['values']['menu']['path']['token_enabled']) {
if ($form_state['values']['menu']['token']['token_enabled']) {
// Substitute link_path with fake well known '<front>'.
$form_state['values']['menu']['link_title'] = $form_state['values']['menu']['token']['link_title'];
$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'];
$path = $form_state['values']['menu']['token']['link_path'];
// Check whether path is external.
$substituted_path = token_replace($path);
if (menu_path_is_external($substituted_path)) {
......@@ -72,7 +73,8 @@ function menu_token_form_menu_edit_item_validate($form, &$form_state) {
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'];
$form_state['values']['menu']['link_title'] = $form_state['values']['menu']['token']['link_title'];
$form_state['values']['menu']['link_path'] = $form_state['values']['menu']['token']['link_path'];
}
}
......@@ -83,11 +85,11 @@ function menu_token_form_menu_edit_item_submit($form, &$form_state) {
$menu_token_enabled = variable_get('menu_token_enabled', array());
$mlid = $form_state['values']['menu']['mlid'];
if (!$form_state['values']['menu']['path']['token_enabled']) {
if (!$form_state['values']['menu']['token']['token_enabled']) {
unset($menu_token_enabled[$mlid]);
}
else {
$menu_token_enabled[$mlid] = $form_state['values']['menu']['path']['link_path'];
$menu_token_enabled[$mlid] = $form_state['values']['menu']['token']['link_path'];
}
variable_set('menu_token_enabled', $menu_token_enabled);
......@@ -102,31 +104,69 @@ function menu_token_translated_menu_link_alter(&$item, $map) {
// 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'];
// Override active trail if showing front page but translated link is not to front page.
// NOTE: This relies on any parent of a tokenised menu item having "option" flag "alter" set,
// which is most easily achieved by setting it to use token translation but not specifying a token.
// Otherwise parent does not get processed through this function and because its untranslated child has
// an href of <front>, the menu system thinks it is part of the active trail to the front page.
if (drupal_is_front_page() && $item['href'] != drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
$item['in_active_trail'] = FALSE;
}
// Check whether path is external.
if (menu_path_is_external($item['link_path'])) {
return;
// If item is generated by admin menu module, tokens should not be replaced and
// indicator that tokens are used should be shown.
if (_menu_token_menu_admin($item)) {
$item['title'] .= theme('menu_token_uses_tokens');
$item['localized_options']['html'] = TRUE;
}
else {
// Replace with tokens.
$item['title'] = token_replace($item['title'], 'global');
$item['link_path'] = token_replace($menu_token_enabled[$mlid], 'global');
$item['href'] = $item['link_path'];
// Override active trail if showing front page but translated link is not to front page.
// NOTE: This relies on any parent of a tokenised menu item having "option" flag "alter" set,
// which is most easily achieved by setting it to use token translation but not specifying a token.
// Otherwise parent does not get processed through this function and because its untranslated child has
// an href of <front>, the menu system thinks it is part of the active trail to the front page.
if (drupal_is_front_page() && $item['href'] != drupal_get_normal_path(variable_get('site_frontpage', 'node'))) {
$item['in_active_trail'] = FALSE;
}
// Load menu_item and check access.
if ($menu_item = menu_get_item($item['link_path'])) {
$item['access'] = $menu_item['access'];
return;
}
// Check whether path is external.
if (menu_path_is_external($item['link_path'])) {
return;
}
$item['access'] = FALSE;
// Load menu_item and check access.
if ($menu_item = menu_get_item($item['link_path'])) {
$item['access'] = $menu_item['access'];
return;
}
$item['access'] = FALSE;
}
}
}
}
\ No newline at end of file
/**
* Implementation of hook_theme().
*/
function menu_token_theme() {
return array(
'menu_token_uses_tokens' => array(),
);
}
/**
* Theming "uses tokens" label.
*/
function theme_menu_token_uses_tokens() {
drupal_add_css(drupal_get_path('module', 'menu_token') . '/menu_token.css');
return '<span class="menu_token_uses_tokens">' . t(' uses tokens') . '</span>';
}
/**
* Returns true if menu item is being generated by menu administration modules.
*
* @see menu_overview_form().
*/
function _menu_token_menu_admin($item) {
global $menu_admin;
return $menu_admin;
}
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