[2.0.0-beta2] Allow computed fields to use "Component per item (UI Patterns)" formatter and its source plugins like any other field

Created on 6 August 2024, 3 months ago
Updated 15 September 2024, 2 months ago

Problem/Motivation

I am currently playing around with the computed_field module in combination with ui_patterns. Unfortunately, computed fields are not able to use the same source plugins as fields with a storage do.

I think the problem is in Drupal\ui_patterns\Plugin\Derivative\EntityFieldSourceDeriverBase::getEntityBundleFieldsMetadata(), where the following condition is implemented:

if (!array_key_exists($field_name, $field_storage_definitions)) {
  continue;
}

Due to the fact, that computed fields do not have a storage, those fields are always skipped. So I altered that condition in my local installation to the following (see the additional check, if the field is computed):

if (!array_key_exists($field_name, $field_storage_definitions) && !$field_definition->isComputed()) {
  continue;
}

This allows computed fields to be selected e.g. with the Add data from field source plugin or use the Add field formatter source directly. Additionally I did not see any errors or problems with this approach yet, while playing around with it.

Steps to reproduce

  • NOTE: I think this should be reproducable with any computed field, but I am mentioning computed_field module here, because I realized the issue with a computed field defined by that module's plugin system
  • Install computed_field and ui_patterns modules
  • Add a computed field to any entity bundle via basic computed_field plugin
  • Head over to Manage display of the entity bundle and apply the Component per item (UI Patterns) formatter to the computed field and use any component that provides a slot
  • Open up the drop button of the slot form and see there is no Add field formatter button
  • Use the drop button of the slot form and select Add data from field - the computed field will not be available in the Field select element
  • Manually apply the above proposed code change, clear all caches and try again - both mentioned source plugins should be selectable now and should work as expected with the computed field

Proposed resolution

  • Review proposed code change, if there may be any negative side-effects - if so, find another solution to support computed fields with Component per item (UI Patterns) formatter

Remaining tasks

  • Create an issue fork and MR to fix this issue

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇩🇪Germany hctom

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024