UPDATE: Approach showed bellow has the same results -> doesn't work.
$new_form['#submit'][] = '_fmu_new_firm_under_user';
Separate button with only that callback doesn't work either. And what's weird, even if it has only the '_fmu_new_firm_under_user' callback it actually works the same way as 'save' button -> it saves the node and doesn't fire my callback.
$new_form['actions']['submit_and_create'] = array(
'#type' => 'submit',
'#value' => t('Vytvořit firmu'),
'#submit' => array('_fmu_new_firm_under_user'),
);
Original message
I've created a page with a 'myContentType_node_form' and attached a callback to the save button. The problem is that the callback is not fired when I press the button. To be more specific the default callback is fired - the node is saved - but my callback is not fired.
function _fmu_register_new_user_firm_callback($uid, $control_hash) { //a callback function for a menu item
include_once(drupal_get_path('module', 'node') . '/node.pages.inc' );
$new_form = node_add('fm_firm');
$new_form['uid']['#value'][] = $uid;
$new_form['actions']['preview']['#access'] = false;
$new_form['actions']['submit']['#submit'][] = '_fmu_new_firm_under_user';
dsm($new_form);
return $new_form;
}
function _fmu_new_firm_under_user($form, &$form_state) {
$user = user_load($form['uid']['#value']);
$user->fmu_users_firm[LANGUAGE_NONE][0]['target_id'] = $form->nid;
user_save($user);
dsm($user);
}
DSM of the form before returning it:
actions (Array, 16 elements)
#type (String, 7 characters ) actions
submit (Array, 23 elements)
#type (String, 6 characters ) submit
#access (Boolean) TRUE
#value (String, 4 characters ) Save
#weight (Integer) 5
#submit (Array, 2 elements)
0 (String, 16 characters ) node_form_submit | (Callback) node_form_submit();
1 (String, 24 characters ) _fmu_new_firm_under_user | (Callback) _fmu_new_firm_under_user();
#input (Boolean) TRUE
#name (String, 2 characters ) op
#button_type (String, 6 characters ) submit
#executes_submit_callback (Boolean) TRUE
#limit_validation_errors (Boolean) FALSE
#process (Array, 1 element)
#theme_wrappers (Array, 1 element)
#defaults_loaded (Boolean) TRUE
#tree (Boolean) FALSE
#parents (Array, 1 element)
#array_parents (Array, 2 elements)
#processed (Boolean) TRUE
#required (Boolean) FALSE
#attributes (Array, 0 elements)
#title_display (String, 6 characters ) before
#id (String, 11 characters ) edit-submit
#ajax_processed (Boolean) FALSE
#sorted (Boolean) TRUE
preview (Array, 23 elements)
#theme_wrappers (Array, 1 element)
#process (Array, 2 elements)
#weight (Integer) 100
#defaults_loaded (Boolean) TRUE
#tree (Boolean) FALSE
#parents (Array, 1 element)
#array_parents (Array, 1 element)
#processed (Boolean) TRUE
#required (Boolean) FALSE
#attributes (Array, 1 element)
#title_display (String, 6 characters ) before
#id (String, 12 characters ) edit-actions
submit_and_create (Array, 3 elements)
#type (String, 6 characters ) submit
#value (String, 15 characters ) Vytvořit firmu
#submit (Array, 1 element)
0 (String, 24 characters ) _fmu_new_firm_under_user | (Callback) _fmu_new_firm_under_user();
node_add()
already callsdrupal_get_form()
, and presumably your function is also being wrapped bydrupal_get_form()
? By the time you get the form back fromnode_add()
it's already been processed so I thinkhook_form_alter()
would be the only way to effect it with your current method. Thehook_node_submit()
method would be preferable IMO – Clive♦ Oct 2 '13 at 17:41drupal_get_form()
has already run, so when the page reloads and the original form is retrieved it doesn't have your changes. You'll be able to affect things on the initial display as that will use the live array you're manipulating, not the cache. Just a theory. The standard way to alter a form ishook_form_alter()
, I've never had any joy with the method you're trying (haven't tried it in a while though) – Clive♦ Oct 2 '13 at 18:04