Data salad when editing taxonomy terms using data field

Created on 30 July 2024, about 2 months ago
Updated 14 August 2024, about 1 month ago

Problem/Motivation

I added a datafield field to a taxonomy term that consists of two subfields, one is an entity reference (to a node) and the other a plain text note. When editing, the already added entity references are replaced with references to seemingly random different nodes in the edit form. I have not been able to uncover any pattern.

Steps to reproduce

1. Create a new taxonomy
2. Add a new datafield field to the taxonomy with an entity reference subfield linking to an existing content type.
3. Create a term and fill in the datafield. Save. Everything looks good.
4. Open the edit form. The entity reference in the datafield no longer references the node from the previous save. Saving again results in data salad (the correct values are replaced with the wrong ones unless manually fixed).

Examples

Some Title (nid 20) is replaced with Different Title (nid 7) in edit form

The issue sounds similar to https://www.drupal.org/project/datafield/issues/3443443 🐛 Data salad after change weight with new Taxonomy Fixed but the situation is different and this case is not fixed.

🐛 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

    Can you take some screenshots of your config? I can't figure out your problem. I guess you didn't configure the bundle in the second save. In the widget, you didn't select the right widget. Try dev, it might fix it.

  • Thank you for your quick response. This is my field configuration, I did select the bundle on the second save.

    uuid: ada114da-01da-4a8c-8d34-45c76c384fb7
    langcode: en
    status: true
    dependencies:
      config:
        - field.storage.taxonomy_term.field_bibliography
        - taxonomy.vocabulary.work
      module:
        - datafield
    id: taxonomy_term.work.field_bibliography
    field_name: field_bibliography
    entity_type: taxonomy_term
    bundle: work
    label: Bibliography
    description: ''
    required: false
    translatable: false
    default_value: {  }
    default_value_callback: ''
    settings:
      field_settings:
        reference:
          label: Reference
          required: false
          entity_reference_type: node
          target_bundles: bibliographic_citation
        note:
          label: Note
          list: false
          allowed_values: ''
          required: false
    field_type: data_field
    

    I kept the default form configuration for the field, it is set to autocomplete.

      field_bibliography:
        type: data_field_table_widget
        weight: 3
        region: content
        settings:
          inline: false
          field_reference: ''
          widget_settings: {  }
        third_party_settings: {  }
    
  • Thank you for responding. The field configuration file (I did select the bundle on second save):

    uuid: ada114da-01da-4a8c-8d34-45c76c384fb7
    langcode: en
    status: true
    dependencies:
      config:
        - field.storage.taxonomy_term.field_bibliography
        - taxonomy.vocabulary.work
      module:
        - datafield
    id: taxonomy_term.work.field_bibliography
    field_name: field_bibliography
    entity_type: taxonomy_term
    bundle: work
    label: Bibliography
    description: ''
    required: false
    translatable: false
    default_value: {  }
    default_value_callback: ''
    settings:
      field_settings:
        reference:
          label: Reference
          required: false
          entity_reference_type: node
          target_bundles: bibliographic_citation
        note:
          label: Note
          list: false
          allowed_values: ''
          required: false
    field_type: data_field

    I kept the default configuration of the form (autocomplete):

      field_bibliography:
        type: data_field_table_widget
        weight: 3
        region: content
        settings:
          inline: false
          field_reference: ''
          widget_settings: {  }
        third_party_settings: {  }
  • I did have the bundle selected and I have the default autocomplete widget selected.

    The problem seems to be caused by getFormElement(). The value of $element["#default_value"] was a numeric string containing the entity id. Therefore the string condition was applied but the regex did not match anything because the entity id was not in brackets. The code then tried searching for any node matching the query and since my titles contain numbers, it often selected the wrong reference.

    To fix the issue, it was enough to replace if for elseif on line 84 of https://git.drupalcode.org/project/datafield/-/blob/2.0.5/src/Plugin/DataField/FieldWidget/EntityReferenceAutocompleteWidget.php This way the is_numeric condition correctly identifies the entity id.

  • 🇫🇷France lazzyvn paris

    it means line 84

    if (empty($entity_id) && is_string($element["#default_value"])) {
    ...
    
    

    Could you check it?

    • lazzyvn committed ded27d02 on 2.x
      Issue #3464658: Data salad when editing taxonomy terms using data field
      
  • Status changed to Fixed about 2 months ago
  • The committed fix looks good, thank you.

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

Production build 0.71.5 2024