Using #type 'textfield' for single-line inputfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In our exploration of Drupal's Form API, a versatile component at your disposal is the #type 'textfield'. This element, prominently used for single-line input, is essential for capturing straightforward user data, such as names or identifiers. This lesson will guide you through configuring and utilizing textfields to enhance user experience on your Drupal site.

Introduction to #type 'textfield'

The textfield element is integral for collecting simple text input within a form, making it one of the most commonly used form elements. Understanding its configuration options and how to implement it effectively ensures that your forms are both intuitive and accessible to users.

Basic Configuration

Let’s start by examining the basic structure of a textfield in Drupal's Form API, using a simple example to capture a user's first name:


$form['first_name'] = [
    '#type' => 'textfield',
    '#title' => $this->t('First Name'),
    '#default_value' => '',  // An empty string indicates no pre-filled data.
    '#required' => TRUE,
    '#maxlength' => 50, // Sets a character limit on input.
    '#description' => $this->t('Please enter your first name.'),
];

    

Key Properties Explained

  • #type: Specifies the element type, in this case, a textfield.
  • #title: Provides a label for the input field, aiding user comprehension.
  • #default_value: Sets the initial content of the field, useful if you want to suggest typical input.
  • #required: Boolean attribute, ensuring that this field must be completed before form submission.
  • #maxlength: Controls the maximum number of characters allowed in the textfield input.
  • #description: Offers additional information to guide the user about the input expected.

Advanced Configurations

While the basic configuration is often sufficient, Drupal allows for further customization and functionality enhancements to satisfy more complex requirements.

Placeholder Text

The #placeholder property provides a hint to the user about what to enter in the field, displayed only when the field is empty.


$form['last_name'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Last Name'),
    '#placeholder' => $this->t('e.g., Smith'),
];

    

Prefix and Suffix

The #prefix and #suffix properties allow you to wrap text or HTML before and after the input field. This is useful for displaying units or additional contextual information.


$form['amount'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Amount'),
    '#prefix' => '$',  // Indicates that the input is currency.
];

    

Custom Validation

Drupal offers flexibility to include custom validation specific to your needs by leveraging the submitForm or custom validation handler in your form class.


public function validateForm(array &$form, FormStateInterface $form_state) {
    if (strlen($form_state->getValue('first_name')) < 2) {
        $form_state->setErrorByName('first_name', $this->t('First name must be at least 2 characters long.'));
    }
}

    

Conclusion

Incorporating the textfield element into your Drupal forms empowers you to gather essential user input effortfully. With its comprehensive configuration capabilities, you can ensure that the data collected is precise and intuitive for the end user.

Next Steps

Continuing our journey with Drupal's Form API, the next lesson will focus on "Using #type 'textarea' for multi-line input", equipping you with the skills to handle more extensive data input requirements for your forms.