You are here

function pathauto_form_alter in Pathauto 6

Same name and namespace in other branches
  1. 5.2 pathauto.module \pathauto_form_alter()
  2. 5 pathauto_node.inc \pathauto_form_alter()
  3. 6.2 pathauto.module \pathauto_form_alter()

Implements hook_form_alter().

This allows alias creators to override Pathauto and specify their own aliases (Pathauto will be invisible to other users). Inserted into the path module's fieldset in the node form.

File

./pathauto.module, line 332
Main file for the Pathauto module, which automatically generates aliases for content.

Code

function pathauto_form_alter(&$form, $form_state, $form_id) {

  // Process only node forms.
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $node = $form['#node'];
    $pattern = FALSE;

    // Find if there is an automatic alias pattern for this node type.
    if (isset($form['language'])) {
      $language = isset($form['language']['#value']) ? $form['language']['#value'] : $form['language']['#default_value'];
      $pattern = trim(variable_get('pathauto_node_' . $form['type']['#value'] . '_' . $language . '_pattern', ''));
    }
    if (!$pattern) {
      $pattern = trim(variable_get('pathauto_node_' . $form['type']['#value'] . '_pattern', ''));
      if (!$pattern) {
        $pattern = trim(variable_get('pathauto_node_pattern', ''));
      }
    }

    // If there is a pattern, show the automatic alias checkbox.
    if ($pattern) {
      if (!isset($node->pathauto_perform_alias)) {
        if (!empty($node->nid)) {

          // If this is not a new node, compare it's current alias to the
          // alias that would be genereted by pathauto. If they are the same,
          // then keep the automatic alias enabled.
          _pathauto_include();
          $placeholders = pathauto_get_placeholders('node', $node);
          $pathauto_alias = pathauto_create_alias('node', 'return', $placeholders, "node/{$node->nid}", $node->nid, $node->type, $node->language);
          $node->pathauto_perform_alias = isset($node->path) && $node->path == $pathauto_alias;
        }
        else {

          // If this is a new node, enable the automatic alias.
          $node->pathauto_perform_alias = TRUE;
        }
      }

      // Add JavaScript that will disable the path textfield when the automatic
      // alias checkbox is checked.
      drupal_add_js(drupal_get_path('module', 'pathauto') . '/pathauto.js');

      // Override path.module's vertical tabs summary.
      $form['path']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'pathauto') . '/pathauto.js';
      $form['path']['pathauto_perform_alias'] = array(
        '#type' => 'checkbox',
        '#title' => t('Automatic alias'),
        '#default_value' => $node->pathauto_perform_alias,
        '#description' => t('An alias will be generated for you. If you wish to create your own alias below, uncheck this option.'),
        '#weight' => -1,
      );
      if (user_access('administer pathauto')) {
        $form['path']['pathauto_perform_alias']['#description'] .= ' ' . t('To control the format of the generated aliases, see the <a href="@pathauto">automated alias settings</a>.', array(
          '@pathauto' => url('admin/build/path/pathauto'),
        ));
      }
      if ($node->pathauto_perform_alias && !empty($node->old_alias) && empty($node->path)) {
        $form['path']['path']['#default_value'] = $node->old_alias;
        $node->path = $node->old_alias;
      }

      // For Pathauto to remember the old alias and prevent the Path-module from deleteing it when Pathauto wants to preserve it
      if (isset($node->path)) {
        $form['path']['old_alias'] = array(
          '#type' => 'value',
          '#value' => $node->path,
        );
      }
    }
  }
}