8 – How to add autocomplete to the text_format element?


How can I add the autocomplete functionality to a text format field? I want to do this because I am trying to create a field that has autocomplete functionality (automatically suggest certain entities configured) and an Input Filter to manipulate the same entities.

I have a custom widget for the field and I have tried to add #autocomplete_route_name and #autocomplete_route_parameters to formElement (), but since the text_format element is a RenderElement element, not a FormElement, it seems that the autocomplete can not be added in this way. I have also searched for Autocomplete Deluxe, which does not work for Text Format fields.

The following code works only for textarea, since the autocomplete states do not work with text_format, but I include it to show what I have tried.

/ **
* Widget plugin implementation & # 39; text_textarea & # 39 ;.
*
* @ FieldWidget (
* id = "text_textarea",
* label = @Translation ("Text area (multiple rows)"),
* field_types = {
* "text_long"
*}
*)
* /
TextareaAutoWidget class extends StringTextareaWidget {

// ...

public function formElement (FieldItemListInterface $ items, $ delta, array $ element, array & $ form, FormStateInterface $ form_state) {
$ main_widget = parent :: formElement ($ items, $ delta, $ element, $ form, $ form_state);

$ element = $ main_widget['value'];
// $ element['#type'] = & # 39; textarea & # 39 ;;
$ element['#type'] = & # 39; text_format & # 39 ;;
$ element['#format'] = $ items[$delta]-> format;
$ element['#base_type'] = $ main_widget['value']['#type'];
$ element['#autocomplete_route_name'] = & # 39; textarea_autocomplete.autocomplete & # 39 ;;
$ element['#autocomplete_route_parameters'] = array (& # 39; clue & # 39; => $ this-> getSetting (& # 39; clue & # 39;));
$ element['#process'] = [
      [Textarea::class, 'processAutocomplete'],
];
$ element['#attached'] = [
      'library' => [
        'textarea_autocomplete/autocomplete'
      ],
];
he came back ['value' => $element];
}

// ...

}