Commit c0248621 authored by Domen Šlogar's avatar Domen Šlogar Committed by Dave Reid
Browse files

Issue #3269454 by Dave Reid, slogar32: Replaced deprecated...

Issue #3269454 by Dave Reid, slogar32: Replaced deprecated hook_field_widget_form_alter() with hook_field_widget_single_element_form_alter() and simplified logic for entity language.
parent 27853bcb
Loading
Loading
Loading
Loading
+6 −12
Original line number Diff line number Diff line
@@ -286,21 +286,15 @@ function entity_embed_filter_format_edit_form_validate($form, FormStateInterface
}

/**
 * Implements hook_field_widget_form_alter().
 * Implements hook_field_widget_single_element_form_alter().
 */
function entity_embed_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
function entity_embed_field_widget_single_element_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();
      }
    }
  if (isset($element['#type']) && $element['#type'] === 'text_format') {
    /** @var FieldItemListInterface $items */
    $items = $context['items'];
    $element['#attributes']['data-entity_embed-host-entity-langcode'] = $items->getEntity()->language()->getId();
  }
}