6

I have added some custom fields to the register user form and I'm discovered that they are sent back, packaged in a form element of type "container". My code needs to handle all kind of different custom register fields.

How should I parse these objects and extract the fields from them? Should I look into the arrays and what can I expect to find there?

I can't seem to find any good documentation on these objects.

Edit:

A regular fields received through user registration might look like this:

name
... (Array, 10 elements)
#type (String, 9 characters ) textfield
#title (String, 10 characters ) Username
#maxlength (Integer) 60
#required (Boolean) TRUE
#attributes (Array, 1 element)
class (Array, 1 element)
0 (String, 8 characters ) username
#default_value (String, 0 characters )
#access (Boolean) TRUE
#weight (Integer) -10
#fb_registration_field (String, 4 characters ) name

A form container looks e.g. like this:

field_birtyday
... (Array, 6 elements)
#type (String, 9 characters ) container
#attributes (Array, 1 element)
#weight (String, 1 characters ) 7
#tree (Boolean) TRUE
#language (String, 3 characters ) und
und (Array, 13 elements)
0 (Array, 12 elements)
#entity_type (String, 4 characters ) user
#bundle (String, 4 characters ) user
#field_name (String, 14 characters ) field_birtyday
#language (String, 3 characters ) und
#field_parents (Array, 0 elements)
#columns (Array, 2 elements)
0 (String, 5 characters ) value
1 (String, 6 characters ) format
#title (String, 8 characters ) Birthday
#description (String, 0 characters )
#required (Boolean) FALSE
#delta (Integer) 0
#weight (Integer) 0
value (Array, 16 elements)
#entity_type (String, 4 characters ) user
#bundle (String, 4 characters ) user
#field_name (String, 14 characters ) field_birtyday
#language (String, 3 characters ) und
#field_parents (Array, 0 elements)
#columns (Array, 2 elements)
0 (String, 5 characters ) value
1 (String, 6 characters ) format
#title (String, 8 characters ) Birthday
#description (String, 0 characters )
#required (Boolean) FALSE
#delta (Integer) 0
#weight (Integer) 0
#type (String, 9 characters ) textfield
#default_value (NULL)
#size (String, 2 characters ) 60
#maxlength (String, 3 characters ) 255
#attributes (Array, 1 element)
#theme (String, 25 characters ) field_multiple_value_form | (Callback) field_multiple_value_form();
#field_name (String, 14 characters ) field_birtyday
#cardinality (String, 1 characters ) 1
#title (String, 8 characters ) Birthday
#required (Integer) 0
#description (String, 0 characters )
#prefix (String, 42 characters ) <div id="field-birtyday-add-more-wrapper">
#suffix (String, 6 characters ) </div>
#max_delta (Integer) 0
#after_build (Array, 1 element)
0 (String, 30 characters ) field_form_element_after_build | (Callback) field_form_element_after_build();
#language (String, 3 characters ) und
#field_parents (Array, 0 elements)
5
  • 2
    Your question is somewhat unclear. Specify when you want to access these fields (During form rendering, submit, ..) and also try to include some example code. Are these fields defined with the Fields system or are they custom form elements? Also, Drupal 6 or 7?
    – Berdir
    Commented Mar 9, 2011 at 14:46
  • @Berdir: Form elements of type "container" are only implemented in Drupal 7. Despite the fact that form field is not documented in the Form API reference, information about the form field type is returned by system_element_info().
    – avpaderno
    Commented Mar 10, 2011 at 4:25
  • @Berdir, the question mention that I'm parsing custom form fields for the register user form so I think it should be fairly clear when the parsing is occurring?
    – grm
    Commented Mar 10, 2011 at 6:10
  • You're not saying if you want to parse them during form rendering or form submit, the answer depends on that. Or actually, you are saying both (parse form fields and "they are sent back").
    – Berdir
    Commented Mar 10, 2011 at 8:00
  • Ok, I understand. I want to parse them both on rendering and when they are sent back.
    – grm
    Commented Mar 10, 2011 at 8:12

2 Answers 2

4

You should use $form_state['values'] to handle submitted user data, not $form.

0

There is no good documentation on that form because it is dynamic, any module can implement hook_form_alter() and completely turn it upside down. In your example, the name is added directly by user.module while field_birthday is a field and fields are built in a way that allows them to be displayed multiple times if necessary. That's why they are a container and only inside them is the actual form element.

Assuming you want to parse the actual form, and not the response, you have to go through the form recursively. The function element_children() can help you with that. It returns the keys of all form elements below the one you passed in to that function.

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.