Custom ajax widget can't save generated value to database - $item always zero 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 =&gt; $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(&amp;$form, &amp;$form_state, $form_id) {
   $form['actions']['submit']['#submit'] = array('b2share_form_submit');
   return $form;
 }

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

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

 $insert = db_insert('field_data_field_b2share')
   -&gt; fields(array(
    'field_b2share_url' =&gt; $values['url'],
    'field_b2share_pid' =&gt; $values['pid'],
    //'entity_id' =&gt; 1,
    //'delta' =&gt; 0,
    //'language' =&gt; 'und',
))
-&gt;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(&amp;$form, &amp;$form_state, $form_id) {
  $form['#submit'][]='b2share_form_submit';
  return $form;
}
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(&amp;$form, &amp;$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'seal_the_deal';
}


function seal_the_deal($form, &amp;$form_state, $entity_id, $entity_type, $form_id) {
 $values = array(
  'url' =&gt; $form_state['values']['b2share_fieldset']['url'],
  'pid' =&gt; $form_state['values']['b2share_fieldset']['pid']
 );

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

    $insert = db_insert('field_data_field_b2share')
     -&gt; fields(array(
        'field_b2share_url' =&gt; $values['url'],
        'field_b2share_pid' =&gt; $values['pid'],
        'entity_id' =&gt; $form_state['node']-&gt;nid,
        'entity_type' =&gt; 'node',
        'bundle' =&gt; $form_state['node']-&gt;type,
        'delta' =&gt; 0,
        'language' =&gt; 'und',
     ))
    -&gt;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