6

I am creating some nodes in Drupal 6 programatically by creating an empty object, setting some properties and calling node_save() on the object.

When I print out the object again, I see new properties, since node_save() alters the node object by reference, but I don't see a path property, but from Drupal I can always view the node at 'node/NID'.

Is 'node/NID' the path?

Should I not set a 'user friendly' path property on the node itself, and just use path_set_alias() after node_save instead of mucking with the path property on the node itself?

4

node/[nid] is indeed the path to any node. That is how it is defined in the node module, you can see for yourself in the node_menu function.

With pathauto module, you can set up rules for automatic aliases so you don't have to figure out yourself what the alias of the node should be. This is the preferred way of creating and automatic "user friendly" or "SEO optimized" URLs, instead of hardcoding the logic in your custom module.

  • As you can imagine, what I am doing is not a simple case where I can or even want to use pathauto, otherwise I would not be creating the node programatically. The other problem with doing this via pathauto, is that it's a configuration set up, which I'd rather have in code than in the UI, though of course I could use an install profile, or something of that sort. – Aaron May 16 '11 at 18:03
5

The canonical URL for a node is http://example.com/node/<nid>; if you want to set a path alias, you need to set it with path_set_alias('node/' . $node->nid, $alias).

  • I guess it's confusing because in the UI the field is 'path', but the path is really just 'node/nid'. The path field in the UI would be better named 'alias' IMO. – Aaron May 16 '11 at 18:05
  • @Aaron In Drupal 7, that field is called "URL alias," and it is inside the "URL path settings" fieldset. Strings used in the user interface are usually not changed, once a Drupal version has its first official release which is not a beta or release candidate. I agree that calling it "path" could be confusing. – kiamlaluno May 16 '11 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.