Commit 83fb2dfd authored by vadim's avatar vadim
Browse files

#1087212 by dealancer: refactored code, solved issue with caching, added more...

#1087212 by dealancer: refactored code, solved issue with caching, added more comments, solved issue with spaces in menu path
parent d9f90a0a
<?php
/*
* @file
*
* Altering menu item edit form.
*
*/
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
// Create new fieldset
$form['menu']['token'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#weight' => -1,
);
// Move form elements to the new fieldset.
$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_item = menu_token_get($form['menu']['mlid']['#value']);
// Replace fake path (<front>) with user inputed one.
if ($menu_token_item) {
$form['menu']['token']['link_path']['#default_value'] = $menu_token_item['link_path'];
}
$form['menu']['token']['token_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Use tokens in title and in path.'),
'#default_value' => isset($menu_token_item),
);
// Token substitutions help.
$form['menu']['token']['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['menu']['token']['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';
}
/**
* Custom validation for form menu_edit_item.
*/
function menu_token_form_menu_edit_item_validate($form, &$form_state) {
// If token replacing 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;
}
else {
$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'];
}
}
/**
* Custom submit function for form menu_edit_form.
*/
function menu_token_form_menu_edit_item_submit($form, &$form_state) {
$mlid = $form_state['values']['menu']['mlid'];
if (!$form_state['values']['menu']['token']['token_enabled']) {
menu_token_unset($mlid);
}
else {
menu_token_set($mlid, array(
'link_path' => trim($form_state['values']['menu']['token']['link_path'])
));
}
}
\ No newline at end of file
......@@ -70,16 +70,16 @@ function menu_token_update_6000() {
'primary key' => array('mlid'),
);
// create schema
// Create schema.
db_create_table($ret, 'menu_token', $schema['menu_token']);
// ibsert records
// Insert records.
foreach (variable_get('menu_token_enabled', array()) as $mlid => $link_path) {
$link_path = db_escape_string($link_path);
$ret[] = update_sql("INSERT INTO {menu_token} (mlid, link_path) VALUES ($mlid, '$link_path')");
}
// delete variable in case of all queries were successful
// Delete variable in case of all queries were successful.
$success = true;
foreach ($ret as $r) {
$success = $success && $r['success'];
......
<?php
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
// Create new fieldset
$form['menu']['token'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#weight' => -1,
);
$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.
$link_path = menu_token_get($form['menu']['mlid']['#value']);
// Replace fake path (<front>) with user inputed one.
if ($link_path) {
$form['menu']['token']['link_path']['#default_value'] = $link_path;
}
$form['menu']['token']['token_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Use tokens in title and in path.'),
'#default_value' => isset($link_path),
);
// Token substitutions help.
$form['menu']['token']['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['menu']['token']['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';
}
/**
* Custom validation for form menu_edit_item.
*/
function menu_token_form_menu_edit_item_validate($form, &$form_state) {
// If token replacing 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;
}
else {
$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'];
}
}
function menu_token_get($mlid) {
static $items;
if (!$mlid) {
return FALSE;
}
if (!isset($items[$mlid])) {
$items[$mlid] = db_result(db_query('SELECT link_path FROM {menu_token} WHERE mlid = %d', $mlid));
}
return $items[$mlid];
}
/**
* Custom submit function for form menu_edit_form.
*/
function menu_token_form_menu_edit_item_submit($form, &$form_state) {
$mlid = $form_state['values']['menu']['mlid'];
$link_path = menu_token_get($mlid);
if (!$form_state['values']['menu']['token']['token_enabled']) {
db_query('DELETE FROM {menu_token} WHERE mlid = %d', $mlid);
}
else {
if (isset($link_path)) {
db_query('DELETE FROM {menu_token} WHERE mlid = %d', $mlid);
}
$link_path = $form_state['values']['menu']['token']['link_path'];
// Create
$item = (object)array('mlid' => $mlid, 'link_path' => $link_path);
drupal_write_record('menu_token', $item);
}
}
module_load_include('inc', 'menu_token');
module_load_include('inc', 'menu_token', 'menu_token.admin');
/**
* Implementation of hook_translated_menu_link().
*/
function menu_token_translated_menu_link_alter(&$item, $map) {
$mlid = $item['mlid'];
$link_path = menu_token_get($mlid);
$menu_token_item = menu_token_get($mlid);
// Check whether we should replace the path.
if (isset($link_path)) {
if (isset($menu_token_item)) {
// 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)) {
......@@ -114,10 +19,9 @@ function menu_token_translated_menu_link_alter(&$item, $map) {
$item['localized_options']['html'] = TRUE;
}
else {
// Replace with tokens.
$item['title'] = token_replace($item['title'], 'global');
$item['link_path'] = token_replace($link_path, 'global');
$item['link_path'] = token_replace($menu_token_item['link_path'], 'global');
$item['href'] = $item['link_path'];
// Override active trail if showing front page but translated link is not to front page.
......@@ -145,6 +49,7 @@ function menu_token_translated_menu_link_alter(&$item, $map) {
}
}
/**
* Implementation of hook_theme().
*/
......@@ -154,6 +59,7 @@ function menu_token_theme() {
);
}
/**
* Theming "uses tokens" label.
*/
......@@ -161,14 +67,4 @@ 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;
}
}
\ 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