Allow computed function callbacks to determine cardinality dynamically

Created on 29 March 2024, 9 months ago
Updated 13 May 2024, 7 months ago

Problem/Motivation

On the original issue, we patched in some logic that adds multiple values to a field that has cardinality > 1 (see commit af74aa5f ), but it doesn't seem to work for my use-case.

It's possible I'm misunderstanding the use-case covered in the original patch, or that I'm not getting how to use it to do what I want, but it *seems* like the goal was similar to mine. I have a computed field whose cardinality is dynamically determined based on the number of values in the set of other fields being used to compute the field value. So I want computed field to call my function (parameterized with a $delta) enough times to cover all the possible values. It seems to me the way to handle this is to let the function indicate when it's done, by returning nothing when it hits a $delta for which there's no further values.

As it stands, I couldn't get this to work without patching my local copy of the logic in the commit above. Possibly I'm missing something, of course- and I'm concerned about breaking whatever the original use-case was with my changes, but they allow my computed field to work as desired for now.

One other note: it looks to me like the original code doesn't really handle the Unlimited case very well. In [generateMissingValues](https://git.drupalcode.org/project/computed_field/-/commit/af74aa5f13a4b...) we do a while loop checking if thereAreMoreValuesToCompute(). That method always returns TRUE if the cardinality is UNLIMITED, so it seems like it'll never stop that while loop. Again, maybe I'm missing something here :)

Steps to reproduce

  1. Create a computed field on a node
  2. Try to write a computed value function that returns a dynamically determined list of field values

Proposed resolution

As I say, the patch I'll attach makes this feature behave the way *I* would want :)

Remaining tasks

Determine whether this patch breaks another legitimate use-case for "multi-value computed fields". If so, figure out a compromise to achieve both. If not, commit my patch? ;)

API changes

Effectively, the behaviour of a computed multi-value changes, so the function returning the computed value can determine how many values the field has.

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇨🇦Canada spiderman Halifax, NS

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

Comments & Activities

Production build 0.71.5 2024