[development] Proper use of drupal execute to submit a cck node type manually.

Anthony Wlodarski anthony at thrillist.com
Tue Jul 22 15:00:51 UTC 2008


This is my first post on this mailing list so I might as well make it an 
interesting one...

I am currently using drupal_execute to programatically 
save/update/insert CCK node types.  The problem is that the CCK node 
types never update or get saved.  Here is the snippet of code that is 
having me scratch my head:

<code>

    // we check to see if the item has an existing nid if so then just 
update the current item
    if($item['nid'] != 0)
    {
      // load the existing node
      $node = node_load($item['nid']);

      // we must create the item form manually then call drupal_execute 
on that
      $item_form = array();
      $item_form['title'] = $item['title'];
      $item_form['body'] = $item['body_filter'];
      $item_form['field_item_address1'][0]['value'] = 
$item['field_item_address1'];
      $item_form['field_item_date'][0]['value'] = 
strtotime($item['field_item_date']);
      //todo the two taxonomies
      $item_form['field_item_latitude'][0]['value'] = 
$item['field_item_latitude'];
      $item_form['field_item_longitude'][0]['value'] = 
$item['field_item_longitude'];

      // debug stuff...*/
      print "<pre>" . print_r($item, TRUE) . "<br/>##########<br/></pre>";
      print "<pre>" . print_r($item_form, TRUE) . 
"<br/>##########<br/></pre>";
      print "<pre>" . print_r($node, TRUE) . "<br/>##########<br/></pre>";
   
      //drupal_process_form('item_node_form', $item_form2);
      $error = drupal_execute('item_node_form', $item_form, $node);
      print "Error:<br/><pre>$error</pre>";
    }
    else // we have a new item to create
    {
        // todo list
    }

</code>

There are a couple of things that we can assume about this piece of 
code, $item['nid'] != 0 evaluates to true, thus entering the block of 
code.  The $node object is built from node_load() correctly and also the 
$item_form array matches how the $form_values array would look if I 
printed it out in a hook_submit() function.  I have tried some 
additional things not mentioned in this block like setting the [op] 
parameter manually to 'Submit' or 'Update' but to no avail the node does 
not update.  You will notice that I have debugging output in the form of 
print statements, the results of said print statements are located after 
this body of text.  What is the proper use of drupal_execute()?  This 
would help out immensely as this is a stumbling block for your 
development schedule right now.  Also my source for dreaming up a scheme 
like this is based on a few lines of lullabot code located at: 
http://www.lullabot.com/articles/quick_and_dirty_cck_imports.

Array
(
    [title] => Tisk tisk eat my disk...
    [body_filter] => Lorem ipsum dolor sit amet, consectetuer adipiscing 
elit. Nullam pulvinar porttitor ante. Pellentesque nulla. Donec 
pharetra, odio ut fermentum gravida, elit augue dapibus quam, a auctor 
ligula est eget mauris. Cras bibendum semper tellus. In volutpat 
vehicula magna. Phasellus in turpis. Suspendisse mollis, lectus eget 
facilisis mattis, ante purus tincidunt turpis, eu dictum tortor nunc vel 
justo. Sed consectetuer, sem et aliquet semper, lectus orci gravida 
nulla, eget faucibus est quam ut neque. Praesent eu mi ac urna facilisis 
egestas. Ut odio urna, dignissim vel, lacinia non, bibendum vitae, sem. 
Donec blandit felis at odio. Maecenas ligula magna, porttitor pretium, 
faucibus eu, aliquet imperdiet, ipsum. Quisque nisi eros, tincidunt nec, 
posuere at, vestibulum non, magna. Praesent ultrices, velit nec luctus 
vehicula, sapien ipsum luctus turpis, at porttitor mauris ante et 
tellus. Duis lobortis. Vestibulum tincidunt tellus in lacus.
    [neighborhood] => Array
        (
        )

    [vertical] => Array
        (
        )

    [field_item_address1] => 560 Broadway
    [field_item_date] => 2008-07-22 00:00:00
    [field_item_latitude] => 40.75
    [field_item_longitude] => -73.98
    [nid] => 326
)

##########

Array
(
    [title] => Tisk tisk eat my disk...
    [body] => Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
Nullam pulvinar porttitor ante. Pellentesque nulla. Donec pharetra, odio 
ut fermentum gravida, elit augue dapibus quam, a auctor ligula est eget 
mauris. Cras bibendum semper tellus. In volutpat vehicula magna. 
Phasellus in turpis. Suspendisse mollis, lectus eget facilisis mattis, 
ante purus tincidunt turpis, eu dictum tortor nunc vel justo. Sed 
consectetuer, sem et aliquet semper, lectus orci gravida nulla, eget 
faucibus est quam ut neque. Praesent eu mi ac urna facilisis egestas. Ut 
odio urna, dignissim vel, lacinia non, bibendum vitae, sem. Donec 
blandit felis at odio. Maecenas ligula magna, porttitor pretium, 
faucibus eu, aliquet imperdiet, ipsum. Quisque nisi eros, tincidunt nec, 
posuere at, vestibulum non, magna. Praesent ultrices, velit nec luctus 
vehicula, sapien ipsum luctus turpis, at porttitor mauris ante et 
tellus. Duis lobortis. Vestibulum tincidunt tellus in lacus.
    [field_item_address1] => Array
        (
            [0] => Array
                (
                    [value] => 560 Broadway
                )

        )

    [field_item_date] => Array
        (
            [0] => Array
                (
                    [value] => 1216699200
                )

        )

    [field_item_latitude] => Array
        (
            [0] => Array
                (
                    [value] => 40.75
                )

        )

    [field_item_longitude] => Array
        (
            [0] => Array
                (
                    [value] => -73.98
                )

        )

)

##########

stdClass Object
(
    [nid] => 326
    [vid] => 326
    [type] => item
    [status] => 0
    [created] => 1216654030
    [changed] => 1216664081
    [comment] => 0
    [promote] => 0
    [sticky] => 0
    [revision_timestamp] => 1216664081
    [title] => This is bananas
    [body] => Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
Nullam pulvinar porttitor ante. Pellentesque nulla. Donec pharetra, odio 
ut fermentum gravida, elit augue dapibus quam, a auctor ligula est eget 
mauris. Cras bibendum semper tellus. In volutpat vehicula magna. 
Phasellus in turpis. Suspendisse mollis, lectus eget facilisis mattis, 
ante purus tincidunt turpis, eu dictum tortor nunc vel justo. Sed 
consectetuer, sem et aliquet semper, lectus orci gravida nulla, eget 
faucibus est quam ut neque. Praesent eu mi ac urna facilisis egestas. Ut 
odio urna, dignissim vel, lacinia non, bibendum vitae, sem. Donec 
blandit felis at odio. Maecenas ligula magna, porttitor pretium, 
faucibus eu, aliquet imperdiet, ipsum. Quisque nisi eros, tincidunt nec, 
posuere at, vestibulum non, magna. Praesent ultrices, velit nec luctus 
vehicula, sapien ipsum luctus turpis, at porttitor mauris ante et 
tellus. Duis lobortis. Vestibulum tincidunt tellus in lacus.
    [teaser] => Lorem ipsum dolor sit amet, consectetuer adipiscing 
elit. Nullam pulvinar porttitor ante. Pellentesque nulla. Donec 
pharetra, odio ut fermentum gravida, elit augue dapibus quam, a auctor 
ligula est eget mauris. Cras bibendum semper tellus. In volutpat 
vehicula magna. Phasellus in turpis. Suspendisse mollis, lectus eget 
facilisis mattis, ante purus tincidunt turpis, eu dictum tortor nunc vel 
justo. Sed consectetuer, sem et aliquet semper, lectus orci gravida 
nulla, eget faucibus est quam ut neque. Praesent eu mi ac urna facilisis 
egestas.
    [log] =>
    [format] => 1
    [uid] => 1
    [name] => tech at thrillist.com
    [picture] =>
    [data] => 
a:2:{s:8:"editions";a:7:{i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;i:6;i:7;i:0;}s:15:"default_edition";s:1:"1";}
    [path] => This is bananas
    [last_comment_timestamp] => 1216654030
    [last_comment_name] =>
    [comment_count] => 0
    [taxonomy] => Array
        (
        )

    [0] =>
    [field_item_address1] => Array
        (
            [0] => Array
                (
                    [value] => 560 Broadway
                )

        )

    [field_item_address2] => Array
        (
            [0] => Array
                (
                    [value] => Suite 208
                )

        )

    [field_item_city] => Array
        (
            [0] => Array
                (
                    [value] => New York
                )

        )

    [field_item_contacts] => Array
        (
            [0] => Array
                (
                    [nid] => 277
                )

            [1] => Array
                (
                    [nid] => 276
                )

            [2] => Array
                (
                    [nid] => 279
                )

        )

    [field_item_date] => Array
        (
            [0] => Array
                (
                    [value] => 1216080000
                )

        )

    [field_item_directive] => Array
        (
            [0] => Array
                (
                    [value] => test 2
                )

        )

    [field_item_email] => Array
        (
            [0] => Array
                (
                    [value] => anthony at thrillist.com
                )

        )

    [field_item_end_date] => Array
        (
            [0] => Array
                (
                    [value] => 1216771200
                )

        )

    [field_item_excerpt] => Array
        (
            [0] => Array
                (
                    [value] => right
                )

        )

    [field_item_exhortation] => Array
        (
            [0] => Array
                (
                    [value] => is
                )

        )

    [field_item_extended_entry] => Array
        (
            [0] => Array
                (
                    [value] => That
                )

        )

    [field_item_images] => Array
        (
            [0] => Array
                (
                    [nid] => 0
                )

        )

    [field_item_is_event] => Array
        (
            [0] => Array
                (
                    [value] => 1
                )

        )

    [field_item_is_recurring] => Array
        (
            [0] => Array
                (
                    [value] => 1
                )

        )

    [field_item_latitude] => Array
        (
            [0] => Array
                (
                    [value] => 40.75
                )

        )

    [field_item_longitude] => Array
        (
            [0] => Array
                (
                    [value] => -73.98
                )

        )

    [field_item_phone] => Array
        (
            [0] => Array
                (
                    [value] => 2019262168
                )

        )

    [field_item_start_date] => Array
        (
            [0] => Array
                (
                    [value] => 1216771200
                )

        )

    [field_item_url] => Array
        (
            [0] => Array
                (
                    [value] => anthonyw.net
                )

        )

    [field_item_zip] => Array
        (
            [0] => Array
                (
                    [value] => 10012
                )

        )

)

##########

Error:


-- 


Anthony Wlodarski
Web Applications Developer
www.thrillist.com
560 Broadway, Suite 308
New York, NY 10012
p 646.274.2435
f 646.557.0803




More information about the development mailing list