Handling Multi-Value Nested Fields

Created on 6 February 2023, over 1 year ago
Updated 26 February 2023, over 1 year ago

I had quite some troubles with multi-value nested fields (from the double field or datafield modules). Got some info from https://www.drupal.org/project/eca/issues/3309212 πŸ’¬ How to set a feed form nested property value with ECA Fixed but didnt work in my case (field_stats_compact is the double field).

I could only set such field values directly with (eg with [node:field_stats_compact:0:first]), but could not set the field as a whole (e.g. with YAML). Getting the field value, setting it as token and saving this as entity field did not work for me either. Getting the field value and displaying it with [node:field_stats_compact] or [node:field_stats_compact:0] did not work, only [node:field_stats_compact:0:first] showed the value of the subfield.

in particular, appending a new value to the double field (e.g. player1 and 90 points) did not work correctly. when appending it always added two new rows (one for player 1 and one for 90 points instead of one row with both values). I also tried adding a separate saving action (and did not save directly in the setting entity value action). same result

The only way i could append nested multi-value fields was

1) to count the items of the double field (field_stats_compact) first (to get the last delta/amount of values already stored in the double field)
2) store the value as "list_counter" and
3) added two "set field value" actions for each of the two values (first and second) to set the subfields [node:field_stats_compact:[list_counter]:first]
and [node:field_stats_compact:[list_counter]:second] with "set and clear previous value" method.

UPDATE:
One needs to be really careful saving the entity as this can cause an infinite loop. Hope someone can post a better way to handle/append multi value, nested fields properly.

πŸ’¬ Support request
Status

Closed: works as designed

Version

1.1

Component

Documentation

Created by

πŸ‡¦πŸ‡ΉAustria coreteamvn

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

Comments & Activities

  • Issue created by @coreteamvn
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    This one is really difficult to provide support for. It is not exactly clear what this means:

    multi-value nested fields (from the double field or datafield modules)

    Multi-value field seems to be clear, but "nested" is confusing. Also "double field" could either be a field type containing doubles, i.e. numbers with a decimal. But I guess it means more like a field type that contains multiple properties? And last but not least, I'm not sure what you mean by "datafield modules".

    To be helpful in some way, it would be important to get a simple working example, where we can try to reproduce and maybe fix the issue. For that, the most simple model that lets you reproduce the problem should be uploaded. That may require some extra modules or even some custom code which defines the field type. If so, please upload that as well. Then we can have a look.

  • πŸ‡¦πŸ‡ΉAustria coreteamvn

    Hi JΓΌrgen!

    Sorry for not being clear enough.

    Nested: The doublefield β†’ module and datafield β†’ modules provide for a composite structure. Ive been using double field with d9 and it works very well and is a stable module (also has good views integration).

    The modules allow two (double field) or more (datafield) values (text, int, decimal, entity reference,...) to be stored in one field. For instance, one can store a name and some score in one field. In that sense it is similar to the above mentioned thread where one can use YAML to set a structure like:

    0:
    first: "name of player"
    second: 12

    I cannot get this to work. Ive been trying the datafield and doublefield module and the result is the same. I can only set the values seperatly in two actions (first and second) where i target a specific delta, but not in one action. Setting value separatly is fine when one can target/set a specific delta (this works perfectly already), but when one wants to add a double field, this approach does not work.

    Attached find a simple model and a screenshot of the properties of the node and the double field in particular. It requires the installation of a double field module (e.g. text and decimal as first and second subfield) attached to a content type. When one updates node 287, node 286 with the double field is loaded and two new values should be added. however, the actions (before) getField value are not executed and a few checkpoint display messages are not firing. From the error log:

    Failed execution of getValue (Activity_0ir16h6) from ECA Testing DoubleField Additions (process_dbpnxw7) for event Drupal\eca_content\Event\ContentEntityUpdate: Property value is unknown
    
    Failed execution of getDelta0 (Activity_1suu54n) from ECA Testing DoubleField Additions (process_dbpnxw7) for event Drupal\eca_content\Event\ContentEntityUpdate: Property value is unknown

    I hope this is useful for you. Please let me know if you need something else.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Looks like this is a bug in the double_field module. Their field type defines 2 properties: first and second. But they don't define the name of the main property which results in value being used as the default. That said, Drupal now checks on the property value which doesn't exist and hence the ECA model has no access to write to that field.

    The solution is simple: double field needs to implement the following method into their \Drupal\double_field\Plugin\Field\FieldType\DoubleField class:

      public static function mainPropertyName() {
        return 'first';
      }
    

    Then it works just as expected.

    However, the model would still not work, because the field value needs to be yaml and it needs to be using the correct property names. I've build another test model for this, which I think is really a simple one and looks like this:

    The model is attached.

    Please report the bug to the double_field issue queue - and maybe also to the datafield issue queue as they may have the same issue.

  • πŸ‡¦πŸ‡ΉAustria coreteamvn

    Thanks for the info!

    I have posted your suggestion in their forum and added the static function in double field FieldType file.

    Getting values and display them work now, but the problem with setting/appending is that it creates a new row for each entry. Rather then appending one row with "player" and "points", it adds two rows: one row with "player value" and empty "points" and one with empty "player" and with "points" value. See attached emptyRow screenshot. I have set "save entity" to "no" in the setField action and added a separate "save action" for the entity. The result is unfortunately the same.

    The only way i could append entries was counting the existing delta and set and clear this delta directly (see above). however, this approach essentially crashed the drupal installation (the error log showed attempts to set/add field values even after deleting the model). Im not sure if there was another update node model running that actually caused this.

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Oh, I see. Yes, it really adds two rows instead of adding one row with 2 values.

    I've just checked the code and have to confirm, this is how it currently works and there is no quick fix to that, I'm afraid.

    Maybe you could resolve it like this:

    • Append a new row with just the player name
    • Then get the index of the last row
    • Set the property for the second value as field_stats_com.#.second and save the entity

    That's probably the best you can currently do.

  • πŸ‡¦πŸ‡ΉAustria coreteamvn

    Thanks for the info, JΓΌrgen!

    Will do it like that.

  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria coreteamvn

    update regarding double_field πŸ“Œ Workflow/ECA Module: Definition main property missing Fixed :

    Added mainPropertyName method that returns NULL as documentation suggests.

Production build 0.69.0 2024