You are here

function pathauto_form_alter in Pathauto 6.2

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 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 364
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'];

    // Find if there is an automatic alias pattern for this node type.
    $language = isset($node->language) ? $node->language : '';
    $pattern = pathauto_pattern_load_by_entity('node', $node->type, $language);

    // 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.
          module_load_include('inc', 'pathauto');
          $pathauto_alias = pathauto_create_alias('node', 'return', "node/{$node->nid}", array(
            'node' => $node,
          ), $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('Generate automatic URL alias'),
        '#default_value' => $node->pathauto_perform_alias,
        '#description' => t('Uncheck this to create a custom alias below.'),
        '#weight' => -1,
      );

      // Add a shortcut link to configure URL alias patterns.
      if (menu_valid_path(array(
        'link_path' => 'admin/build/path/patterns',
      ))) {
        $form['path']['pathauto_perform_alias']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/build/path/patterns');
      }
      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,
        );
      }
    }
  }
}