Updated: new
Problem/Motivation
In trying to set a default value for an entity reference field, the provided API does not function as expected.
Specifically, the problem is in ConfigurableEntityReferenceFieldItemList::getDefaultValue
:
The loadByProperties method does not accept an array of values. However, it fails silently rather than throwing an exception.
Here's the existing code for ConfigStorageController::loadByProperties:
public function loadByProperties(array $values = array()) {
$entities = $this->loadMultiple();
foreach ($values as $key => $value) {
$entities = array_filter($entities, function($entity) use ($key, $value) {
return $value === $entity->get($key);
});
}
return $entities;
}
This method is explicitly for loading a single entity. The calling method trying to load multiple entities. Doing so throws an error:
Error message
Notice: Undefined variable: entity_ids in Drupal\entity_reference\Plugin\Field\FieldType\ConfigurableEntityReferenceFieldItemList->getDefaultValue() (line 44 of core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Field/FieldType/ConfigurableEntityReferenceFieldItemList.php).
Setting the call to ->loadByProperties(array('uuid' => current($uuids)));
fixes the issue, but this code needs rethinking.
Proposed resolution
Patch the faulty logic in ConfigurableEntityReferenceFieldItemList::getDefaultValue()
Remaining tasks
Patch needs to be written.
User interface changes
None
API changes
None.,
Original report by [agentrickard]
Comment | File | Size | Author |
---|---|---|---|
#15 | 2143797-followup.patch | 1.01 KB | amateescu |
Comments
Comment #1
agentrickardComment #2
agentrickardComment #3
amateescu CreditAttribution: amateescu commentedHere we go.
Comment #4
agentrickardWhy not use entity_load_multiple_by_properties() here? Less duplicate code.That doesn't work either, so nevermind.
Comment #5
amateescu CreditAttribution: amateescu commentedBecause entity_load_multiple_by_properties() does exactly what we're replacing here.
Comment #6
agentrickardRight. For some reason the original patch code fails, but this patch works.
Comment #7
agentrickardTry again, using loadMultiple() this time.
Comment #8
amateescu CreditAttribution: amateescu commentedYep, that's the correct code. This was a clear misuse of the API so I don't think we need to test it..
Comment #11
amateescu CreditAttribution: amateescu commented7: 2143797-7.patch queued for re-testing.
Comment #12
agentrickardLatest patch is green.
Comment #13
catchCommitted/pushed to 8.x, thanks!
Comment #14
yched CreditAttribution: yched commentedThen TaxonomyTermReferenceFieldItemList needs a similar fix ?
Comment #15
amateescu CreditAttribution: amateescu commentedAlways the voice of reason :)
Comment #16
yched CreditAttribution: yched commented;-)
Comment #17
catchCommitted/pushed to 8.x, thanks!