0

I have fields of entity type foo and bundle name bar. These fields get rendered as an HTML input inside a form. There are other fields present on the form, but I'm only interested in foo/bar ones.

The number of foo/bar fields is dynamic (as are the names). I need a way to extract their name/value pairs.

Here's my situation in code:

function submit($form, &$form_state) {
  // Get all the submitted data that is backed by a field of
  // an entity type 'foo' and bundle 'bar'
}

Thanks.

8
  • I don't see hook_form_submit() anywhere. Do you mean hook_node_submit() or hook_form_alter()?
    – beth
    Dec 13, 2012 at 21:16
  • Sorry beth, that was a bad choice for a name (it's changed now). I actually use hook_form_alter where I do $form['#submit'][] = 'submit';. The point is: I can see the values I need inside $form and $form_state but am looking for a proper way to get them out.
    – geca
    Dec 13, 2012 at 21:54
  • What do you mean by 'see' and 'get them out'?
    – beth
    Dec 13, 2012 at 21:55
  • Well, using a debugger I can see that both objects ($form and $form_state) contain the field values I'm looking for. Now I wonder if there's perhaps a function in D7 that I could use to extract these values (that correspond to certain entity_type/bundle).
    – geca
    Dec 13, 2012 at 22:03
  • 1
    Oh, and are you familiar with Devel module and dpm()?
    – beth
    Dec 13, 2012 at 22:35

2 Answers 2

1

All the field values are in $form_state['values']. See: drupal_form_submit.

You might also be interested in using hook_form_BASE_FORM_ID_alter instead of hook_form_alter to target a particular content type. Your function would look like: 'mymodule_form_' . $form_id . '_alter'.

Or at least an:

if ($form_id == 'your_content_type') { // add new submit handler }

in hook_form_alter.

Also use LANGUAGE_NONE, without quotes, instead of 'und' ie $some->var[LANGUAGE_NONE]['value'];


Also for future reference: Install the Devel module, use dpm($form_state,'$form_state');

When you use dpm you get nice dumps of variables that look like: http://goo.gl/qIf6p

1
  • My solution (using field_info_instances) works fine with hook_form_alter but I'll keep your suggestion in mind. Plus, thank you for devel recommendation, I'm already a happy user. :)
    – geca
    Dec 14, 2012 at 23:35
0

Okay, I solved my problem with field_info_instances function.

array_keys(field_info_instances('foo', 'bar')) gives me the names of the fields I'm interested in.

At this point, all I have to do is to loop thru the field names and access their values like this:

$form_state['values'][$field_name][LANGUAGE_NONE][0]['value']

Works like a charm. But do let me know if there's a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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