Multivalues from token replacement

Created on 17 September 2023, over 1 year ago

Problem/Motivation

You cannot use tokens and multivalues because the Token replacement cannot return an array of values. So, for example, the result of your token generation is an array (such as multiple values from a URL query param), the token returns always a scalar, so "Array".

Steps to reproduce

Proposed resolution

Create a new option to explode a string value in multiple before setting it onto the entity. This way, the token can return a comma-separated value that EPP can explode if so configured.

User interface changes

Add an option to the third party setting and field config.

πŸ“Œ Task
Status

Needs work

Version

1.0

Component

Code

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @Upchuk
  • Here's a quick patch that does this. So now the token can return a comma-separated list of values and EPP can explode them and set them onto the field.

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    This module uses YAML encoding. I was able to get multiple values working out of the box using something like this:

      $ids = []
      foreach ($entity->get('field_foo')->getValue() as $x) {
        $ids[] = intval($x['target_id']);
      }
      $route_options['query']['foo'] = Yaml::dump($ids);
    

    This will produce a url like foo=-%208%0A-%209%0A where 8 and 9 are the values. On the EPP side it calls new Parser())->parse($yamlValue, ...); where is transformed back into an array.

    The docs are severely lacking an example of how to do this. https://www.drupal.org/docs/contributed-modules/entity-prepopulate/entit... β†’ only gives the hint of passing a one level array.

  • πŸ‡ΈπŸ‡°Slovakia coaston

    Hi devkinetic

    It sounds good. Would you be able to provide a patch?

  • πŸ‡ΊπŸ‡ΈUnited States devkinetic

    I'm not sure exactly what you mean, there is nothing to patch on the UI side. My reply really only explains how properly construct your URLs. In the case of multivalue, you need to do something like this if your building your url from code:

    // each integer is a entity target_id on the multivalue field
    $x = [1, 2, 3];
    Yaml::dump($x);
    

    Which will produce the following YAML:

    - 1
    - 2
    - 3
    

    This can then be added as your query parameter, making the value of the token a urlencoded YAML string.

    If your building your URL manually, you can do /node/add/article?field_multivalue=-%208%0A-%209%0A where the format is dash space VALUE newline dash space VALUE.

  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    Thanks @upchuck, being able to prepopulate multiple fields and documentation on how to do it would be fantastic. Specifically, I am trying to prepopulate a single node with multiple addresses, using the Address module, filling out just the "Street address" (called "Address line 1"/address_line1 in the configuration) and "Country" fields. For example:

    • Tailor Street 23, UK
    • Baker Street 42, UK

    etc.

    It would be great with having the feature, but also how to actually target the fields, like the "0" in [node:field_location:0:address_line1] probably needs to be something else, right?

    Also, how would you configure EPP to pick up tokens under an Address field type? There is only a single shared "Entity Prepopulate" field, but multiple fields (or they are called "properties") such as "First name", "Middle name", etc.

    Thanks!

  • Merge request !12Add support for multivalues from token β†’ (Open) created by ressa
  • Pipeline finished with Failed
    about 2 months ago
    Total: 167s
    #328331
  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    I tried the patch from #2. It works well, and I can prepopulate multiple fields via an URL such as /node/add/article?Body=24&multi=first value,second value in a multivalue field.

    I re-created the patch as a Gitlab MR, in case it can help get this great feature committed.

  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    ressa β†’ changed the visibility of the branch 3387774-multivalues-from-token-semi-colon to hidden.

  • Merge request !13Add support for pipe-separated multivalues. β†’ (Open) created by ressa
  • Pipeline finished with Failed
    about 2 months ago
    Total: 77s
    #328389
  • πŸ‡©πŸ‡°Denmark ressa Copenhagen

    Values can often have commas in them, so perhaps another separator such as pipe ("|") might be better? I have created an alternative suggestion which uses pipe as separator: 3387774-multivalues-from-token-pipe-separator.

Production build 0.71.5 2024