Commit e42df691 authored by develCuy's avatar develCuy
Browse files

Issue #1087212 by develCuy: Changed data storage from a system variable to a database table.

parent 0702a6ab
......@@ -4,9 +4,60 @@
* Install file for menu_token module.
*/
/**
* Implementation of hook_schema().
*/
function menu_token_schema() {
$schema['menu_token'] = array(
'description' => t('Menu token properties'),
'fields' => array (
'mlid' => array (
'description' => t('The menu link {menu_links}.mlid'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'link_path' => array (
'description' => t('The actual path with tokens'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('mlid'),
'indexes' => array(
'mlid' => array('mlid'),
),
);
return $schema;
}
/**
* Implementation of hook_install()
*/
function menu_token_install() {
drupal_install_schema('menu_token');
}
/**
* Implementation of hook_uninstall().
*/
function menu_token_uninstall() {
variable_del('menu_token_enabled');
drupal_uninstall_schema('menu_token');
}
/**
* Implementation of hook_update_N().
*/
function menu_token_update_6001() {
$ret = array();
drupal_install_schema('menu_token');
foreach (variable_get('menu_token_enabled', array()) as $mlid => $link_path) {
$item = (object)array('mlid' => $mlid, 'link_path' => $link_path);
drupal_write_record('menu_token', $item);
unset($item);
}
return $ret;
}
\ No newline at end of file
......@@ -15,16 +15,16 @@ function menu_token_form_menu_edit_item_alter(&$form, &$form_state) {
$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());
$link_path = menu_token_get($form['menu']['mlid']['#value']);
// Replace fake path (<front>) with user inputed one.
if (isset($menu_token_enabled[$form['menu']['mlid']['#value']])) {
$form['menu']['token']['link_path']['#default_value'] = $menu_token_enabled[$form['menu']['mlid']['#value']];
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($menu_token_enabled[$form['menu']['mlid']['#value']]),
'#default_value' => isset($link_path),
);
// Token substitutions help.
......@@ -77,32 +77,49 @@ function menu_token_form_menu_edit_item_validate($form, &$form_state) {
}
}
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) {
$menu_token_enabled = variable_get('menu_token_enabled', array());
$mlid = $form_state['values']['menu']['mlid'];
$link_path = menu_token_get($mlid);
if (!$form_state['values']['menu']['token']['token_enabled']) {
unset($menu_token_enabled[$mlid]);
db_query('DELETE FROM {menu_token} WHERE mlid = %d', $mlid);
}
else {
$menu_token_enabled[$mlid] = $form_state['values']['menu']['token']['link_path'];
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);
}
variable_set('menu_token_enabled', $menu_token_enabled);
}
/**
* Implementation of hook_translated_menu_link().
*/
function menu_token_translated_menu_link_alter(&$item, $map) {
$menu_token_enabled = variable_get('menu_token_enabled', array());
$mlid = $item['mlid'];
$link_path = menu_token_get($mlid);
// Check whether we should replace the path.
if (isset($menu_token_enabled[$mlid])) {
if (isset($link_path)) {
// If item is generated by admin menu module, tokens should not be replaced and
// indicator that tokens are used should be shown.
......@@ -114,7 +131,7 @@ function menu_token_translated_menu_link_alter(&$item, $map) {
// Replace with tokens.
$item['title'] = token_replace($item['title'], 'global');
$item['link_path'] = token_replace($menu_token_enabled[$mlid], 'global');
$item['link_path'] = token_replace($link_path, 'global');
$item['href'] = $item['link_path'];
// Override active trail if showing front page but translated link is not to front page.
......
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