- Issue created by @erik.erskine
- π¬π§United Kingdom joachim
Could you rebase this so tests run again please?
It would be nice to be able to use SingleValueTrait
for fields of cardinalty 1, but that don't always have a value (like a non-required normal field).
At the moment you can't do that, and it's not obvious why.
Consider the following implementation:
singleComputeValue(EntityInterface $host_entity, ComputedFieldDefinitionWithValuePluginInterface $computed_field_definition): mixed {
if (/* some condition */) {
return 'some value';
}
else {
return NULL;
}
}
The trait produces these field values:
[
0 => NULL,
]
This leads to some cryptic error messages down the line:
Warning: array_flip(): Can only flip string and integer values, entry skipped in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 278 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
The trait should probably produce this: (ie empty)
[]
.
Change SingleValueTrait
as described. Also add something to the function's DocBlock to explain that returning NULL means "the field is empty".
Patch, test etc.
Active
4.0
Code
Could you rebase this so tests run again please?