3

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

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

// Set the path
$path = array(
  'source' => "node/$node->nid",
  'alias' => "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).

1

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->language = LANGUAGE_NONE; at the point of creation.

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

$node->path = array('alias' => $path, 'pathauto' => FALSE);
  • Using $node = entity_create('node', array('type' => 'article')); should take care of the language thing. Off the top of my head I can't remember if it also takes care of the node_object_prepare(). – mpdonadio Jun 20 '14 at 17:22
  • Ours is a multilingual site so certainly have to select English for it and still seeing this issue – pal4life Sep 5 '14 at 21:23

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.