0

There is the standard insert or edit node form with an entity_reference field and the EntityReferenceAutocomplete widget.

The client needs to save string values inputed to the widget in case of entity wasn't selected from the autocomplete list. That is, client needs to have possibility to select entity from the autocomplete list or input string value if, for example, entity is absent in the list. And new entity shouldn't be created in this case.

I'm thinking how to create this opportunity the best way. And I'm little bit confused.

Is it possible to perform it with alter hooks only and without creating new field widget ?

Or otherwise is it possible to do it creating a new field widget having inherited it from the class EntityReferenceAutocompleteWidget ?

Or maybe the single solution for it is creating a custom widget based on TextField with including autocomplete function manually ?

Has anyone encountered with a simular task ?

2
  • You want to save a string if the value is not in the autocomplete list instead of creating a new entity? In that case, you need at least two fields, one for the entity references and one for the strings... Sep 30, 2017 at 7:51
  • No, the client needs to use single edit field to input string or select from autocomplete list. I can save the string in my custom table. Sep 30, 2017 at 8:02

1 Answer 1

0

There is no need to create a custom table or a second field. While editing your entity reference field, in the "Reference type" section, you could check the "Create referenced entities if they don't already exist" option. If for instance, your field is referencing taxonomy terms, a new term for the referenced vocabulaty will be created. I hope that this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.