1

I am trying to get the "#default_value" property of a form element that is embedded in a fieldset.

The problem is that in my code I know the element name only, but I do not know the element fieldset in which it resides. Is there a mechanism to fetch that out without having to search up and down the $form array to find in which fieldset my element resides, and then getting it using $form['someFieldset']['myElement']['#default_value']?

1 Answer 1

1

The array indexes contained in $form_state are reported in Form generation.

The default values are only present in the $form array, which is passed as argument to the form validation handlers, the form submission handlers, and any implementation of hook_form_alter() (including hook_form_FORM_ID_alter()).

The only way to access the default value for a form field is to know the array structure of $form. If the code is using a form created from a module you are writing, that is not difficult; if the code is using a form created by a module you didn't write, you need to check the structure of the form array created by that module.

4
  • it is a module that i am writing...the problem is that $form_state is a flattened version of the $form. so i loose the tree information of the form.... in one on my inputs, i only receive the form element name, but to access its default_value i need it tree path in the form.... so does that mean i need to write a function that search my $form and find the parent of the element so that i can then get its default_value??... i am assuming evenif i use hook_form_ater, i would still need to know the tree path, no???
    – fawzi
    Commented Sep 15, 2011 at 18:02
  • If it's a module you are writing, you know exactly the structure you are using.
    – avpaderno
    Commented Sep 15, 2011 at 18:39
  • yes i do know the structure... but say in my code i have a loop over all form_state[values][x] . Based on the value of each of these elements i have to decide whether to keep it or replace it with the its default value .... so in that loop i do not have access to the form tree and i could not guess whether x is or is not in a fieldset. so are you telling me that i need to write a function that takes x and returns its default value by traversing the array? is this how drupal deals with it, or am i going about this the wrong way and there is a better drupal method to accomplish the same?
    – fawzi
    Commented Sep 15, 2011 at 18:52
  • The generic code would require you traverse the $form array. If the code just needs to find the default value for specified form fields you know are present (i.e., you are not interested to the default values of form fields added by third-party modules), then you can write code that works for that specific form.
    – avpaderno
    Commented Sep 17, 2011 at 21:32

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.