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

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hctom created an issue. See original summary.

hctom’s picture

Issue summary: View changes

Fixed typo in module name

pdureau’s picture

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

It is a new feature, so let's check that in beta2

hctom’s picture

Only added the proposed fix as issue fork / MR, so we have a starting point and it may be used for patching the module.

pdureau’s picture

Assigned: Unassigned » pdureau
Status: Active » Needs review
pdureau’s picture

Status: Needs review » Fixed

Disclaimer: This issue is not about computed field properties (in Drupal Core, found in: DateRangeItem, DateTimeItem, EntityReferenceItem, FileUriItem) but computed fields.

Unfortunately, AFAIK, there are no entity type with computed fields in Drupal Core.

So, considering the change is very light (a single line) and looks safe, I will merge it.

  • pdureau committed a22fee34 on 2.0.x authored by hctom
    Issue #3466359 by hctom: Allow computed fields to use Component...
pdureau’s picture

Assigned: pdureau » Unassigned
pdureau’s picture

Status: Fixed » Closed (fixed)