Using #type 'submit' or #type 'button'for Drupal 8 , 9 , 10 , and 11

Last updated :  

As we wrap up our exploration of Drupal’s Form API, we turn our focus to two critical form elements: #type 'submit' and #type 'button'. These elements are integral in controlling form submissions and executing commands, maintaining efficient user interactions with your site.

Introduction to #type 'submit' and #type 'button'

The submit button is designed to send form data to the server when pressed, triggering processes defined in your form's submission handlers. In contrast, the button type can be configured to perform a range of actions, including JavaScript functions, without necessarily submitting the form.

Basic Configuration for Submit Button

Consider the need for a simple form with a submit button to process user data:


$form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Submit'),
    '#attributes' => ['class' => ['button--primary']],
];

    

Key Properties Explained

  • #type: Declares the form element as a submit button, enabling data delivery to the server.
  • #value: Sets the label text displayed on the button, which guides user interactions.
  • #attributes: Allows for assigning classes and IDs, facilitating style and behavioral control via CSS and JavaScript.

Configuration for Custom Buttons

For tasks that don’t require form submission, like resetting fields or calling custom JavaScript, utilize the button type:


$form['actions']['reset'] = [
    '#type' => 'button',
    '#value' => $this->t('Reset'),
    '#attributes' => [
        'onclick' => 'this.form.reset(); return false;',
        'class' => ['button--secondary'],
    ],
];

    

Key Differences and Usage

  • Submit Button: Primarily used to trigger form validation and submission logic, seamlessly integrating with form handling in Drupal.
  • Button: Provides versatile interaction capabilities, such as triggering client-side scripts without affecting the server-side data processing.

Advanced Submit Button Configurations

To enhance actions tied to a submit button, explore additional configurations and functions:

Custom Submission Handlers

Add custom handlers to segregate specific tasks or conditions that suit varied operational requirements:


function mymodule_form_submit(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $values = $form_state->getValues();
    \Drupal::messenger()->addMessage($this->t('Form submitted with value: @value', ['@value' => $values['field_name']]));
}

    

Multi-step Form Navigation

Leverage controls for multi-step forms, guiding users sequentially through steps (partial submissions / progress-saving):


$form['actions']['next'] = [
    '#type' => 'submit',
    '#value' => $this->t('Next Step'),
    '#submit' => ['::nextStepSubmitHandler'],
];

    

Benefits of Using Submit and Button Elements

  • User Engagement: Provides precise control over submission and interaction, enhancing form interactivity and responsiveness.
  • Flexibility: Supports a variety of tasks ranging from form submission to script execution, adapting to the requirements of flexible web application environments.
  • Styling and User Experience: Customizable and stylable, supporting consistency in design and functional display that aligns with UX best practices.

Conclusion

Understanding how to employ #type 'submit' and #type 'button' elements unlocks the potential to create dynamic, engaging, and functionally efficient Drupal form interactions. Mastering these provides you with a vital toolkit for diverse web applications, ensuring intuitive user experiences.

Next Steps

This concludes our detailed exploration of Drupal’s Form API components. As we move forward, consider how these principles apply to complex web forms, exploring more intricate integrations and workflow automations as part of a broader mastery of Drupal development.