8 – How to set the default format in text_format and hide the selection?

/**
 * Implements hook_form_alter().
 */
function MODULE_NAME_form_alter(
  &$form,
  DrupalCoreFormFormStateInterface $form_state,
  $form_id
) {

  if ($form_id == 'node_article_form') {
    $form("body")("widget")(0)('#allowed_formats') = ('full_html'); // Your default format
    $form("body")("widget")(0)('#after_build')() = 'MODULE_NAME_hide_format';
  }
}

function MODULE_NAME_hide_format(
  &$element,
  FormStateInterface $form_state
) {
  if (isset($element("format")("format"))) {
    $element("format")("format")("#access") = FALSE;
  }

  return $element;
}

theming: form element of type text_format, keeping the format WYSYWIG

In Drupal 8 I have a form element for type text format, and I need to render it on a twig, but keeping the same format was entered in the WYSYWIG editor. Do I need to know how to render this element in a way, keeping the format? This is done in a personalized way.

$ form['template'] = [
  '#type' => 'text_format',
  '#title' => 'My title',
  '#rows' => 100,
  '#cols' => 120,
  '#format' => 'full_html',
];

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];
}

// ...

}