Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

readonly_field_widget

4,815 sites Security covered
View on drupal.org

Provides a new field widget which shows a read-only (or view mode) version of a field on a form.

Useful for adding context while editing content.

Field widgets are rendered using one of the available field formatters for that field. The formatter can be selected in the field widget settings.

The widget is available to use for any field type.

NOTE: The field must have a value for this widget to show - this can either be a default value (set while the widget uses a form type widget or programatically) or from the saved value while on an entity edit form.

- Still shows up if the user doesn't have edit access to the field but they do have view access.
- The formatter options for the field are available under the widget settings.
- Label position and hide/show option is available under the widget settings.

A common scenario where this module is useful is switching the form widget type of an entity reference field conditionally when the referenced entity is known. This makes a prettier UX than a disabled select/autocomplete.

/**
 * Implements hook_ENTITY_TYPE_prepare_form().
 */
function my_module_node_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {

  // Set the value of field_my_entity_ref_field if id is known.
  if ($entity instanceof NodeInterface && $entity->bundle() == 'MY_BUNDLE') {
    $ref_id = \Drupal::request()->get('ref_id');
    $ref_node = \Drupal\node\Entity\Node::load($ref_id);
    if ($ref_node instanceof NodeInterface && $ref_node->bundle() == 'MY_REF_BUNDLE') {
      $entity->set('field_my_entity_ref_field', $ref_node);
    }
  }
}

/**
 * Implements hook_entity_form_display_alter().
 */
function my_module_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) {

  // Change the field_my_entity_ref_field to a readonly field widget if id is known.
  if ($context['entity_type'] == 'node' && $context['bundle'] == 'MY_BUNDLE') {
    $ref_id = \Drupal::request()->get('ref_id');
    $ref_node = \Drupal\node\Entity\Node::load($ref_id);
    if ($ref_node instanceof NodeInterface && $ref_node->bundle() == 'MY_REF_BUNDLE') {
      $component = $form_display->getComponent('field_my_entity_ref_field');
      if ($component) {
        $component['type'] = 'readonly_field_widget';
        $component['settings'] = [
          'label' => 'inline',
          'formatter_type' => 'entity_reference_label',
          'formatter_settings' => [
            'entity_reference_label' => ['link' => FALSE],
          ]
        ];
        $form_display->setComponent('field_my_entity_ref_field', $component);
      }
    }
  }
}

Activity

Total releases
3
First release
Jul 2025
Latest release
8 months ago
Release cadence
5 days
Stability
33% stable

Release Timeline

Releases

Version Type Release date
2.1.0 Stable Aug 7, 2025
2.1.x-dev Dev Aug 7, 2025
2.x-dev Dev Jul 29, 2025