Home > book, drupal, drupal 7, entities, nodes, programatically, Uncategorized > Drupal 7 – Programatically Create Nodes

Drupal 7 – Programatically Create Nodes

December 6, 2010 Leave a comment Go to comments

Getting straight to the topic,

From drupal 6, there have been some small changes in the way users can create a node programatically. Like for eg. instead of using the drupal_execute function, we now have to use drupal_form_submit. Both these functions do the same thing, i.e, submit the form programatically.

Since, I experimented with creating book nodes, i’ll be running through the same example.

Minimally, 3 arguments need to be specified to the drupal_form_submit function in this case.

The first one, is the form id. This id can be got by taking a quick look at the form POST parameters while creating a book “normally”. I did that by using firebug on the node/add/book page.

The 2nd argument is an array of form values that should mimic the values posted from the browser. The focus should be to copy the (relevant) variable names that are seen while analyzing the post request. See below,

The values should go within an array keyed by the key ‘values’

global $user;
// Body Structure
// For more information on how this variable is read by the form engine, see
// drupal_array_get_nested_value.
// Substitute the language "en" for the language of the node. Make sure that
// the appropriate locale is enabled
$body["en"][0]['value'] = "Some body text";

$formState['values']['title'] = "Title of the book"
$formState['values']['language'] = "en"; // substitute for the language of the node
$formState['values']['name'] = $user->name;
$formState['values']['body'] = $body;
$formState['values']['status'] = 1;
$formState['values']['promote'] = 0;
$formState['values']['sticky'] = 0;
$formState['values']['op'] = t('Save');

And the last argument is a node object reference that one must create with the necessary information.

$newNode = array(
 'type' => 'book',
 'book' => array(
 'bid' => $node->book['bid'], // use 'new' here if the book page is of a new book
 'plid' => $node->book['mlid'],
 'weight' => $chapNo,
 'options' => array(),
 'has_children' => 0,
 'module' => 'book'
 'language' => $node->language // This is needed

Last thing to do is to call the drupal_form_submit function.

drupal_form_submit('book_node_form', $formState, (object) $newNode);

Thats it! We’re done!

  1. January 7, 2011 at 5:41 am

    Is there a way to generate the form_build_id so it can be created automatically at the end of another process. For example if you have a custom form using the webform module and would like the output of that form to trigger an action that created a node, how would you work around having to manually find the form_build_id? Maybe I’m over complicating this issue. What I’m trying to do is allow users to create nodes but limit the fields they can actually access.

  2. Nuves
    February 17, 2011 at 4:16 pm

    Nice explanation but it does NOT work in the current Drupal version with php 5.3.
    Probably should the book modul implement hook_forms.

  3. July 8, 2011 at 5:50 am

    Good starter post.

    How do I define a path for my new node? Or more specific how do i stop path auto from kicking in by default ? My non-working code looks like:

    $form_state[‘values’][‘path’] = array(
    ‘alias’ => $data[‘product_id’] . ‘/’ . $data[‘title’],
    ‘pathauto’ => 0

  4. July 16, 2013 at 10:25 am

    Heya i will be for any key time right here. I found the following aboard so i believe it is actually helpful & them helped me out there much. I really hope to provide some thing all over again as well as aid other individuals just like you helped me.

  5. April 4, 2014 at 12:52 pm

    Today, I wet to the beachfront wiuth myy children. I found a sea shell and gave iit to myy 4 yeear oldd daughter aand said “You can hear the ocean if you put this to your ear.” Shee placed tthe shell tto her ear aand screamed.
    There wass a hermit crab inside and iit pinched her ear. She never waqnts
    to go back! LoL I know thjs iis totally off topicc but
    Ihhad to tell someone!

  6. May 8, 2014 at 8:17 pm

    My partner and I stumbled over here by a different web page and thought
    I might check things out. I like what I see so i am just following you.
    Look forward to exploring your web page repeatedly.

  7. June 12, 2014 at 3:59 pm

    Have you ever considered writing an ebook or guest authoring on other
    blogs? I have a blog centered on the same ideas you discuss and would really like to have you share some stories/information.
    I know my readers would value your work. If you are even remotely interested,
    feel free to send me an e-mail.

  8. Ivy
    June 17, 2014 at 4:45 pm

    Right away I am going away to do my breakfast, after having my breakfast coming over again to
    read additional news.

  9. June 18, 2014 at 11:48 am

    Hi to every body, it’s my first visit of this web site; this website contains remarkable and truly excellent stuff for readers.

  10. June 30, 2014 at 2:37 pm

    I don’t know whether it’s just me or if perhaps everyone else encountering problems with your blog.
    It appears as iff some off the written text within youyr posts are running off the screen. Can somebody else please comment and let me know if
    this is happenin to them too? Thiis might be a issue with my browser because I’ve had this happen before.

  11. August 29, 2014 at 8:42 am

    certainly like your web-site but you need to take a look
    at the spelling on quite a few of your posts. A number of
    them are rife with spelling problems and I find it very troublesome to tell the truth however I will surely come
    again again.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: