Problem/Motivation

The conclusion reached by #2392845: Add a trait to standardize handling of computed item lists is that a computed field needs a couple of things:

1) a custom field item list class which contains a method responsible for computing the values
2) implement all the "entry points" which can be used to access the field value and call that method

In that issue, a trait has been added to help developers with the second point mentioned above, so they only need to take care of the method which computes the field value(s) "on demand".

This all works out well for fields defined in code, usually base fields, because they can specify a custom field items list to be used instead of the default one. However, fields defined in the UI have no way of doing the same thing, so they can't be marked as computed at the moment.

Proposed resolution

Figure out a solution for how to declare a field added in the UI as computed.

Remaining tasks

TBD.

User interface changes

Maybe a new setting / checkbox in the UI?

API changes

TBD.

Data model changes

TBD.

Comments

amateescu created an issue. See original summary.

yched’s picture

I can see at least two cases :

1) The one the OP seems to be about :
"Through config/UI, I want to add a field of an arbitrary type (e.g. number), and have it be computed by some custom logic I specify somehow (maybe by specifying the name of a custom function, as we do for allowed values ?)"

Some challenges here :
- need to figure a way to specify a piece of custom logic / function in config/UI (and extend field/storage config schemas accordingly)
- need to be able to derive a "computed using an arbitrary function" variant for every existing FieldItemList class (that one might be a bit more tricky & invasive :-/)

2) The "backreference" example
Such a field type (EntityBackRefItemList) would :
- extend EntityReferenceItemList to benefit from the entity_ref API and formatters
- use the ComputedItemListTrait to run the queries that compute the values (all entities of type $settings['target_entity_type'] referencing this entity)
- yet, the existence of a backref field on a given entity_type/bundle would be defined by site admins in config (I don't think we want to auto-create the backref fields for every existing entity_ref field, that would result in a lot of fields)

In short, that case is :
"Through config/UI, I want to add a field of a type (here, backreference) that happens to state that all its fields are computed".

That one seems more approachable :
- no additions to the current config schemas needed (it's just a field type + settings)
- we just (AFAIR) lack a way for a field type to specify '"all my fields are computed"

claudiu.cristea’s picture

My 2 cents:

The FieldStorageConfig has a boolean property custom_storage. In the case of computed fields we should set somehow that flag to TRUE when a new FieldStorageConfig entity is created. We should check the field type for that. Otherwise, the dedicated field tables are still created (only with bundle, deleted, entity_id, revision_id, langcode, delta columns) even the field item ::schema() returns an empty array.

we just (AFAIR) lack a way for a field type to specify '"all my fields are computed"

We can do this:

  • either in @FieldType annotation
  • or adding a new interface method: ::isComputed()
  • or a method, scanning all properties returned by ::propertyDefinitions(). If all are computed, then it's computed.
jibran’s picture

https://www.drupal.org/project/geocoder is an example of computed field I think it falls under #2.1 category.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

penyaskito’s picture

A new example: I needed a computed field that shows if another field of the same entity has been set (in my case I wanted to show if a file was uploaded in a different field of the same entity, but we could make this as generic as we wanted to).

E.g.

New option 3: Create a computed bool field which checks if the file is set, and make which field it refers to configurable via UI or
Option 2.1: Create a computed bool field where I can specify a custom php function that checks the above.

graber’s picture

I'm trying to create a field that allows to add/edit entities which reference the current entity. All the storage is in the referencing entities so there should be no schema as this is a computed field.

According to Drupal\Core\Field\FieldItemInterface::schema() doc comment: "Computed fields having no schema should return an empty array.", but then I get the storage settings form and field instance delete form error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 1 OFFSET 0' at line 5: SELECT 1 AS expression
FROM
{node_revision__field_testbook} t
WHERE
LIMIT 1 OFFSET 0;

This is definitely a bug as I have to stay with the needless storage.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

There's a contrib implementation of this: https://www.drupal.org/project/computed_field_plugin

It works with a plugin for the computed field's logic, though one improvement on its architecture would be to allow a field config entity to be created, which holds configuration for the plugin.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joachim’s picture

Update on my last comment - I've now made a new major version of https://www.drupal.org/project/computed_field which uses a config entity to store plugin configuration.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.