You are here

public static function TranslationHelper::updateEntityLangcode in Inline Entity Form 8

Updates the entity langcode to match the form langcode.

Called on submit to allow the user to select a different language through the langcode form element, which is then transferred to form state.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

bool TRUE if the entity langcode was updated, FALSE otherwise.

2 calls to TranslationHelper::updateEntityLangcode()
InlineEntityFormComplex::extractFormValues in src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
Extracts field values from submitted form values.
InlineEntityFormSimple::extractFormValues in src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
Extracts field values from submitted form values.

File

src/TranslationHelper.php, line 72

Class

TranslationHelper
Provides content translation helpers.

Namespace

Drupal\inline_entity_form

Code

public static function updateEntityLangcode(ContentEntityInterface $entity, FormStateInterface $form_state) {
  $changed = FALSE;

  // This method is first called during form validation, at which point
  // the 'langcode' form state flag hasn't been updated with the new value.
  $form_langcode = $form_state
    ->getValue([
    'langcode',
    0,
    'value',
  ], $form_state
    ->get('langcode'));
  if (empty($form_langcode) || !$entity
    ->isTranslatable()) {
    return $changed;
  }
  $entity_langcode = $entity
    ->language()
    ->getId();
  if ($entity_langcode != $form_langcode && !$entity
    ->hasTranslation($form_langcode)) {
    $langcode_key = $entity
      ->getEntityType()
      ->getKey('langcode');
    $entity
      ->set($langcode_key, $form_langcode);
    $changed = TRUE;
  }
  return $changed;
}