You are here

function entity_embed_field_widget_form_alter in Entity Embed 8

Implements hook_field_widget_form_alter().

File

./entity_embed.module, line 285
Framework for allowing entities to be embedded in CKEditor.

Code

function entity_embed_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  // Add a `data-entity_embed-host-entity-langcode` attribute so that
  // entity_embed's JavaScript can pass the host entity's language to
  // EntityEmbedDialog, allowing it to present entities in the same language.
  if (!empty($element['#type']) && $element['#type'] == 'text_format') {
    if (!empty($context['items']) && $context['items'] instanceof FieldItemListInterface) {
      $element['#attributes']['data-entity_embed-host-entity-langcode'] = $context['items']
        ->getLangcode();
    }
    else {
      $entity = $form_state
        ->getFormObject()
        ->getEntity();
      if ($entity instanceof ContentEntityInterface) {
        $element['#attributes']['data-entity_embed-host-entity-langcode'] = $entity
          ->language()
          ->getId();
      }
    }
  }
}