Action "List: add item" ignores specified key when setting a numeric key

Created on 25 April 2025, about 1 month ago

Problem/Motivation

The action "List: add item" allows to create custom lists and provides an option "Set by specified key".
Such a custom list is mostly then a DataTransferObject created on runtime by ECA.

When setting a numeric key, then the DataTransferObject will apply a rekey:
https://git.drupalcode.org/project/eca/-/blob/2.1.x/src/Plugin/DataType/...

That leads to the problem that a specified numeric key will be dropped and replaced by a numeric sequence order of the list. So whenever the (numeric) key is important, for example when setting an options list in a form, then this mechanism to use a custom list for that case becomes unusable.

It may even be worse when not knowing about this fact, that wrong keys are being used within a process.

Steps to reproduce

Proposed resolution

Somehow we need to prevent the rekey logic when setting a specified key.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇩🇪Germany mxh Offenburg

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

Merge Requests

Comments & Activities

  • Issue created by @mxh
  • 🇩🇪Germany jurgenhaas Gottmadingen

    This is a challenging one. Should we probably introduce a flag in \Drupal\eca\Plugin\DataType\DataTransferObject to optionally turn off rekeying which should be called right before calling $list->set($index, $item); in the action plugin?

  • 🇩🇪Germany mxh Offenburg

    Should we probably introduce a flag in \Drupal\eca\Plugin\DataType\DataTransferObject to optionally turn off rekeying which should be called right before calling $list->set($index, $item); in the action plugin?

    Either this, or initialize a DTO with a rekey option, or do both. Maybe then let the user decide whether rekey should happen, by adding a corresponding option to the "List: add item" action? There is another place where someone can add an item to a list, for example with "Token: set value" using token name [mylist:+]. But I think that one is probably never used by anyone. Still there may be other places where an item is being added to a list (other actions, such as merging).

  • 🇩🇪Germany jurgenhaas Gottmadingen

    For now, I've only implemented the explicit disabling of rekeying for a DTO in the context of this action plugin. Reason being that we don't know where the DTO is being initialized, that could be almost everywhere. And using a special token syntax to disable rekeying is something that won't be used, as @mxh already stated.

  • Pipeline finished with Success
    17 days ago
    Total: 443s
    #502961
  • 🇩🇪Germany mxh Offenburg

    Thanks for addressing this. Took a brief look and left one note.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Addressed that in another commit and commented in the MR thread.

  • 🇩🇪Germany mxh Offenburg

    Another suggestion, since we already check for instanceof DTO: We could add a helper method to the DTO "setWithoutRekey" or "setWithRekeyOption" which takes care of properly setting the rekey state back-and-forth.

    public function setWithRekeyOption($property_name, $value, $notify = TRUE, $disableRekey = FALSE) {
      $initial = $this->disableRekey;
      $this->disableRekey = $disableRekey;
      $this->set(...);
      $this->disableRekey = $initial;
    }
    
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Another suggestion, since we already check for instanceof DTO: We could add a helper method to the DTO "setWithoutRekey" or "setWithRekeyOption" which takes care of properly setting the rekey state back-and-forth.

    I wouldn't do that, because we're not calling writePropertyValue either, we're calling the set method in the interface which may do more things than just calling writePropertyValue. I think what we now have should be safe.

  • Pipeline finished with Failed
    17 days ago
    Total: 467s
    #503006
  • Pipeline finished with Success
    17 days ago
    Total: 380s
    #503017
  • 🇩🇪Germany mxh Offenburg

    Not sure what you mean in #9 - I think calling a helper method once would be a bit more readable code rather than back-and-forth flipping of the boolean variable from the outside. Nevermind, the current state of the MR now looks also to be a working solution.

  • 🇩🇪Germany mxh Offenburg

    Setting to RTBC since tests are green and code looks like a working solution.

  • Pipeline finished with Skipped
    17 days ago
    #503047
    • jurgenhaas committed 4834ad40 on 3.0.x
      Issue #3521175 by jurgenhaas, mxh: Action "List: add item" ignores...
    • jurgenhaas committed 472f4eea on 2.1.x
      Issue #3521175 by jurgenhaas, mxh: Action "List: add item" ignores...
    • jurgenhaas committed a1624e95 on 2.0.x
      Issue #3521175 by jurgenhaas, mxh: Action "List: add item" ignores...
    • jurgenhaas committed b7f987d5 on 1.1.x
      Issue #3521175 by jurgenhaas, mxh: Action "List: add item" ignores...
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Merged and back ported to 2.1.x, 2.0.x, and 1.1.x

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

Production build 0.71.5 2024