Problem creating path alias in code I am creating a node and a path alias roughly as follows:

&lt;?php
$node = (object) array( 'type' =&gt; 'article');
node_object_prepare($node);
$node_wrapper = entity_metadata_wrapper('node', $node);
// ... some more fields get set here...
$node_wrapper-&gt;title-&gt;set( 'Foo' );
$node_wrapper-&gt;save();

// Set the path
$path = array(
  'source' =&gt; "node/$node-&gt;nid",
  'alias' =&gt; "foo/bar",
);
path_save($path);


The node is created OK, and the path seems to be created OK, too. I can type foo/bar in as the path and I see the new blank page. However when I edit that page, the Path Alias is blank?!

I have pathauto installed - could this be something to do with it? Nb. when I edit the page the "Generate path automatically" pathauto checkbox is not checked (which is what I'd expect).
Inspecting the differences in the node table, I noticed that the node created by my code had an empty language field. Updating this to "und" fixed the node alias.

This makes sense, because path_save() defaults to LANGUAGE_NONE (i.e. "und").

I had assumed that node_prepare would do likewise, but it does not.

So the solution is to set $node-&gt;language = LANGUAGE_NONE; at the point of creation.

Also, this is an easier way to create the alias...

$node-&gt;path = array('alias' =&gt; $path, 'pathauto' =&gt; FALSE);