Problem/Motivation
Multiple use cases will require to lookup for an entity with a default lookup value, for instance, importing content from other systems not always have an author to map in the importing system.
When entity_lookup don't found the entity NULL is given and no entity is set.
Actually, the best option is to use default_value plugin, but this requires them to fix the id in the configuration, but as long as the 'default entity' is content it can have different id in different site installations and also makes the configuration less readable.
Proposed resolution
Implement a default entity lookup plugin where instead of the fallback to fixed value fallbacks to a entity field value to lookup for the entity.
Comments
Comment #2
quironThis patch is working for me.
Comment #3
quironAdded checking of the user already exists on the system.
Comment #4
heddnSome thoughts. First great work here. Generally useful plugin. I wonder if we could make it even more useful? Also, we should add some tests. See other examples in this module. Since you are interacting with entities, it will probably need to be a kernel test.-
Setting to NW for tests and the suggestion to make this more generally useful.
Can we extend DefaultValue and pass things off to it after loading the uid?
Actually, what if we made this a 'default_value_plus' plugin? And provided an entity type, lookup_value, lookup_key, etc so we can then use an entity query to look for a default value based on any query-able condition.
Example:
entity_type: user
lookup_key: username
lookup_value: "john smith"
default_value_key: uid
The above names are just examples. If you have better suggestions on naming, feel free to use them instead.
Comment #5
quironAfter work on it IMHO the better approach to get the
DefaultValuePlusis directly extendEntityLookupand keep it's behavior and configuration keys. This way the plugin implementation keeps simple and full-featured.@heddn agree with this approach? If yes I will start with tests. There is an example of similar tests for it I can take as reference?
Comment #7
quironThis is a small syntax fix.
@heddn if you can point me how to work on the test I would start with it.
Comment #8
quiron@heddn Added new version of the patch with the default_value_plus plugin + a proper Kernel test for it. It checks both the case a value is provided and no value is provided and the default is used. Test case with users, like the Kernel test for EntityLookup.
Comment #9
quironComment #11
quironFixed the codesniffer issues.
Comment #12
heddnNaming is hard. Would default_entity_value / DefaultEntityValue make sense more to convey a more direct meaning?
Comment #13
quironI was also wondering about the naming. I guess that default_entity_reference is also a good candidate, but there is no similar name in the actual migration universe. So ok to go with default_entity_value. I kept the configuration option name to default_value as long IMHO makes sense to not add the 'entity' keyword because it stands to a field value, not the whole entity.
Comment #14
quironUpdated title and issue description
Comment #16
heddnThanks for your contributions!