In my form I create a field of type list (with the widget box is checked). By default, this field has no value.

In my hook_form_alter () method, I add options (via PHP) has this field.

$form['field_genric_domains']['und']['#options']= _get_domaine_options();

if($form['field_genric_domains']['und']['#default_value'] == NULL){ 
    $form['field_genric_domains']['und']['#default_value']  = array(_get_this_domain_id());

Then I set a default value. I set up a test that shows whether we are editing or creating.

When creating, it works perfectly, but when editing the table (# default_value) is empty. Normally it should contain the values ​​previously inquire.

Why? Thank you for your help


You need to check that value returned by _get_this_domain_id() function is in the array key used to create #options for form element ['field_genric_domains'] .

Example :

If _get_domaine_options() function returns array as :


Then _get_this_domain_id() should return key either 1 or 2 .That will set the default value depending on the array key .

  • It is good, it is not error. The _get_domaine_options () function allows me to retrieve a list of field (array (1 => 'domain.fr', 2 => 'domain.com', 3 => 'domain.de). The _get_this_domain_id () function returns the id of the current domain. The current domain is included in the list of options.
    – Djik
    Feb 25 '14 at 11:05
  • Then it should work properly . One more think check if _get_this_domain_id() is returning only the Id value and not complete array. Feb 25 '14 at 11:09
  • The _get_this_domain_id () function returns an integer that I inserted into a table. Before the end of my hook_form_alter () method, I put a var_dump that returns me an array array (0 => 1)
    – Djik
    Feb 25 '14 at 12:23
  • Another ideas ?
    – Djik
    Feb 26 '14 at 9:13
  • Can you paste your form-alter code here ? or may be that if condition that checks for NULL would be causing that problem try removing it . Feb 26 '14 at 9:30

Found! Made in, when loading a form, drupal will verify that the value is in # default_value also found in # options. If it does not find a match it displays an empty table.

As the set of options and # # default_value is in the code, you must override the operation of drupal and make the comparison in the code before displaying

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.