tags:

views:

735

answers:

3

I would like to programatically (using php) fill out an existing drupal form to create a content type that is included in a contributed module.

Details: The module is SimpleFeed and the content type is Feed. I would like to call the module's functions to accomplish this. The method I am interested in is hook_insert which appears to require vid and nid which I am unsure what these are.

Any help is appreciated.

+1  A: 

can you provide a bit more information (which modules?). generally, i'd probably suggest calling the modules functions to create the content type, instead of trying to pass it through a form programatically. this way you don't have to worry about implementation, and can trust that if the module works, it'll work for your script too :)

of course this does tie your module to theirs, so any changes in their functions could affect yours. (but then again, you run that risk if they update their database structure too)

ex.

// your file.php

function mymodule_do_stuff() {
    cck_create_field('something'); // as an example, i doubt this
                                   // is a real CCK function :)
}

edit: vid and nid are node ID's, vid is the revision id, and nid is the primary key of a particular node. because this is an actual node, you may have to do two operations.

  1. programatically create a node

    you'll have to reference the database for all the exact fields (tables node and node_revisions), but this should get you a basic working node:

    $node = (object) array(
        'nid' => '',            // empty nid will force a new node to be created
        'vid' => '',
        'type' => 'simplefeed'. // or whatever this node is actually called
        'title' => 'title of node',
        'uid' => 1,             // your user id
        'status' => 1,          // make it active
        'body' => 'actual content',
        'format' => 1,
                                // these next 3 fields are the simplefeed ones
        'url' => 'simplefeed url',
        'expires' => 'whatever value',
        'refresh' => 'ditto',
    );
    
    
    node_save($node);
    

    now i think it should automatically call simplefeed's hook_insert() at this point. if not, then go on to 2. but i'd check to see if it worked out already.

  2. call it yourself!

    simplefeed_insert($node);
    

edit2: drupal_execute() isn't a bad idea either, as you can get back some validation, but this way you don't have to deal with the forms API if you're not comfortable with it. i'm pretty sure node_save() invokes all hooks anyhow, so you should really only have to do step 1 under this method.

Owen
+1  A: 

The drupal api provides drupal_execute() to do exactly this. I would suggest you avoid calling the functions directly to create the node (unless there is a performance reason). By using drupal_execute() all the proper hooks in other modules will be called and your code is far more likely to continue to work through future versions of drupal.

Note that a classic bug in using this method is not first calling something like

module_load_include('inc', 'node', 'node.pages')

which will load the code for your node creation form.

Calling node_save directly is generally considered deprecated and could leave you with broken code in future versions of drupal.

There is a nice example at this lullabot post

Steven Noble
do you have a reference for that? node_save is how drupal builds nodes, i haven't seen any indication they are moving to a different model.
Owen
I should have been more clear. I have not heard that node_save is being deprecated, but using it to solve this type of problem is. I've had several drupal irc conversations where I was told that node_save was the old way to solve this problem and drupal_execute is the new way.
Steven Noble
There is a nice discussion of when to use each in the comments at http://www.lullabot.com/articles/quick_and_dirty_cck_imports
Steven Noble
ah nice, thanks for the info
Owen
A: 

Here's a nice article that gives a good description of drupal_execute. Its the standard way to fill out forms programatically

http://drupal.org/node/293663

It should solve the problem

Nerrve