Setting entity form widgets and view display formatters

Submitted by victor.bourgade on Fri, 12/09/2022 - 09:55

Were you ever looking for the list of widget types and view formatters you can set for an entity? And asking yourself why the documentation of Drupal is so poor about them? Well, there is a reason for that and that's because this list can never be exhaustive as it depends on the modules you have installed. Indeed, Drupal core provide quite few of them but other modules also.

Every time you'll be building a custom entity programmatically, for each of your fields you will have to ask yourself which type of form widget and view formatter you will want by default. This is quite important because when using Drupal default route provider for your entity you'll be able to access the view, edit and cancel page with just few configurations (like you do for the node entity for example: /node/nid, /node/nid/edit, /node/nid/delete).

$fields['closed_at'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Closed at'))
      ->setDescription(t('The date the entity is closed.'))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
        'type' => 'datetime_default',
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'inline',
        'type' => 'datetime_default',

Using the timestamp field definition, you can see that here is set "datetime_default" for the default form widget and "datetime_default" for the view formatter.

The form widget:

Datetime form widget

The view formatter:

Datetime view formatter

Following below are two snippets you can use to quickly list all your form widget available.

The form widgets list:

$widget_types = \Drupal::service('plugin.manager.field.widget')->getDefinitions();
$plugin_ids = array_keys($widget_types);

The view formatters list:

$widget_types = \Drupal::service('plugin.manager.field.formatter')->getDefinitions();
$plugin_ids = array_keys($widget_types);

If you often build custom entity by hand, not using Drupal console for them (and probably still with it), these two snippets are really nice to have around!


Drupal 9 Drupal 8 Entity

About the writer


Victor is a web developer passionnated in drupal and bootstrap technologies. He likes challenges and beautiful designs.

When not behind his computer you'll find him drinking beers with friends or in the middle of nowhere hiking with his dog.