You are here

function path_nodeapi in Drupal 6

Same name and namespace in other branches
  1. 4 modules/path.module \path_nodeapi()
  2. 5 modules/path/path.module \path_nodeapi()

Implementation of hook_nodeapi().

Allows URL aliases for nodes to be specified at node edit time rather than through the administrative interface.

File

modules/path/path.module, line 175
Enables users to rename URLs.

Code

function path_nodeapi(&$node, $op, $arg = NULL) {

  // Permissions are required for everything except node loading.
  if (user_access('create url aliases') || user_access('administer url aliases') || $op == 'load') {
    $language = isset($node->language) ? $node->language : '';
    switch ($op) {
      case 'validate':
        if (isset($node->path)) {
          $node->path = trim($node->path);
          if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s' AND language = '%s'", $node->path, "node/{$node->nid}", $language))) {
            form_set_error('path', t('The path is already in use.'));
          }
        }
        break;
      case 'load':
        $path = 'node/' . $node->nid;
        $alias = drupal_get_path_alias($path, $language);
        if ($path != $alias) {
          $node->path = $alias;
        }
        break;
      case 'insert':

        // Don't try to insert if path is NULL. We may have already set
        // the alias ahead of time.
        if (isset($node->path)) {
          path_set_alias('node/' . $node->nid, $node->path, NULL, $language);
        }
        break;
      case 'update':

        // $node->pid is usually only set when updating from a node edit form
        // (see path_form_alter). If it is not set (e.g. on most node_save()
        // commands), we cannot be sure whether a change in $node->path is meant
        // to replace an existing alias or add one extra, so we do the latter.
        path_set_alias('node/' . $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
        break;
      case 'delete':
        $path = 'node/' . $node->nid;
        if (drupal_get_path_alias($path) != $path) {
          path_set_alias($path);
        }
        break;
    }
  }
}