How can I add autocomplete functionality to a text_format field? I want to do this because I'm trying to create a field which has both autocomplete functionality (auto-suggesting certain configured entities) and an Input Filter on it to manipulate the same entities.

I have a custom widget for the field and have tried adding #autocomplete_route_name and #autocomplete_route_parameters to the formElement(), but since the text_format element is a RenderElement, not a FormElement, it seems like autocomplete cannot be added in this way. I've also looked into Autocomplete Deluxe, which doesn't work for Text Format fields.

The code below works only for textarea, since the autocomplete states don't work with text_format, but I include it to show what I've tried.

 * Plugin implementation of the 'text_textarea' widget.
 * @FieldWidget(
 *   id = "text_textarea",
 *   label = @Translation("Text area (multiple rows)"),
 *   field_types = {
 *     "text_long"
 *   }
 * )
class TextareaAutoWidget 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'] = 'textarea';
    $element['#type'] = 'text_format';
    $element['#format'] = $items[$delta]->format;
    $element['#base_type'] = $main_widget['value']['#type'];
    $element['#autocomplete_route_name'] = 'textarea_autocomplete.autocomplete';
    $element['#autocomplete_route_parameters'] = array('clue' => $this->getSetting('clue'));
    $element['#process'] = [
      [Textarea::class, 'processAutocomplete'],
    $element['#attached'] = [
      'library' => [
    return ['value' => $element];

  // ...


Your Answer

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

Browse other questions tagged or ask your own question.