2

I have entity reference autocomplete field (which is term reference field )

users are not allowed to create new terms while node creation.

enter image description here

The validation error shows like below on form submit :

enter image description here

There are no entities matching "newterm".

I need to override this message with something like "No new terms are allowed please select from existing terms"

When I tried to find code from where it is working it looks from this :

core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php and function where message is there : https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Element%21EntityAutocomplete.php/function/EntityAutocomplete%3A%3AmatchEntityByTitle/8.0.x

Please suggest how I can override this. I tried searching lot but was not able to find right direction and have tried overriding status message as well but sure how to proceed with this.

Thanks!

2
  • As Much I know, autocomplete entity reference fields can't create a new entity. The error message which you are getting says that the entity reference term which you are trying to add doesn't exists. If you want your users to select from predefined terms, I would suggest to make this field either a select dropdown or radio button field widget.
    – Bharat
    Commented Mar 27, 2018 at 8:45
  • There are 40,000 plus terms so cant set it radio / checkbox or select-box. I did similar things in 7 ie: to override error message. I think there must be some way in 8 also which I am not aware of. In form submit hook i got $error_messages = drupal_get_messages('error'); and then alter or add etc but I am not sure about 8 method ie: correct approach to handle such things.
    – jas
    Commented Mar 27, 2018 at 8:47

1 Answer 1

2

In HOOK_form_FORM_ID_alter() add #element_validate attribute to the form element

$form['field_your_field_name']['#element_validate']

Reference: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x#element_validate

0

Your Answer

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

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