Drupal - automating form submission content

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

Details: SimpleFeed module and content type Feed. I would like to call the module functions to accomplish this. I'm interested in the hook_insert method, which seems to require vid and nid, which I'm not sure what it is.

Any help is appreciated.

+1


source to share


2 answers


can you provide a little more information (which modules?). in general, I would suggest that you are calling module functions to create the content type, rather than trying to pass it through the form programmatically. this way you don't need to worry about the implementation and can trust that if the module works, it will work for your script as well:

of course, this binds your module to theirs, so any changes to their functionality might affect yours. (but again, you run this risk if they also update their database structure)

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 IDs, vid

is the revision ID, and nid

is the primary key for a specific node. because this is the actual node, you might have to do two things.

  • create node programmatically

    you will need to reference the database for all exact fields (tables node

    and node_revisions

    ), but this should help you get the basic node work done:

    $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 at this point hook_insert()

    . if not then go to 2. but I would check if this is already worked out.

  • name it yourself!

    simplefeed_insert($node);
    
          

edit2 : Not adrupal_execute()

bad idea too, as you can return some validation, but this way you don't need to deal with the Forms API if you're not comfortable with it. I'm sure it node_save()

calls all the interceptors anyway, so you really only need to do step 1 with this method.

+1


source


The Drupal api provides drupal_execute () to do just that. I would advise you to avoid directly calling functions to create a node (unless there is a performance reason). By using drupal_execute (), all correct hooks in other modules will be called and your code will be more likely to continue working through future versions of drupal.

Note that the classic error when using this method does not initially call something like

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



which will load the code for your form node.

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

There is a good example in this lullabot comment

+1


source







All Articles