function Ckeditor5Hooks::fieldWidgetSingleElementFormAlter

Implements hook_field_widget_single_element_form_alter().

Attributes

#[Hook('field_widget_single_element_form_alter')]

File

core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php, line 381

Class

Ckeditor5Hooks
Hook implementations for ckeditor5.

Namespace

Drupal\ckeditor5\Hook

Code

public function fieldWidgetSingleElementFormAlter(&$element, FormStateInterface $form_state, $context) : void {
  // Add an attribute so that CKEditor 5 plugins can vary their behavior based
  // on host entity type, host entity bundle and host entity language.
  if (!empty($element['#type']) && $element['#type'] == 'text_format') {
    $items = $context['items'];
    assert($items instanceof FieldItemListInterface);
    $host_entity = $items->getEntity();
    $element['#attributes']['data-ckeditor5-host-entity-type'] = $host_entity->getEntityTypeId();
    $element['#attributes']['data-ckeditor5-host-entity-bundle'] = $host_entity->bundle();
    $element['#attributes']['data-ckeditor5-host-entity-langcode'] = $host_entity->language()
      ->getId();
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.