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?
<?php
/**
* Testing programatic content creation.
*/
function mymodule_update_7000() {
$about = _create_node_and_menu_item('About', 'about', 'main-menu', 'About the site');
dpm($about);
}
/**
* 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->title->set($title);
$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 :'(
$entity->menu->set(array(
'link_title' => $menu_title,
'menu_name' => $menu_name,
'plid' => 0,
'enabled' => 1,
));
}
// Save the entity and its meta-data.
$entity->save();
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?