Prepopulate data in entity reference autocomplete form fields

Created on 25 June 2024, 6 months ago
Updated 26 June 2024, 6 months ago

Problem/Motivation

I tried to use ECA to replace Prepopulate module but I can't prepopulate data in Autocomplete form for Taxonomy. (Prepopulate module does work.)

What I do is I want to fill in the data from URL, in case that data is not already available in taxonomy, I can just save and create new Term Name. However, when I prepopulated with ECA, all taxonomy form field is blank. The only way I can make it show is when I use Term ID.

Steps to reproduce

Use step here : https://www.drupal.org/project/eca/issues/3309340
- Build Form
- Then, "Form Field: Set Default Value"
- Field name : field_item
- Value : [current-page:query:item]

ECA module :

1. When use URL -> https://xxx.com/node/add/item?item=NewTerm
That Form field is blank.

2. When use URL -> https://xxx.com/node/add/item?item=5
If TermID 5 already there in the Taxonomy list, then, the data will show "Term (5)", if not, then, blank

Prepopulate module :

- This URL -> https://xxx.com/node/add/item?edit[field_item][widget][0][target_id]=New...
That Form field will show "NewTerm"

Feature request
Status

Active

Version

2.1

Component

Code

Created by

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

Comments & Activities

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

    Well, I think the answer is already given in the question itself: the entity reference field requires the entity ID of which it needs to refer to.

    That said, your model should be loading or creating the entity from the given ?tem=NewTerm and then get the ID from that entity to use it for setting the field value.

    Any reason why that wouldn't work?

  • What I would like to do it to pre-fill the form using a data from URL, in which, it may need to change or may not save at all.

    If I created Term and get Term ID for that, I might end up with unneeded data. That's why I set it as "Create referenced entities if they don't already exist" so that new term only created when save data and there is no such term in the system.

    Thanks jurgenhaas,

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Yeah, I can see in the ECA code that this is on purpose:

                if ($value instanceof EntityInterface) {
                  $entities[] = $value;
                }
                elseif (is_scalar($value) && isset($element['#target_type']) && ($entity = $this->entityTypeManager->getStorage($element['#target_type'])->load($value))) {
                  $entities[] = $entity;
                }
    

    This means, if the given value for the entity reference autocomplete field is scalar (a string), it tries to load that entity and if available, it adds it as the field value.

    The prepopulate module in contrast uses the given value as the default value if it can't load an existing entity from it.

    That behaviour from the prepopulate module could be adopted here as well, but only if the field configuration is set to "Create referenced entities if they don't already exist" being enabled. Otherwise, this would be a false behaviour, I think.

    I've updated the issue meta data accordingly.

  • Yes, If "Create referenced entities if they don't already exist" is not enabled, then, data should be there in the system. So, load entity to check for "Term ID" should be done in that case.

    Thanks for your help, jurgenhaas.

Production build 0.71.5 2024