Problem/Motivation

You can get a fuller picture of what I'm trying to do on my other issue, but this is a separate concern. I'd still recommend scanning/answering that one first before answering this one.

I'd like to add multiple entries to a Entity Reference Field that is using the Autocomplete widget/format. The other issue is where I'm hoping to get the best ECA-supported answer, but I also tried using the unlimited_field_settings module, and it does let me load the /node/add page with multiple skills fields showing for the entity reference field (ie, the page loads and shows 10 rows or however many you set)
Thus the page will have entity reference fields with the names: field_related_skills[0][target_id], field_related_skills[1][target_id], field_related_skills[2][target_id], etc.

When I'm iterating through an ECA loop, I can't seem to be able to use a token to put in the field name in the Form field: set value task so that as I'm looping through a list, it's updating the field name for each item. Is there a way to turn off token support for the Field Name field so that or the value of a token so that it just gets sent to the Field Name field as plain text?

Example setup:
I create an iteration token to track my progress through the loops: loop_counter_token
I create a token to represent the field name: field_name_token
I create a token called "target_id" to hold the "target_id" text because that will get interpreted as a token in the token value field.
What I want to be seeing is: field_related_skills[0][target_id]

The field_name_token's value is: field_related_skills[[loop_counter_token]][target_id]
The problem is, that when I do that, the result that the Form field: set value task gets is: field_related_skills[0]
So I end up writing something like: field_related_skills[[loop_counter_token]][[[target_id]]] in the field_name_token's value to get the brackets in there, but that's not interpreted correctly by the Set Field Value task either. I think it ends up still seeing: field_related_skills[0]target_id

Bottom line is that because I don't have a way to turn off tokenization, the field name gets butchered because there's a token in there and once it sees that, everything gets treated as a token.

Any ideas would be appreciated. I've tried adding more brackets and looking up if there's some sort of escape character I could use, but nothing's getting me where I want.

Note that I can put in the one field name, like field_related_skills[0][target_id], and this works, but it doesn't allow me to iterate through a series of fields.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork eca-3565516

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rex.barkdoll created an issue. See original summary.

jurgenhaas’s picture

As the referenced issue had a concept that doesn't apply as field widgets are not relevant for ECA operating on the backend. this issue is probably not relevant any longer either?

rex.barkdoll’s picture

Category: Support request » Bug report

Ok, so I'm back here with additional testing.

In the other ticket, you suggested targeting the deltas of a field to insert values. My understanding is that you would do this my setting the field value using this method:

Field Name: field_related_skills[0][target_id]
Field Value: Skill Name (45)

This works. However, when I try to use a token that's counting the iterations through a loop, it doesn't work:

Field Name: field_related_skills[[iteration_token]][target_id]
Field Value: Skill Name (45)

I think this is an ECA bug.

I've tried a number of solutions to understand the problem, but am not a knowledgeable enough coder to understand where the exact breakdown is happening.

Here's what I've tried:
OPTION A:
field_related_skills[[iteration_token]][target_id] results in field_related_skills[1]

OPTION B: Add extra brackets to fix target_id being cut out
field_related_skills[[iteration_token]][[target_id]] results in field_related_skills[1][]

OPTION C: Creating tokens to fix [target_id] being interpreted as a variable:
Token LB value: [
Token RB value: ]
Token target_id value: [LB]target_id[RB]
field_related_skills[[rel_skill_iteration_int]][target_id]
This does actually print out the correct thing in a Message field: field_related_skills[2][target_id], but it still doesn't put the value in the field. I've tested this by having a split path where the first iteration through a loop, I put in the field directly field_related_skills[0][target_id], but then for the rest of the loops, I try using the iteration_token: field_related_skills[[iteration_token]][target_id] The first one works but any that use a token for the Delta don't.

The only option is to create a conditional loop path for every field assignment and set the field name manually.
Example:
field_related_skills[0][target_id]
field_related_skills[1][target_id]
field_related_skills[2][target_id]
...
field_related_skills[99][target_id]

Inserting Values in this manner does not work:

Field Name: field_related_skills[1]
Field Value: target_id: 45
Interpret as YAML (YES or NO)

I also tried assigning the value with Entity: Set field value, but to no avail.
Targeting: field_related_skills.0.target_id or field_related_skills.0 or field_related_skills had no effect.

I've also made sure to include a Form State: Rebuild in the loop to help reveal any changes.

jurgenhaas’s picture

Category: Bug report » Support request

Instead of using field_related_skills[0][target_id] you can use field_related_skills.0.target_id. That way, you use periods as separators instead of brackets that get confused with tokens.

Then, you can use a token for the delta: field_related_skills.[delta].target_id

rex.barkdoll’s picture

This works as long as you're putting in field_related_skills.0.target_id, but not when using field_related_skills.[delta].target_id

I think there's a bug where the field name is getting sent in before the token is swapped out. I've tested with both my iterator token as well as a token set to the number 0 and neither works.

jurgenhaas’s picture

StatusFileSize
new1.25 KB

It does work for me with the attached model. It contains a tags field with a cardinality of 4 and sets the default value in the edit form as expected.

The only exception is that the "Autocomplete (Tag style)" widget doesn't respect the default value, but all others (Autocomplete, select, checkboces) work just fine.

rex.barkdoll’s picture

Thank you for the example and the patience while I did more testing,
Some important lessons learned:

#1: Your example works perfectly when there isn't an Ajax handler involved. I'm setting my values after an Ajax call which apparently makes things a little harder. I need the Ajax call to detect when an Entity Reference field changes value, I then grab the chosen value and load an entity from that selection, which is then used to populate the node form with values from its fields.

#2: You used Form field: set default value, which works great when not waiting for after the Ajax call has occurred. In fact, it makes it really simple to set a field value with the ID number, but it doesn't seem to work if it has to wait for an Ajax event and subsequent actions to fire.

Form field: set value does work after an ajax call, but it doesn't load entities into fields as nicely as Form field: set default value does. You have to do the formatting so that your user sees "Entity Name (ID)" in the field.

Trying to use a delta variable in a field name after an ajax call apparently doesn't work. I'm attaching a recipe, I'm hoping this will work. I added an entity reference field that lets a user select an article to my article content type. The selection of that field triggers the Ajax handler and then the tags from the selected entity are used to fill in a field on the current form.

You should see that C: Form Field: set value works only when you don't use the [delta2] variable. Replace the variable with the number 2 in the delta spot and it works.

I'm hoping this can help you see what I'm experiencing.

jurgenhaas’s picture

Version: 3.0.9 » 3.0.x-dev
Status: Active » Needs review

Looks like we introduced a bug in the set field value action in this issue: #3519886: ECA Form: provide a way to set the form field value when submitted via Ajax last year in May. It receives the form element before replacing tokens in the field name. I'm just changing the order in the MR here, please give that a try.

rex.barkdoll’s picture

Awesome! It's working!
I think this gets me past the roadblock. :)

Thank you!

  • jurgenhaas committed 4fd8ef6d on 3.0.x
    bug: #3565516 How to iterate through fields - how to turn off token...

  • jurgenhaas committed 4ad7a599 on 2.1.x
    bug: #3565516 How to iterate through fields - how to turn off token...
jurgenhaas’s picture

Status: Needs review » Fixed

That's great, thanks for reporting and testing @rex.barkdoll, so we eliminated one of those nasty bugs. Merged this and also back ported to ECA 2.1

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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