Hello,

It's a complex feature I want to add into my own module.

First, my module let user to add a specific cck field into any content-form. The cck is only a button to load a modal frame with complex jquery behaviors to mass upload pictures, process pictures (rotate, crop, etc...) and save each picture as a "my_picture" content type. The "my_picture" content type is a node-based module.

I create a new content type called "album" and add my cck field. The cck field is named "field_test". Like you imagine, user can add several of this cck component into the same content-type. So I must reference each cck component with it's name.

When all process are ended for the upload, the modal frame is closed and return to parent form a list of "my_picture" id's, stored in a hidden field (called {component_name}_hidden_pids).

When I build the content-type form, I add a custom submit to the form, to handle data and manipulate it. I would like to get the value of the hidden field to retrieve the ids of "my picture"'s, but the problem is that I must do it with the reference, by iterate on the $form['#field_info'], test if each result type == my cck component.

Each time I find a correct match, I must search into the "form_state" to get the "{component_name}_hidden_pids" value and process it. The problem is when I'm in the submit function, I can't retrieve the nid of the new album I create, because it's not saved in database at this time...

I don't know how to do the trick, because I must know the component name to find into the form_state "hidden field", but because the content-type isn't created, I can't associate each pid with it... It's a vicious circle...

Any suggestion are welcome. Thanks in advance.

Comments

m4olivei’s picture

The function node_form_submit() in /modules/node/node.pages.inc is the submit function that runs when a node is saved (insert or update). It sets the nid of the node created inside of $form_state['nid']. Check for this variable in the $form_state array of your submit function. If its not there, its likey that your submit function is running before the node.module's submit function. Move your submit function to the end of the $form['#submit'] array and see if that helps.

Matt

titouille’s picture

Hi matt,

It's what I understand too, but I can't see how to add my submit after all other... It's added at the end of the #submit array but in my $form_state, nid doesn't exists... I tried to add it into hook_node_form_after_build but same result, the custom submit is called at the same time...

After that, I tried to implement a "hook_field" to handle "insert" operation, but when it's the "insert" op, my "hidden field" is empty. It's only in the "validate" op that I can see my hidden value, but at this time, the nid isn't set.

The only solution I found is to re-affect the hidden value into a new form_state['values'] place like form_state['values']['{component_name}_pids'] and this time, in the "insert" op, I can get my hidden value and process it like I want.

I think it's strange to be forced to re-assign the value to get it, but maybe it's because my module is not correctly implemented.

titouille’s picture

Hi,

Ok, my problem occurs because I didn't affect the correct value from the form_state into my form_alter function. When the saving methods are called by drupal, the form_alter hook is re-called. So if I don't use the "#default_value" with the correct value set in my form component, when the op "insert" is called, my form_state value is empty.

I can't move the _submit callback to handle it after the node is created, but I can use the hook_field to get nid and process my additional values.

marita93’s picture

function myfunc_node_form_submit_handler($form, &$form_state) {
if ($form_state['values']['nid']) {
_myfunc_write_pdf($form_state['values']['nid'],
$form_state['values']['Month'],
$form_state['values']['Year']);
}
}

function myfunc_nodeapi (&$node, $op) {
if ($op=="insert") {
_myfunc_write_pdf($node->nid,$node->Month,$node->Year);
}
}

chris_car’s picture

This is the solution to get the node nid in a custom submit handler even for a newly created node: http://drupal.org/node/871138#comment-4648964

drupalastic’s picture

Please Try :

dpm([$form_state][0]['build_info']['args'][0]);

Hope it helps...