Hi im trying to add a page programatically (the easy bit) but im having issues trying to add the Menu and URL alias. Please see below.

My node Page creation code

This works!

       // Create a new node
	$form_state = array();
	module_load_include('inc', 'node', 'node.pages');
	$node = array('type' => $node_type);
	$form_state['values']['title'] = 'My title';
	$form_state['values']['body'] = 'This is the body text!';
	$form_state['values']['name'] = 'admin';
	$form_state['values']['op'] = t('Save');
	drupal_execute("{$node_type}_node_form", $form_state, (object)$node);

My attempt at Menu and URL alias for Page

i tried the following for the page menu settings and url alias

$form_state['values']['menu_link_title'] = 'Primary Menu Link';
$form_state['values']['menu_weight'] = 100;
$form_state['values']['pathauto_perform_alias'] = 1;
$form_state['values']['path'] = 'my-new-node-alias';

Though the above didnt work, can anyone help me here?? I've looked through lost of post before submitting this question. Seems like something fairly complex or something blindingly obvious.... im assuming the later.

Please help me, your my only hope.

Thanks!

Comments

vasi1186’s picture

Hi,

for the url alias, can you just remove the "$form_state['values']['pathauto_perform_alias'] = 1;" line and see if it works?

Vasi.

Prine’s picture

Hi mate, i tried that and it didnt work, but then i think it might be other values i was inserting. I finally gave up trying to make changes programatically through drupals api and headed for immediate database changes through db_query().

This worked for url alias only.


$node_type = 'page'; //change this to match the type of node you want
    global $user; //this code will use the current logged in user as the nodes author

    // Create a new node
	module_load_include('inc', 'node', 'node.pages');
	
	$node = new StdClass();
	//creating a bare node
	
	$node->type = $node_type;
	//giving it type
	
	$node->status = 1;
	//give it a published staus
	
	$node->title = "Title";
	//gives title
	
	$node->body = "Text for page goes here, block that sits on top.";
	//gives body
	
	$node->uid = $user->uid;
	//gives author id
	
	node_save($node);
	
	/**** CHANGE URL ALIAS MANUALLY ****/
	
	$query = db_query("select * from url_alias WHERE src = 'node/".$node->nid."' LIMIT 1");   
	//Select row from freshly inserted node

	$mysql_obj = db_fetch_object($query);
	$pid = $mysql_obj->pid;
       //Fetch pid
	
	
	db_query("UPDATE {url_alias} SET dst='%s' WHERE pid = '%d'", 'url alias here', $pid);
//Assign URL alias
	

I just assumed there would be a much quicker way. There probably is!

Now onto creating a menu item. I have no idea how im going to solve this!

IntoTheWoods’s picture

Setting $node->path='url alias here' before node_save($node) doesn't seem to work, possibly for access reasons in these simple boostrap scripts, but you can use the function

path_set_alias('node/'. $node->nid, $node->path, NULL, $language);

to replace everything after the /**** CHANGE URL ALIAS MANUALLY ****/

ronaldmulero’s picture

Add these two lines to your "Create a new node" drupal_execute:

$form_state['values']['menu']['parent'] = 'primary-links:0';
$form_state['values']['menu']['link_title'] = 'My title';

That's enough info for Menu to add the node link to the Primary links menu and for Pathauto to generate the URL alias automatically.

So, in your case:

       // Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => $node_type);
$form_state['values']['title'] = 'My title';
$form_state['values']['body'] = 'This is the body text!';
$form_state['values']['name'] = 'admin';
$form_state['values']['menu']['parent'] = 'primary-links:0';
$form_state['values']['menu']['link_title'] = 'My title';
$form_state['values']['op'] = t('Save');
drupal_execute("{$node_type}_node_form", $form_state, (object)$node);

Tested in Drupal 6.19

cbearhoney’s picture

So which is the best way to create a new node - drupal_execute on a node object or node_save on $form_state['values']?

graysadler’s picture

node_save would use less resources but it depends on where the values come from and your situation.

drupal_execute runs values through the Form API, whereas node_save simply skips that step and creates the node.

Lead Developer and Founder of StreamRiot.com

devoted.designer’s picture

Hey, all that is very helpful, but it didn't work with drupal 7! Giving me the following error:
Fatal error: Call to undefined function drupal_execute()
After some searching, "drupal_execute()" seems obsolete!
Now there's "drupal_form_submit()", but i'm having problems in using it!
I'm getting the following error:
Fatal error: Cannot access empty property in C:\...\...\...\...\modules\field\field.attach.inc on line 198

I'm using the following code:

$form_state = array();
module_load_include('inc', 'node', 'node.pages');
	
$node = array('type' => 'page');
$form_state['values']['uid'] = 1;
$form_state['values']['name'] = 'admin';
	
$form_state['values']['title'] = 'Programatic Page';
$form_state['values']['language'] = 'en';
	
$form_state['values']['body']['en'][0]['value'] = 'My own value';
$form_state['values']['body']['en'][0]['summary'] = 'My own summary';
$form_state['values']['body']['en'][0]['format'] = 'filtered_html';
	
$form_state['values']['menu']['enabled'] = 1;
$form_state['values']['menu']['link_title'] = 'Auto-page';
$form_state['values']['menu']['description'] = 'Own Programatic Page';
$form_state['values']['menu']['parent'] = 'main-menu:0';
$form_state['values']['menu']['weight'] = 5;
	
$form_state['values']['path']['alias'] = 'auto-page';
$form_state['values']['comment'] = 1;
	
$form_state['values']['status'] = 1;
$form_state['values']['promote'] = 0;
$form_state['values']['revision'] = 0;

$form_state['values']['op'] = t('Save');
	
drupal_form_submit('page_node_form', $form_state, $node);

Is there something wrong?

Waiting your help eagerly ...

budda’s picture

I too am looking for a solution to disabling pathauto so my custom alias will take effect on my newly created nodes.

On a side note i found including $form_state['values']['promote'] = 0; actually caused my created nodes to be promoted! Removing the line completely made them not promoted. Weird and had me confused for 4 hours....

devoted.designer’s picture

Check out the following post:
http://drupal.org/node/1173136

That worked for me so i hope it's what you want!