0

So these are my functions:

function mycustom_form_alter(&$form, &$form_state, $form_id) { $form['actions']['submit']['#submit'][] = 'b2share_form_submit'; return $form; }

I can also attach the custom submit handler in other ways to the form, but only this way I can still access the nid and the bundle of the node.

function b2share_form_submit($form, &$form_state, $entity_id, $entity_type) {
   $values = array(
   'url' => $form_state['values']['b2share_fieldset']['url'],
   'pid' => $form_state['values']['b2share_fieldset']['pid']
  );

  if ( (empty($values['url'] == false) || (empty($values['pid']) == false)) ) {

    $insert = db_insert('field_data_field_b2share')
      -> fields(array(
       'field_b2share_url' => $values['url'],
       'field_b2share_pid' => $values['pid'],
       'entity_id' => $form_state['node']->nid,
       'entity_type' => 'node',
       'bundle' => $form_state['node']->type,
       'delta' => 0,
       'language' => 'und',
    ))
   ->execute();
 }
}

I can add nodes and edit them perfectly, but whenever I try to delete one, the custom handler function is called and prevent drupal from doing anything. No error messages, nothing in the logs.

Can someone help?

  • hook_form_alter passes the form by reference (notice the ampersand in &$form) and therefor you should not return the $form variable. – Neograph734 Aug 9 '16 at 14:15
  • Thank you for the input. I removed that line, but still can't delete any nodes. – stopopol Aug 9 '16 at 14:22
1

You made the mistake of giving the form submit function a name that matches the patter form-name_submit. So it does not only trigger by your submit button. It is triggered by every submit button in the form.

Rename your function and you should be good.

  • Unfortuntely, no. I called it: function seal_the_deal($form, &$form_state, $entity_id, $entity_type) {...} and it's still called when trying to delete a node. – stopopol Aug 9 '16 at 13:13
  • I guess I missunderstood you answer at first: renaming the other function to function b2share_form_node_form_alter(&$form, &$form_state, $form_id) { $form['actions']['submit']['#submit'][] = 'seal_the_deal'; } did the trick – stopopol Aug 10 '16 at 7:07
  • Perhaps the next time it's better to use your real function names. With all the hooks Drupal uses, function names can provide important context. It will help others help you better. I am glad you got this working. – Neograph734 Aug 10 '16 at 10:17

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.