Using #default_value for form elementsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In our previous lesson, we explored how submission handlers in Drupal's Form API help manage data flow and user tasks using submit and button types. Building on that knowledge, we'll focus on setting initial values with #default_value for form elements, a simple feature that enhances user experience by pre-filling forms.

Understanding #default_value in Drupal

The #default_value property is used in Drupal's Form API to define pre-set values for form fields. This can be particularly useful for improving form usability, saving user time, and reducing input errors by providing intelligent defaults.

Importance of #default_value

Using #default_value ensures that users engage with forms that are partially completed. This can be beneficial in several scenarios:

  • Prefill known information, such as user data for logged-in users.
  • Set default options that align with common choices, minimizing required user input.
  • Enhance user experience by streamlining interaction.

Implementing #default_value

Consider a scenario where a user profile form requires a username. By using #default_value, you can pre-set their last used or preferred username. Here’s a simple example:


$form['username'] = [
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => 'john_doe', // Default value set
    '#description' => t('Please enter your username.'),
];

        

In this example, the "username" text field is prepopulated with "john_doe". When the form loads, the text field displays this value, offering users a starting point.

Using #default_value for Various Elements

#default_value isn't limited to text fields; it can be used for select lists, radios, checkboxes, and more. For instance:

Select Lists


$form['options'] = [
    '#type' => 'select',
    '#title' => t('Options'),
    '#default_value' => 'option_2', // Default selected option
    '#options' => [
        'option_1' => t('Option 1'),
        'option_2' => t('Option 2'),
        'option_3' => t('Option 3'),
    ],
];

Here, "Option 2" appears as the default selection in a dropdown menu.

Checkboxes


$form['subscribe'] = [
    '#type' => 'checkbox',
    '#title' => t('Subscribe to newsletter'),
    '#default_value' => TRUE, // Checkbox checked by default
];

In this checkbox example, the #default_value is set to TRUE, causing the form to render with the checkbox marked by default.

Conclusion

Leveraging the #default_value property in your Drupal forms encourages precise inputs, helps end-users save time, and ensures that you gather prefixed and validated data that aligns with user expectations. It's a minor tweak with substantial benefits, especially for frequently-used forms or recurring actions.

What’s Next?

Our next lesson will dive into using #attributes to customize form elements further with CSS classes and HTML attributes, enabling you to enhance visual presentation and accessibility. Stay tuned!