Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've seen the excellent documentation on Drupal.org about how to create a node in code, but that documentation doesn't address how to create the node's alias. I've seen code snippets that use $node->path; and $node->menu with node_save($node); which works, but with $entity->path->set(); or with $entity->menu->set(); I'm receiving the following error message:

Unknown data property path. Unknown data property menu.

Why does this not work with entity api and how can I modify my code (below) to make it work?

 * Testing programatic content creation.
function mymodule_update_7000() {
  $about = _create_node_and_menu_item('About', 'about', 'main-menu', 'About the site');

 * Helper function creates a node, with alias, and menu item, and
 * returns the mlid.
 * @param  string  $title      the title of the node
 * @param  string  $alias      the URL alias
 * @param  string  $menu_name  the machine name of the menu, eg 'main-menu'
 * @param  string  $menu_title the title of the menu item, if none is
 *                             provide, the $title will be used.
 * @param  integer $plid       the numeric id of the parent menu item
 *                             (defaults to 0).
 * @param  integer $uid        the numeric user id (defaults to 1)
 * @param  string  $body       the body copy to use on the node, defaults
 *                             to lorem ipsum.
 * @return Entity              the node entity object that was created.
function _create_node_and_menu_item($title, $alias, $menu_name = '', $menu_title = '', $plid = 0, $uid = 1, $body = '') {
  if (!$body) {
    $body = '<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>';
  $node = entity_create('node', array(
    'type' => 'page',
    'uid' => $uid,
    'status' => 1,
    'comment' => 1,
    'promote' => 0,

  // Get a wrapper for adding meta-data.
  $entity = entity_metadata_wrapper('node', $node);
  // Using the wrapper, we do not have to worry about telling Drupal
  // what language we are using. The Entity API handles that for us.
  $entity->body->set(array('value' => $body));

  if ($alias) {
    // FIXME this line is broken :'(
    $entity->path->set(array('alias' => $alias));
  if ($menu_name) {
    if (!$menu_title) {
      $menu_title = $title;
    // FIXME this line is broken :'(
      'link_title' => $menu_title,
      'menu_name' => $menu_name,
      'plid' => 0,
      'enabled' => 1,
  // Save the entity and its meta-data.
  return $entity;

I realize there are other ways to create a path using path_save(), but this is predicated on knowing the node id first. A valid answer here would show how to do this purely with Entity API and one call to $entity->save(). Is it possible?

share|improve this question
up vote 2 down vote accepted

I would suggest that you use the node_save() and have a $node->path set to create the desired path alias, rather than using $entity->save() because if inspect (via dpm()) the output of an entity_load() of a node, you would see nothing there related to the node's path alias.

Of course you could use $entity->save() > then fetch the node/entity id > and then use path_save() too.

share|improve this answer
Thanks, after looking at entity/modules/node.info.inc its obvious that both menu and path are not implemented, so the only real way to do this in "one single save" step, is to use node_save(). Drats :( – JamesWilson Feb 18 '14 at 16:16
@JamesWilson If you can be bothered to implement it you could get this working - it'll involve implementing hook_entity_property_info_alter() to add the path property, then a custom getter and setter to handle updating the data in the path table – Clive Feb 18 '14 at 16:24
@Clive, you're right, and from the technical standpoint, that would be the real solution to this question. – JamesWilson Feb 18 '14 at 16:43

For path alias you can add path alias settings to array which passes entity_create function like this:

 $node = entity_create('node', array(
'type' => 'page',
'uid' => $uid,
'status' => 1,
'comment' => 1,
'promote' => 0,
               'pathauto' => false), //if you use module pathauto));

Same principal should work with menu item settings.

share|improve this answer

After calling $entity->save();, the newly created id should be in the entity object. You can take a look with debug($entity);. Then you use path_save().

Alternatively, use the entity_id('node, $entity); to get the id.

In order to have the entity API change the path for you, you can use hook_node_presave() and call path_save() in it.

share|improve this answer
Hmf I responded to the OP changes to many times already. Anyway the path save in database is a second query since it inserts a second record anyway. – Wtower Feb 18 '14 at 16:20
Sorry, Wtower for updating the OP. I should have been clearer that I was looking for a solution with one single function call and the original version did state nearly that, and Sumeet correctly interpreted what I meant. Your answers are nearly the same, except that you suggest path_save() only which i clearly stated in the OP wouldn't be a satisfactory answer. For me, the next-best solution is node_save(). – JamesWilson Feb 18 '14 at 16:38
No worries. The funny thing is that I had originally suggested that node_save would be best for you, but removed it since you insisted on editing the post. Why do you insist on a single function call though? – Wtower Feb 18 '14 at 16:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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