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]

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

agentrickard’s picture

Issue summary: View changes
agentrickard’s picture

Issue summary: View changes
amateescu’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
1.22 KB

Here we go.

agentrickard’s picture

Why not use entity_load_multiple_by_properties() here? Less duplicate code.

That doesn't work either, so nevermind.

amateescu’s picture

Because entity_load_multiple_by_properties() does exactly what we're replacing here.

agentrickard’s picture

FileSize
1.21 KB

Right. For some reason the original patch code fails, but this patch works.

agentrickard’s picture

FileSize
1.23 KB

Try again, using loadMultiple() this time.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Yep, that's the correct code. This was a clear misuse of the API so I don't think we need to test it..

The last submitted patch, 3: 2143797.patch, failed testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 7: 2143797-7.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review

7: 2143797-7.patch queued for re-testing.

agentrickard’s picture

Status: Needs review » Reviewed & tested by the community

Latest patch is green.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

yched’s picture

Then TaxonomyTermReferenceFieldItemList needs a similar fix ?

amateescu’s picture

Status: Fixed » Needs review
FileSize
1.01 KB

Always the voice of reason :)

yched’s picture

Status: Needs review » Reviewed & tested by the community

;-)

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.