Search code examples
drupal-6cckdrupal-fapi

Drupal 6, Create a form in a node which save data to another content type (cck)


I need to create a form in the view (not in the edit) of a content type A. This form need to submit the data to a content type B.

I notice that node/<nodeID/edit is the "action" of the form which let you edit a node. But if I put the same action in my form on A it shows me the editing node page of B.

My form is as simple as:

<form action="xxx">
  <input type="text" name="cck_field_to_be_added_in_B" value="foobar">
</form>

Setting the xxx action isn't enough, because the FAPI requires form_id and other stuff... So, How can I build a form which is "correct" and sends the data in the rigth way?

Any idea?

----EDIT----

Using the rimian solution it worked. Heres are the dettails:

I needed the form in the view of a node created with CCK. So I have my module with two functions:

function getForm(){ 
    return drupal_get_form('buildForm');

}

function buildIngredientsForm(){
    $form[]... //bla bla bla build the form
    return $form;
}

Because I want this inside a cck content, than I can "hack" the theming system of CCK. Here explain how: http://drupal.org/node/206980

Now, whenever you wats to display your form just call print mymodule.getForm(); and the magic is done.

Regards, Segolas


Solution

  • Add another callback to the array of functions that are called when the node's edit form is submitted. You'll need to understand the basics of the form API. It's not too hard.

    It goes something like this:

    $form['#submit'][] = 'my_function';
    

    Then..

    function my_function() {
      //do stuff to the other node
    }
    

    See:
    http://api.drupal.org/api/drupal/includes--form.inc/group/form_api
    http://drupal.org/project/examples