Read-only Field Widget
Provides a field widget that displays field values in read-only format using field formatters on entity edit forms.
readonly_field_widget
Install
composer require 'drupal/readonly_field_widget:^2.1'
Overview
The Read-only Field Widget module provides a field widget that replaces editable form inputs with read-only formatted output on entity forms. This is useful when you want to display field values on edit forms without allowing users to modify them, while still maintaining the field's presence in the form.
The widget uses Drupal's field formatter system to render the field values, giving you full control over how the read-only data appears. It dynamically supports all field types that have available formatters, making it a universal solution for creating non-editable field displays.
The module intelligently handles field access - if a user doesn't have edit access to a field but does have view access, the read-only widget will still display the field value. This respects Drupal's field-level access control system while providing a better user experience than simply hiding restricted fields.
Features
- Universal field type support - works with any field type that has a formatter available
- Uses field formatters for display - leverage all formatter options and settings for the read-only display
- Configurable label position - choose from Above, Inline, Hidden, or Visually Hidden label placement
- Formatter settings integration - access all settings for the selected formatter directly in the widget configuration
- Optional field description display - show or hide the field's help text below the read-only value
- Error validation toggle - control whether validation errors are displayed for readonly fields
- Access control integration - respects field-level view permissions, showing fields to users who can view but not edit
- Entity reference support - properly displays referenced entities using their configured formatter
- Support for default values - works with field default values on entity creation forms
- Compatible with Drupal 8, 9, 10, and 11
Use Cases
Displaying auto-generated field values
Use the readonly widget for fields that are populated programmatically or through automated processes, such as computed fields, auto-generated reference numbers, or system-assigned values. Users can see the value without accidentally modifying it.
Showing entity references without modification
For entity reference fields where the referenced content should be visible but not changeable by content editors, the readonly widget can display the referenced entity using any available formatter (label, rendered entity, etc.) while preventing changes.
Creating tiered editing permissions
When some users should be able to view certain field values but not modify them, combine the readonly widget with Drupal's field-level access control. Users with view-only access will see the formatted value, while those without view access won't see the field at all.
Preserving default values on creation forms
For fields with default values that should be visible but not editable during entity creation, the readonly widget displays the default value. This is useful for showing pre-assigned categories, status values, or system defaults.
Displaying computed or derived data
For fields that display data computed from other sources (such as aggregated values or data pulled from external systems), the readonly widget ensures users understand this information is display-only and cannot be directly edited through the form.
Audit trail and metadata display
Show creation dates, author information, revision history, or other metadata fields on edit forms in a read-only manner. Users can see this important context while editing other fields without risk of modification.
Tips
- The widget summary in the form display table shows the current formatter, label position, and description setting for quick reference
- Use 'Rendered entity' formatter for entity references when you want to show the full referenced entity display, not just a link
- The 'Visually Hidden' label option maintains accessibility while hiding the label from sighted users
- Combine with Field Permissions module for granular control over who can view readonly field values
- Test readonly widgets with different user roles to ensure field access behaves as expected
- For multi-value fields, the entire field is rendered read-only - individual delta values cannot be selectively edited
Technical Details
Hooks 3
hook_field_widget_info_alter
Makes the readonly_field_widget available for all field types. The module iterates through all available field formatters and collects their supported field types, then assigns this complete list to the readonly widget's field_types property.
hook_form_alter
Controls field access for readonly widget fields based on view permissions. When a field uses the readonly widget, its form access is set based on whether the current user can view the field value, not edit it. This allows fields to be displayed to users who have view-only access.
hook_theme_suggestions_alter
Handles template suggestions specifically for the title field when using the readonly widget. Core overrides the 'field' template for node titles, which conflicts with readonly_field_widget behavior. This hook ensures the standard 'field' template is used for titles configured with the readonly widget.
Troubleshooting 5
The readonly widget only displays when the field has a value. Ensure the field either has a default value configured or the entity has an existing value for that field. On new entity forms, fields without default values will not appear.
This is expected behavior. When viewing the field's default value settings, the readonly widget displays a status message indicating you need to switch to an editable widget to set default values. Change the widget temporarily, set the default, then switch back to readonly.
After changing the formatter type, wait for the AJAX request to complete before adjusting formatter-specific settings. The settings form updates dynamically based on the selected formatter.
The module includes specific handling for title fields due to core template overrides. If issues persist, check that no custom theme is overriding field templates in a way that conflicts with the module's theme suggestions.
The module sets field access based on view permissions, not edit permissions. Ensure your field access hooks or configuration correctly define view access. Fields completely hidden from a user (no view access) will not appear in the form.
Security Notes 4
- Field values displayed through the readonly widget respect Drupal's field-level access control - users without view access will not see the field
- The widget does not prevent server-side validation; enabling error_validation helps catch any constraint violations on the field
- Values cannot be modified through the readonly widget, but other code or APIs might still change field values - this is display-only protection
- The module uses Drupal's standard entity view builder and formatter system, inheriting their security characteristics