Provide array of primitive property values on multivalue field

Created on 24 February 2020, over 4 years ago
Updated 24 April 2024, 7 months ago

Problem/Motivation

I'm currently missing a token that allows me to feed all the target_ids of an entity reference field to a views configuration using EVA β†’ .
Having such a token would greatly improve the reach of the token argument handler of EVA.

Proposed resolution

There's already the token handling as well as delta processing for multivalue fields.
I'd suggest to add a new "delta" called array which will use the array handling to provide access to all primitive property types of a field.
A token would look like this: [node:multivalued_field_image:array:target_id:join:+], this would provide 1+2 as value.

Attached patch should provide exactly that.

Remaining tasks

Feedback needed.
Reviews needed.
I suspect the replacement logic could be streamlined, but that makes only sense if the general approach & idea is accepted.

User interface changes

None.

API changes

None. There shouldn't be any sideeffects for existing tokens as it is a separated non numeric keyword that's needed to trigger access the new functionality.

Data model changes

None.

Release notes snippet

Discussion about token structure:

Taxonomy terms and paths for example already implement a join: functionality: [term:parents:join:?].
This part of the issue summary should be the result of our discussion below. The logic should be

No explicit modifier

[node:multivalued_field:*:{Everything like behind delta}]
How to join these value without an explicit modifier like below given?

:join:delimiter

Further modifiers to discuss

:count

Returns the number of elements in the multi value field (max delta?)
Not existing yet on multi value fields.
Suggestions:
a) [node:multivalued_field:count]
b) [node:multivalued_field:*:count]

:first

Returns the first multi value field value.
Equal to delta=0, but should be implemented to be complete because :last exists

Not existing yet on multi value fields.
Suggestions:
a) [node:multivalued_field:first:{Everything like behind delta}]
b) [node:multivalued_field:*:first:{Everything like behind delta}]

:last

Returns the last multi value field value.
Important because we can't know the max delta on a multi-value field.

Not existing yet on multi value fields.
Suggestions:
a) [node:multivalued_field:last:{Everything like behind delta}]
b) [node:multivalued_field:*:last:{Everything like behind delta}]

:reversed

Returns the results in inverse logical order.
Not existing yet on multi value fields.

This might be quite complicated to understand, so perhaps we should better think about an inversed "*" like for example "-*", "*-" or simething similar which inverts the field value order before any values are handled.
a) [node:multivalued_field:-*:{Everything like behind delta}] **suggested**
b) [node:multivalued_field:*:{Everything like behind delta}:reversed]

:join-path

Like :join but with path cleanup and "/" as separator

:keys

Unsure if this makes any sense.
For entity reference fields the result could be similar to [node:multivalued_field:*:target_id] but for other fields like multiple text fields it seems unclear. Better leave out?

Not existing yet on multi value fields.

Suggestions:
a) [node:multivalued_field:keys:{Everything like behind delta}]
b) [node:multivalued_field:*:keys:{Everything like behind delta}]

:values

Unsure if this makes any sense.
For entity reference fields the result could be similar to [node:multivalued_field:*] but for other fields like multiple text fields it seems unclear. Better leave out?

Not existing yet on multi value fields.

Suggestions:
a) [node:multivalued_field:values:{Everything like behind delta}]
b) [node:multivalued_field:*:values:{Everything like behind delta}]

Concrete examples:

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland das-peter

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    This still needs works because of the tests. I needed this too, but I actually needed being able to customize the join ΒΏseparator?

    With this patch
    [node:multivalued_field_image:array:target_id:join:+] would return 1+2
    [node:multivalued_field_image:array:target_id] would return 1,2

    This definitely needs a ton of tests though. I'm marking it as needs review just to see if current tests are still passing with this.

  • Status changed to Needs work almost 2 years ago
  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    At least I didn't broke anything. Back to needs work.

    @Berdir @Anybody Can we work on defining the minimum acceptable test cases that would need to be for this to be accepted?
    I'm willing to work on those.

  • πŸ‡ΊπŸ‡ΈUnited States lisagodare@gmail.com

    I also needed the ability to customize the join separator, but #28 didn't work for me. I modified it just a little bit.

  • Status changed to Needs review over 1 year ago
  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    Marked as NR to check if tests still pass.
    @lisagodare@gmail.com Could you describe your scenario? If last patch wasn't working for you, maybe is something to consider when we write the tests.

  • πŸ‡ΊπŸ‡ΈUnited States lisagodare@gmail.com

    Sure thing!
    It's sort of complex, but in the end I'm using tokens + metatag + pivot (multivalue in metatag), and needed to not use the default comma as separator, because that breaks on text that has commas in it.
    Using the patch in this issue for a custom delimiter in Metatag, which I have set to `||`:
    https://www.drupal.org/node/3067803 β†’
    And using patch in #28, my field token looks something like:
    [node:multivalued_entity_reference_field:array:entity:field_text:join:||]

    However, this resulted in always using a comma for the separator, until the changes I made in #30.

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    Thanks for the answer! Yeah, my scenario was similar with schema_metatag, i had a similar token pattern that you do and in my case I needed ✨ Improve support for multi-value schema using Metatag's custom separator option Fixed , so I might overlooked yours. Thanks for documenting it!

  • πŸ‡ΊπŸ‡ΈUnited States carsoncho Kansas City, MO

    This patch does not appear to work with. I'm attempting to get a multivalue term reference field to output the term names in a list with this [webform_submission:values:multivalue_term_reference:array:entity:name] and it's being output as "Term 1 (1)entityTerm 2 (2)".

    I am doing this via custom code with a custom WebformHandler that's calling on the webform submission handler's token service.

    Any thoughts?

  • First commit to issue fork.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MariaDB 10.3.22
    last update about 1 year ago
    77 pass
  • For me, patch #30 doesn't work for image-style tokens like [node:field_image:array:image_style] or [node:field_image:array:entity:image_style]
    Patch #36 fixes works for [node:field_image:array:image_style]

  • First commit to issue fork.
Production build 0.71.5 2024