Using form modes for entity formsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

As we continue to explore Drupal's capabilities, understanding form modes is a crucial step for those looking to create dynamic, user-specific workflows. Form modes offer a powerful means to customize how entities are presented to different users based on specific needs without altering the underlying data structure.

What Are Form Modes?

Form modes in Drupal enable you to specify different form presentations for an entity type. This allows for tailored interfaces, which are particularly useful when different user roles require different form fields or layouts. By using form modes, you can essentially switch between multiple versions of a form that best suit different use cases or workflows.

Benefits of Using Form Modes

  • Custom Workflows: Streamline processes by presenting relevant fields for specific tasks.
  • Improved User Experience: Simplify forms for non-technical users by showing only essential fields.
  • Flexibility: Quickly adapt to changing requirements without recoding form logic.

Implementing Form Modes

Let's build on the Fruit entity example we have been using. We'll create a form mode to tailor the experience for users who need to enter only specific data fields, such as just the name of the fruit, without its color.

Step-by-Step Guide to Using Form Modes

Step 1: Define a Form Mode

To define form modes, you can use the UI provided by the Drupal core or you can define them programmatically in your module. For our example, assume we have used the Drupal admin interface:

  • Navigate to the Structure -> Content types page.
  • Select “Manage form display” for the Fruit content type.
  • Click on “Add form mode” and name it, for example, "Simple Entry".

Step 2: Customize the Form Display

After creating a form mode, edit its form display settings to choose which fields should be visible or hidden for that mode. For the "Simple Entry" mode, disable fields except for the necessary ones like the name.

Step 3: Implement Form Mode Switching in Code


// In src/Form/FruitSimpleEntryForm.php
namespace Drupal\fruit_module\Form;

use Drupal\Core\Entity\ContentEntityForm;

/**
 * Provides a custom form for the Fruit entity using the "Simple Entry" form mode.
 */
class FruitSimpleEntryForm extends ContentEntityForm {

  public function buildForm(array $form, FormStateInterface $form_state) {
    // Load the form display for the specific mode.
    $form_display = \Drupal::service('entity_display.repository')
      ->getFormDisplay('fruit', 'simple_entry');

    // Build the form from the form display.
    $form_display->buildForm($this->entity, $form, $form_state);

    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    
    $entity = $this->entity;
    $entity->save();
    drupal_set_message($this->t('Saved the %label Fruit.', ['%label' => $entity->label()]));
    
    $form_state->setRedirect('entity.fruit.collection');
  }
}

This snippet switches to the "Simple Entry" form mode programmatically, ensuring that users only access fields relevant to their tasks.

Summary

Form modes provide a streamlined approach for managing varied entity form displays in Drupal, enabling developers to meet specific user needs and business requirements efficiently. By using form modes, you can enhance user experience, maintain data integrity, and implement flexible content workflows without extensive coding.

Teaser for the Next Lesson

In the upcoming lesson, we'll explore hook_entity_form_alter(), a powerful tool for dynamically altering entity forms, allowing for on-the-fly customizations without changing the original form structure. Stay tuned to learn how to leverage this hook for maximum flexibility!