Entity Reference Subfield Causes Error when Populated

Created on 21 March 2024, 3 months ago
Updated 27 April 2024, 2 months ago

Problem/Motivation

I created a Data Field with subfields of type Entity Reference. When I then create content where I fill out the Data Field, the data is stored successfully but I then get the following warning and error:

Warning: array_flip(): Can only flip string and integer values, entry skipped in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 278 of /var/www/html/drupal/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php)

Error: Object of class Drupal\node\Entity\Node could not be converted to string in Drupal\Core\Entity\EntityStorageBase->buildCacheId() (line 142 of /var/www/html/drupal/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php).

The same happens when a user tries to view the node of the content where the entity reference subfield had been filled out. I have no problem viewing nodes of the same Content Type if I leave the Data Field empty or fill out only subfields of type plain text.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

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

Comments & Activities

  • Issue created by @brejchova
  • πŸ‡«πŸ‡·France lazzyvn paris

    Are you try dev version? Maybe it fixed

  • The issue still persists when using the dev version. We had no problem using v 1 of the module on Drupal 10.1. I attach also the full error backtrace in a file.

  • πŸ‡«πŸ‡·France lazzyvn paris

    So strange. I use taxonomy every day and never have this problem.
    I guess you are not selecting vocabulary in the field settings
    could you debug in file (line 103)
    modules/contrib/datafield/src/Plugin/DataField/FieldFormatter/EntityReferenceLabelFormatter.php
    it must have $reference_type = 'taxonomy_term' and $entity_id = (your tid)

  • $reference_type = 'taxonomy_term' is correct. I have two references to taxonomy terms and a reference to a node in the data field, I get an error on each of them (depending on which of the subfields is filled in and called first).

    The problem seems to be with $entity_id. The problem is likely in the code that passes $item to the function viewElements($item, $langcode) in EntityReferenceLabelFormatter.php. $item->value returns an object (that of the taxonomy term) instead of the tid/nid. (oddly, this is only the case when rendering the data field from the node itself, when I set the manage display to 'rendered entity' in a node that references a node of the content type using the data field causing the error, the table with the subfields renders correctly).

    Do you have any idea why the parameter $item would not have an object with the expected structure? Thank you very much for your help so far.

    • c589de69 committed on 2.x
      Issue #3432787: Entity Reference Subfield Causes Error when Populated
      
  • πŸ‡«πŸ‡·France lazzyvn paris

    @brejchova try the dev version it should be ok (it took me a lot of time but I can't reproduce this problem)

  • Thank you! The fix worked. I had also been debugging and adding this simple condition after line 66 also worked:

    if (!is_string($entity_id)) {
      $entity_id = $entity_id->id();
    }
    

    However, some edits in src/Plugin/DataField/FieldWidget/EntityReferenceAutocompleteWidget.php cause a different error when editing content:

    TypeError: trim(): Argument #1 ($string) must be of type string, Drupal\taxonomy\Entity\Term given in trim() (line 82 of /var/www/html/drupal/web/modules/contrib/datafield/src/Plugin/DataField/FieldWidget/EntityReferenceAutocompleteWidget.php).

    Wrapping the if block starting at line 82 of EntityReferenceAutocompleteWidget.php in an elfis block with the condition is_string($element["#default_value"]) fixed it.

    • lazzyvn β†’ committed ceaee9a0 on 2.x
      Issue #3432787: Entity Reference Subfield Causes Error when Populated
      
  • Status changed to Fixed 2 months ago
  • πŸ‡«πŸ‡·France lazzyvn paris
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024