By Prine on
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
Hi, for the url alias, can
Hi,
for the url alias, can you just remove the "$form_state['values']['pathauto_perform_alias'] = 1;" line and see if it works?
Vasi.
Hi mate, i tried that and it
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.
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!
$node->path
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
to replace everything after the /**** CHANGE URL ALIAS MANUALLY ****/
drupal_execute node and menu at same time
Add these two lines to your "Create a new node" drupal_execute:
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:
Tested in Drupal 6.19
So which is the best way to
So which is the best way to create a new node - drupal_execute on a node object or node_save on $form_state['values']?
node_save would use less
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
How to achieve that in Drupal 7?
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:
Is there something wrong?
Waiting your help eagerly ...
I too am looking for a
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....--
Ixis (UK): Drupal support, Drupal hosting.
Check out the following
Check out the following post:
http://drupal.org/node/1173136
That worked for me so i hope it's what you want!