Datafield field form missing items

Created on 4 March 2025, about 1 month ago

Problem/Motivation

In certain configurations the form for a datafield can be incomplete, which can lead to data rewriting. The problem seems to be entity reference subfields (verified with taxonomy references). From testing the pattern is following:

  • datafield contains at least one entity reference subfield
  • all subfields up to and including first entity reference subfield are empty

Then after a save, data entered to any other subsequent subfields will be missing from the node edit form. After another save, original data is rewritten with new data.

Steps to reproduce

  1. Create a datafield with a text subfield, entity reference subfield (referencing taxonomy) and another text subfield
  2. Fill out only the last text subfield and save the node
  3. Edit the node (the previously saved value is missing from the form)
  4. Save the node
🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇨🇿Czech Republic xadler1

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

Comments & Activities

  • Issue created by @xadler1
  • 🇫🇷France lazzyvn paris

    it's not a bug it's a feature. when a line is empty it will check and delete that line. Please note this module is written for the purpose of storing data and sub field is not like the fields you usually use.

  • 🇨🇿Czech Republic xadler1

    This seems strange to me to be a feature, maybe my example/explanation wasn't the best.

    Simplest example is a datafield with entity reference and plain text fields (in this order). If I leave the entity reference empty and fill out the text field, the datafield will be saved correctly. But on next edit, the form will show an empty line even though there is data that could be edited.

    The first example summarized into table:

    Field definition from first example:

    status: true
    dependencies:
      config:
        - field.storage.node.field_test_datafield_5
        - node.type.test_type
      module:
        - datafield
    id: node.test_type.field_test_datafield_5
    field_name: field_test_datafield_5
    entity_type: node
    bundle: test_type
    label: 'TEST DATAFIELD 5'
    description: ''
    required: false
    translatable: false
    default_value: {  }
    default_value_callback: ''
    settings:
      field_settings:
        text_1:
          label: Text_1
          list: false
          allowed_values: ''
          required: false
        ref_1:
          label: Ref_1
          required: false
          entity_reference_type: taxonomy_term
          target_bundles: islandora_models
        text_2:
          label: Text_2
          list: false
          allowed_values: ''
          required: false
    field_type: data_field
    
  • 🇨🇿Czech Republic xadler1

    This seems strange to me to be a feature, maybe my example/explanation wasn't the best.

    Simplest example is a datafield with entity reference and a text field (in this order). When the reference field is left empty and the text field is filled, the data is correctly saved. However, on next edit, this line is missing from the form, and after saving the data is overwritten.

    A table for better ilustration of original example:

    Datafield config:

    langcode: en
    status: true
    dependencies:
      config:
        - field.storage.node.field_test_datafield_5
        - node.type.test_type
      module:
        - datafield
    id: node.test_type.field_test_datafield_5
    field_name: field_test_datafield_5
    entity_type: node
    bundle: test_type
    label: 'TEST DATAFIELD 5'
    description: ''
    required: false
    translatable: false
    default_value: {  }
    default_value_callback: ''
    settings:
      field_settings:
        text_1:
          label: Text_1
          list: false
          allowed_values: ''
          required: false
        ref_1:
          label: Ref_1
          required: false
          entity_reference_type: taxonomy_term
          target_bundles: islandora_models
        text_2:
          label: Text_2
          list: false
          allowed_values: ''
          required: false
    field_type: data_field
    

    PS: sorry for the later reply, I thought I posted the comment, but apparently that was not the case.

  • 🇨🇿Czech Republic xadler1

    I think I've found the issue. Method isEmpty()
    https://git.drupalcode.org/project/datafield/-/blob/2.x/src/Plugin/Field/FieldType/DataFieldItem.php?ref_type=heads#L650
    returns early when checking entity reference and file fields. Altering the if block to only return false when not empty fixes the issue.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇫🇷France lazzyvn paris

    Check dev version. it fixed

  • 🇨🇿Czech Republic xadler1

    Thank you, the problem is mostly fixed, however now the emptiness check for entity references is broken.
    Changing the condition on line 669 to include objects fixes it.

    ...
    if (is_array($value) || is_object($value)) {
    ...
    
  • 🇨🇿Czech Republic xadler1

    Thank you, the problem is mostly fixed, however now the emptiness check for entity references is broken.
    Changing the condition on line 669 to also include entities fixes the problem.

  • 🇨🇿Czech Republic xadler1

    Thank you and sorry for the delay. Now it works correctly.

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

Production build 0.71.5 2024