You are here

auto_menutitle.module in Auto Menu Title 6.2

Same filename and directory in other branches
  1. 8 auto_menutitle.module
  2. 7 auto_menutitle.module

Allows the menu title field to be automatically populated with the contents written into the node Title field

File

auto_menutitle.module
View source
<?php

/**
 * @file
 * Allows the menu title field to be automatically populated with the contents written into the node Title field
 */
define('AUTO_MENUTITLE_DISABLED', 0);
define('AUTO_MENUTITLE_ENABLED_ON', 1);
define('AUTO_MENUTITLE_ENABLED_OFF', 2);
define('AUTO_MENUTITLE_ENABLED_PATTERN_ON', 3);

/**
 * Implementation of hook_perm()
 */
function auto_menutitle_perm() {
  return array();
}

/**
 * Implementation of hook_form_alter().
 */
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 we're on a node edit form and the automenu state is off or the setting has been set off for this menu item, then lets disable everything
      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';
    }
  }
}

/**
 * hook nodeapi function to store/retrieve/set auto_menutitle states
 */
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']);

        //var_dump($node->menu);exit;
      }
      break;
    case 'presave':
      if (variable_get('amt_' . $node->type, '') == AUTO_MENUTITLE_ENABLED_PATTERN_ON) {

        // lets do our menutitle token calculation here.
        $pattern = variable_get('amt_pattern_' . $node->type, '');
        if (trim($pattern)) {
          $node->changed = time();
          $node->menu['link_title'] = _auto_nodetitle_patternprocessor($pattern, $node);
        }
      }
      break;
  }
  return;
}

/**
 * Helper function to generate the menu title according to the token string.
 * Right now its only a wrapper, but if this is to be expanded, here is the place to be.
 * @return a title string
 */
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;
}

/**
 * Implementation of node_settings_form().
 */
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.'),
  );
}

/**
 * Returns the auto_menutitle setting depending on the content type paramatar.
 */
function auto_menutitle_get_default_setting($type) {
  return variable_get('amt_' . $type, AUTO_MENUTITLE_DISABLED);
}

/**
 * Returns the auto_menutitle setting for the node, if its been custom set for this particular node.
 */
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;
}

Functions

Namesort descending Description
auto_menutitle_after_build
auto_menutitle_form_alter Implementation of hook_form_alter().
auto_menutitle_get_default_setting Returns the auto_menutitle setting depending on the content type paramatar.
auto_menutitle_get_setting Returns the auto_menutitle setting for the node, if its been custom set for this particular node.
auto_menutitle_nodeapi hook nodeapi function to store/retrieve/set auto_menutitle states
auto_menutitle_node_settings_form Implementation of node_settings_form().
auto_menutitle_perm Implementation of hook_perm()
_auto_menutitle_patternprocessor Helper function to generate the menu title according to the token string. Right now its only a wrapper, but if this is to be expanded, here is the place to be.

Constants

Namesort descending Description
AUTO_MENUTITLE_DISABLED @file Allows the menu title field to be automatically populated with the contents written into the node Title field
AUTO_MENUTITLE_ENABLED_OFF
AUTO_MENUTITLE_ENABLED_ON
AUTO_MENUTITLE_ENABLED_PATTERN_ON