8

I have form alter for the user_register_form which is called. In this hook, I try to set a default value for a textfield but it doesn't work. I've tried all the syntax possible #default_value, #value, value, default_value, event in the attributes array.

This is my code.

function pre_registration_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['field_code_postal']['#cache']['max-age'] = 0;
  $form['field_code_postal']['#default_value'] = 'COUCOU';
  $form['field_code_postal']['#value'] = 'COUCOU';
  $form['field_code_postal']['value'] = 'COUCOU';
  $form['field_code_postal']['value']['#value'] = 'COUCOU';
  $form['field_code_postal']['value']['#default_value'] = 'COUCOU';
  $form['field_code_postal']['value']['value'] = 'COUCOU';
  $form['field_code_postal']['value']['default_value'] = 'COUCOU';
  $form['field_code_postal']['default_value'] = 'COUCOU';

  $form['field_code_postal']['#attributes']['value'] = 'COUCOU1';
  $form['field_code_postal']['#attributes']['#value'] = 'COUCOU2';
  $form['field_code_postal']['#attributes']['default_value'] = 'COUCOU3';
  $form['field_code_postal']['#attributes']['#default_value'] = 'COUCOU4';
}

How can I set a default value?

2
  • $form['field_code_postal'][LANGUAGE_NONE][0]['value']['#default_value']
    – m1r1k
    May 27, 2016 at 11:25
  • Thanks for your answer. It doesn't work, FYI I'm using D8 not D7.
    – Greg__
    May 27, 2016 at 11:56

4 Answers 4

11

According to the documentation for hook_form_alter() you can use code similar to the following one.

 $form['field_code_postal']['widget'][0]['value']['#default_value'] ='COUCOU1';

If you want to, for example, fill an entity referenced field, you can use the following code.

$form['field_image']['widget'][0]['target_id']['#default_value'] = $file;
1
  • For future readers, the documentation for hook_form_alter() does not describe the structure of a form containing entity fields. The description of the form structure used for an entity field is given in the documentation for that field type.
    – apaderno
    Aug 10, 2023 at 8:21
2

For my case, I wanted a default automatic node title for a custom content type.

In hook_form_FORM_ID_alter(), I added a line containing kint($form['title']); which allowed me to see the array structure for the node title field. Then, I changed the default value for the node title with the following code.

$form['title']['widget'][0]['value']['#default_value'] = 'my custom node title';`
0

To set the default value in hook_form_alter(), make a dump of $form. You will see the array structure and what you need to target.

In my case with a webform form, actually I can do this.

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'form_submission_lorem_add_form') {
    if (isset($form['elements']['nom'])) {
      $form['elements']['nom']['#default_value'] = 'hello world!';
    }
  }
}

The form markup then includes the following lines.

screenshot

-2

In Drupal 8:

$form['field_name']['widget']['#default_value'][0] = 'value';

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