path_nodeapi

Versions
4.6 – 6
path_nodeapi(&$node, $op, $arg)

Implementation of hook_nodeapi().

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

Code

modules/path.module, line 196

<?php
function path_nodeapi(&$node, $op, $arg) {
  if (user_access('create url aliases') || user_access('administer url aliases')) {
    switch ($op) {
      case 'validate':
        $node->path = trim($node->path);
        if ($node->path && !valid_url($node->path)) {
          form_set_error('path', t('The path is invalid.'));
        }
        else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) {
          form_set_error('path', t('The path is already in use.'));
        }
        break;

      case 'form pre':
        $output = form_textfield(t('Path alias'), 'path', $node->path, 60, 250, t('Optionally specify an alternative URL by which this node can be accessed.  For example, type "about" when writing an about page.  Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'));
        if ($node->path) {
          $output .= form_hidden('pid', db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $node->path)));
        }
        return $output;

      case 'load':
        $path = "node/$node->nid";
        $alias = drupal_get_path_alias($path);
        if ($alias != $path) {
          $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 ($node->path) {
          path_set_alias("node/$node->nid", $node->path);
        }
        break;

      case 'update':
        path_set_alias("node/$node->nid", $node->path, $node->pid);
        break;

      case 'delete':
        $path = "node/$node->nid";
        if (drupal_get_path_alias($path) != $path) {
          path_set_alias($path);
        }
        break;
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.