Computed Field
Allows administrative users to add dynamically-computed fields to entity types, where field values are generated by developer-created plugins.
computed_field
Install
composer require 'drupal/computed_field:^3.0'
Overview
The Computed Field module provides a robust framework for creating fields on Drupal entities whose values are computed dynamically rather than stored in the database. This enables developers to create computed field plugins that generate values based on the entity's existing data, related entities, or any other logic.
Computed fields seamlessly integrate with Drupal's field system, using the same field types and formatters as stored fields. This means site builders can display computed values using familiar widgets like string formatters, entity reference formatters, or any other compatible display format.
The module supports two attachment modes: automatic attachment (where plugins declare their target entity types/bundles in code) and configured attachment (where site admins create fields through the UI). The Computed Field UI submodule provides a user-friendly interface for creating and managing computed fields without code.
A key feature is the lazy builder system, which handles fields with different caching requirements than their host entities. This ensures computed values that depend on external data or have complex cache dependencies are rendered correctly without affecting page caching performance.
Features
- Create computed fields that generate dynamic values from custom plugin logic
- Support for any Drupal field type (string, entity_reference, text, link, image, etc.) allowing reuse of existing formatters
- Automatic field attachment through plugin annotations - fields declared in code are automatically added to specified entity types and bundles
- Configurable field attachment through admin UI - site builders can create computed fields without coding
- Built-in Reverse Entity Reference plugin for finding entities that reference the current entity
- Lazy builder support for proper caching of dynamically computed values with different cache contexts
- Full integration with Field UI - computed fields appear alongside stored fields in the Manage Display interface
- Support for both base fields (all bundles) and bundle-specific fields
- PHP 8 attribute support for modern plugin definition alongside legacy annotations
- Configurable plugins with settings forms for flexible field configuration
Use Cases
Display related content automatically
Use the built-in Reverse Entity Reference plugin to automatically display content that references the current entity. For example, show all articles by an author on their user profile, or display all products in a category without maintaining the relationship manually.
Calculate derived values
Create computed fields that calculate values from other fields on the entity. Examples include full name from first/last name fields, total price from quantity and unit price, or age calculated from a birthdate field.
Display contextual information
Show information that depends on the current request context, like the current user, time, or request parameters. Use lazy builders to ensure proper caching while still displaying dynamic content.
Aggregate data from related entities
Compute statistics or aggregations from related entities, such as average rating from review entities, total order amount from line items, or comment count. The lazy builder system handles cache invalidation when related content changes.
Format complex output
Use the computed_render_array field type to return complete render arrays when the output requires complex markup, embedded views, or other elements that don't fit standard field formatters.
Automatic field attachment across bundles
Developers can create computed field plugins that automatically attach to specific entity types and bundles through annotation configuration, ensuring consistent functionality across the site without manual setup per bundle.
Tips
- Use SingleValueTrait for plugins that return a single value - it simplifies implementation by letting you implement singleComputeValue() instead of building the full array structure
- For plugins with complex caching needs, return TRUE from useLazyBuilder() and provide proper cache metadata in getCacheability() to ensure correct cache invalidation
- Set no_ui: TRUE in your plugin annotation/attribute to prevent it from appearing in the admin UI dropdown - useful for plugins that should only be attached automatically
- Automatic plugins can use the 'dynamic' property and override attachAsBaseField()/attachAsBundleField() methods for complex attachment logic based on existing fields
- The computed_render_array field type gives you full control over output when standard formatters aren't sufficient
- Remember that computed fields are read-only and don't appear on entity forms - they're purely for display purposes
Technical Details
Admin Pages 4
/admin/structure/types/manage/{bundle}/fields/add-computed-field
Form to create a new computed field on a content type. Allows selecting a computed field plugin, configuring its settings (if available), and setting the field label and machine name.
/admin/structure/types/manage/{bundle}/fields/computed/{computed_field}
Edit form for modifying an existing computed field's settings including its label and plugin configuration.
/admin/structure/types/manage/{bundle}/fields/computed/{computed_field}/delete
Confirmation form to delete a computed field from the entity bundle.
/admin/reports/fields/computed
Report listing all computed fields configured on the site, showing their entity type, bundle, and plugin information.
Permissions 1
Hooks 1
hook_computed_field_info_alter
Allows modules to alter computed field plugin definitions before they are cached. Can be used to modify plugin properties, change classes, or remove plugins from discovery.
Troubleshooting 5
Apply the core patch from https://www.drupal.org/project/drupal/issues/3349739 for automatic base field declaration in Views. Bundle fields require manual Views integration as there's no core support yet.
Ensure your plugin implements useLazyBuilder() returning TRUE and getCacheability() returning proper cache tags. The lazy builder system handles cache invalidation based on the metadata you provide.
This is a known Drupal core issue (https://www.drupal.org/project/drupal/issues/3045509). Bundle fields are not included in the field map. The fields still function correctly for display purposes.
Ensure your plugin class implements both ConfigurableInterface and PluginFormInterface. Check that buildConfigurationForm(), validateConfigurationForm(), and submitConfigurationForm() are properly implemented.
This is tracked in https://www.drupal.org/project/drupal/issues/3016895. Manually remove the field from view displays after deletion if needed.
Security Notes 4
- The 'administer computed_field entities' permission is restricted and should only be granted to trusted administrators
- Computed field plugins have full access to the entity they're attached to - ensure plugin code properly sanitizes output
- When implementing plugins that query other entities, use accessCheck(TRUE) on entity queries to respect access permissions
- Be cautious with plugins that expose data from related entities - ensure appropriate access checks are in place