- 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.