I hope that maybe someone had a similar use case and has an idea how to solve it: I have content type that has an entity reference field to a vocabulary. With that I would like to achieve it to not only reference to the term itself in the manage display (in my case a country name), but also choose a certain value of a multiple value field of that term (one of the several historic names of that country). I created a hack with an additional list field in the content type (choose historic name 1, 2, 3...), but this is no good solution because as soon as I modify the order of the field items (e.g. because of adding one), the name is displayed wrongly all over the website. So there is the need to directly reference the field value of the referenced term itself.
-
Can't you use view modes on taxonomy terms? Then have a view mode that displays that multivalue field of that term and in the node types manage display settings choose this view mode for the entity reference?– leymannxCommented Jan 3, 2020 at 20:52
-
This way I could show all values of the multiple field, but at node edit I could not choose a specific one (or none of them) to be displayed.– BenjaminCommented Jan 3, 2020 at 22:34
2 Answers
You can do this in a custom field type based on the core reference field, adding a second property and dynamically filling the options of the select list depending on the first property.
For the multi-value part of your question a non-coding solution would be a paragraph containing both fields. Adding the dynamic behavior can then be done in a form alter hook or you can try Business Rules and adjust this recipe to your requirements: https://www.drupal.org/docs/8/modules/business-rules/advanced-usage/dependent-field
-
Thank you for you quick response. It looks good to me although I am uncertain if my coding skills will be good enough to create the custom field type. The non-coding solution part looks also promissing to me. My "hacky" solution already uses 2 fields in a paragraph. My only problem will probably be, that in my case the premise 'The child field type must be "Entity reference" ' is not true. My multi value field inside the term is another paragraph. But I will give it a try nevertheless.– BenjaminCommented Jan 3, 2020 at 22:24
-
the difference between "hacky" and not is code standerds then unit / functional tested.– taggartJCommented Jan 6, 2020 at 5:10
You achieve this with a multi-level taxonomy. Your country name would be the term on the top level and then the historic names would exist as children terms. You could write some code to infer the root term based on the historic term selected. Maybe as simple as having a second field on your entity and just populating it with the historic term's parent on entity save.
-
Thank you for your good suggestion, too, although it does not work in my case, as the country term is already part of a taxonomy hierarchy for other reasons.– BenjaminCommented Jan 3, 2020 at 22:25