0

I'm currently working on a custom module that communicates with a REST API. It is implemented as a new field type with a custom widget. The entire communication with the API works fine. The thing is that I'd like to save one part of the response in the database and I can't get it to work.

My code is here: https://github.com/stopopol/b2share/blob/master/b2share.module

Basically, I only need the information of one textfield ( $form['b2share_fieldset']['pid_button']) to be saved in the database, but the save function doesn't seem to be called.

I tried following this tutorial: http://getlevelten.com/blog/ian-whitcomb/defining-custom-field-types-drupal but I had no success yet. Maybe it's also important to mention that this field will be embedded in a content type together with plenty of other fields, therefore the communication with the rest api is done via ajax, but the values I get from the API should be stored together with the regular fields in the node form.

I only need someone to tell me what's missing from being able to save it. I've been stuck for days and can't figure it out.

Thanks.

EDIT: Ok. I narrowed it down and it seems like the problem is that

foreach ($items as $delta => $item)

never runs because the size of $items is always 0. I haven't found the reason yet. But will post my insights here.

EDIT2: Thanks for the helpful comments. I used hook_submit and was able to save values in the database, but drupal is complaining about the entity_Id and delta.

Example code I found suggests a function like this:

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

 function b2share_form_submit($form, &$form_state) {
 drupal_set_message( 'submit handler is called' );

 $values = array(
   'url' => $form_state['values']['b2share_fieldset']['url'],
   'pid' => $form_state['values']['b2share_fieldset']['pid']
 );

 $insert = db_insert('field_data_field_b2share')
   -> fields(array(
    'field_b2share_url' => $values['url'],
    'field_b2share_pid' => $values['pid'],
    //'entity_id' => 1,
    //'delta' => 0,
    //'language' => 'und',
))
->execute();
}

But apparently the functions needs to give more info or I am missing something somewhere else.

EDIT 3: Calling b2share_form_submit breaks the node form. The error message says: "The requested page "/.../node//current-revision" could not be found."

Logs say :

"Warning: Missing argument 3 for b2share_form_submit(),"

"Warning: Missing argument 4 for b2share_form_submit()"

EDIT 4: As described here in a comment (Do something after submit the form at hook_form_alter) this is the correct way to write the form_alter function: function b2share_form_alter(&$form, &$form_state, $form_id) { $form['#submit'][]='b2share_form_submit'; return $form; }

  • Attention .. you have nested functions in your code ! – rémy Aug 8 '16 at 12:36
  • And what does that mean exactly? Every function in the code is called. Everything seems fine as far as I can see, except for $items. – stopopol Aug 8 '16 at 12:54
  • You want to save something in hook_field_presave but are using a form, you should save your values in hook_submit. – rémy Aug 8 '16 at 12:59
  • Is it solved? If yes, please post the solution in answer. – Jimmy Ko Aug 9 '16 at 9:46
  • Not quite. I can call the submission_handler and partially fill the database, but I'm not quite there yet. I will post the final solution as an answer. – stopopol Aug 9 '16 at 9:50
0

I found multiple examples on how to write your a custom submit handler, but most of them had a flaw in it. They were either always or never called. Sometimes I couldn't access the node_id, which was crucial for me.

I ended up using this:

function b2share_form_node_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'seal_the_deal';
}


function seal_the_deal($form, &$form_state, $entity_id, $entity_type, $form_id) {
 $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();
     }
   }

Details can be found here:

Custom submit handler is also called when deleting a node

http://atendesigngroup.com/blog/form-altering-node-addedit-forms

Drupal 7 custom form submit handler is not being called

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.