0

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();
  • // are you sure its not fired? function _fmu_new_firm_under_user($form, &$form_state) { dpm("hello world"); } – arpitr Oct 2 '13 at 17:03
  • Yes, I'm sure it is not fired. Neither dpm nor dsm renders any text. – Petrroll Oct 2 '13 at 17:09
  • node_add() already calls drupal_get_form(), and presumably your function is also being wrapped by drupal_get_form()? By the time you get the form back from node_add() it's already been processed so I think hook_form_alter() would be the only way to effect it with your current method. The hook_node_submit() method would be preferable IMO – Clive Oct 2 '13 at 17:41
  • No it's not, this is callback function for a URL (menu item). BTW: Check update. – Petrroll Oct 2 '13 at 17:50
  • Might be that the form is already cached as drupal_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 is hook_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
2

You would be better off using hook_node_submit which would fire when any node is submitted.

You can make it specific to your node by the following:

function MODULE_node_submit($node, $form, &$form_state)
{
    if ($node->type == "NODE-TYPE")
    {
        //do stuff
    }
}
  • Isn't it a bit overkill for one callback attached to one form? I mean, my approach should work, shouln't it? – Petrroll Oct 2 '13 at 17:10
  • I don't think, it's one function call and an if statement if the node is not the one you want. Plus, it will reduce your code and increase maintainability – Scott Joudry Oct 2 '13 at 17:11
  • Ok, I'll think about it. Anyway, I would like to know why my approach doesn't work. – Petrroll Oct 2 '13 at 17:13
0

You don't add the callback to the actions, you add it directly to the ['#submit']

Soo

$new_form['#submit'][] = '_fmu_new_firm_under_user';

If you are trying to add it to a button you would do something like

$new_form['buttons']['continue']['#submit'][]
  • 1
    The first suggested approach doesn't work (tried it), the second one is the same as mine (actions is the name of the group in which the buttons are so I'm adding it directly to a button). – Petrroll Oct 2 '13 at 17:34
  • Add a die(); after the dsm(); and refresh the cache. What you posted should work. – Steven Oct 2 '13 at 17:35
  • Nope, it doesn't work. The callback is just simply not fired, damn. – Petrroll Oct 2 '13 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.