View source
<?php
define('AUTO_MENUTITLE_DISABLED', 0);
define('AUTO_MENUTITLE_ENABLED_ON', 1);
define('AUTO_MENUTITLE_ENABLED_OFF', 2);
define('AUTO_MENUTITLE_ENABLED_PATTERN_ON', 3);
function auto_menutitle_perm() {
return array();
}
function auto_menutitle_form_alter(&$form, $form_state, $form_id) {
if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
auto_menutitle_node_settings_form($form);
}
elseif (isset($form['#node']) && isset($form['#method']) && $form['#node']->type . '_node_form' == $form_id) {
$default_option = auto_menutitle_get_default_setting($form['#node']->type);
$option = auto_menutitle_get_setting($form['#node']);
$form['menu']['#collapsed'] = variable_get('amt_collapsed_' . $form['#node']->type, FALSE);
if (!empty($default_option) && $default_option != AUTO_MENUTITLE_DISABLED) {
$form['menu']['fixtitle'] = array(
'#type' => 'checkbox',
'#default_value' => $default_option == AUTO_MENUTITLE_ENABLED_ON || $default_option == AUTO_MENUTITLE_ENABLED_PATTERN_ON || $option,
'#weight' => -1,
'#title' => t('Automatically update Menu Title'),
'#description' => t('To allow editing of the Menu title, simply un-check this option.'),
);
$form['menu']['link_title']['#weight'] = -2;
if ($option == AUTO_MENUTITLE_ENABLED_ON || $option == AUTO_MENUTITLE_ENABLED_PATTERN_ON) {
$form['menu']['link_title']['#attributes'] = array(
'readonly' => 'readonly',
);
}
if (!empty($form['nid']['#value']) && !$form['#node']->menu['automenu_state'] && !$option) {
$form['menu']['fixtitle']['#default_value'] = FALSE;
$form['menu']['link_title']['#attributes'] = array();
}
$form['#after_build'][] = 'auto_menutitle_after_build';
}
}
}
function auto_menutitle_nodeapi(&$node, $op, $teaser) {
switch ($op) {
case 'insert':
case 'update':
if (isset($node->menu['mlid'])) {
$res = db_query('UPDATE {menu_links} SET automenu_state = %d WHERE mlid = %d', $node->menu['fixtitle'], $node->menu['mlid']);
}
break;
case 'presave':
if (variable_get('amt_' . $node->type, '') == AUTO_MENUTITLE_ENABLED_PATTERN_ON) {
$pattern = variable_get('amt_pattern_' . $node->type, '');
if (trim($pattern)) {
$node->changed = time();
$node->menu['link_title'] = _auto_nodetitle_patternprocessor($pattern, $node);
}
}
break;
}
return;
}
function _auto_menutitle_patternprocessor($output, $node) {
if (module_exists('token')) {
$output = token_replace($output, 'node', $node);
}
if (module_exists('token')) {
$output = preg_replace('/[\\t\\n\\r\\0\\x0B]/', '', strip_tags($output));
}
return $output;
}
function auto_menutitle_node_settings_form(&$form) {
$form['auto_menutitle'] = array(
'#type' => 'fieldset',
'#title' => t('Automatic Menu title generation'),
'#weight' => 2,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['auto_menutitle']['amt'] = array(
'#type' => 'radios',
'#default_value' => auto_menutitle_get_default_setting($form['#node_type']->type),
'#options' => array(
t('Disabled'),
t('Copy the node title into the menu title field'),
t('Copy the node title into the menu title field, but only after manually checking on'),
),
);
if (module_exists('token')) {
$form['auto_menutitle']['amt']['#options'][] = t('Copy the pattern contents below, into the menu title field');
$form['auto_menutitle']['amt_pattern'] = array(
'#type' => 'textarea',
'#title' => t('Pattern for the title'),
'#description' => t('Leave blank for using the per default generated title. Otherwise this string will be used as title.'),
'#default_value' => variable_get('amt_pattern_' . $form['#node_type']->type, ''),
);
$form['auto_menutitle']['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
);
$form['auto_menutitle']['token_help']['help'] = array(
'#value' => theme('token_help', 'node'),
);
}
$form['auto_menutitle']['amt_collapsed'] = array(
'#type' => 'checkbox',
'#title' => 'Collapse the menu fieldset',
'#default_value' => variable_get("amt_collapsed_" . $form['#node_type']->type, FALSE),
'#description' => t('When checked, the fielset will be collapsed. Note: if using the verticaltabs module, this setting will have no impact.'),
);
}
function auto_menutitle_get_default_setting($type) {
return variable_get('amt_' . $type, AUTO_MENUTITLE_DISABLED);
}
function auto_menutitle_get_setting($node) {
if (isset($node->menu['automenu_state'])) {
return $node->menu['automenu_state'];
}
}
function auto_menutitle_after_build($form, $form_state) {
drupal_add_js(array(
'autoMenutitle' => array(
'amtCurrentSetting' => variable_get('amt_' . $form['type']['#value'], ''),
'amtConstantPattern' => AUTO_MENUTITLE_ENABLED_PATTERN_ON,
),
), 'setting');
drupal_add_js(drupal_get_path('module', 'auto_menutitle') . '/auto_menutitle.js');
return $form;
}