2

Hide the term id on autocomplete widget

How can I hide the term id on autocomplete widget ?

I use drupal 8.

3

There is no built-in option to hide term ID from entity_autocomple form element. Its behavior is simple, if there is a term that match then shows (TID).

1

Here is the trick to hide Reference EntityID using Javascript/jQuery. this is not a proper way to hide it, but it works.

jQuery("body").find('.form-autocomplete').change(function() { var val = jQuery(this).val(); var match = val.match(/\((.*?)\)$/); if (match) { jQuery(this).data('real-value', val); jQuery(this).val(val.replace(' ' + match[0], '')); } });

1

For Drupal 8 I would adjust your code a bit to remove the number after selecting the suggested value, not after user click outside the input:

    jQuery("body").find('.form-autocomplete').on('autocompleteclose', function(event, node) {
  var val = jQuery(this).val();
  var match = val.match(/\((.*?)\)$/);
  if (match) {
    jQuery(this).data('real-value', val);
    jQuery(this).val(val.replace(' ' + match[0], ''));
  }
});
0

Similar to other jQuery-related answers, you could add an function and call it for each form field or an entire form:

    function autocomplete_tid_remove(field){

        var autocomplete_tid = $(field);

        var autocomplete_tid_stripped = $(autocomplete_tid).val().split(" ")[0];    

        $(field).val(autocomplete_tid_stripped);    

    }

    var selector = "[**put form id here**]";

    autocomplete_tid_remove(selector + " .form-autocomplete");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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