You are here

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

Prepares the inline entity for translation.

Parameters

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

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

Return value

\Drupal\Core\Entity\ContentEntityInterface The prepared entity.

See also

\Drupal\Core\Entity\ContentEntityForm::initFormLangcodes()

2 calls to TranslationHelper::prepareEntity()
InlineEntityForm::processEntityForm in src/Element/InlineEntityForm.php
Builds the entity form using the inline form handler.
InlineEntityFormBase::prepareFormState in src/Plugin/Field/FieldWidget/InlineEntityFormBase.php
Prepares the form state for the current widget.

File

src/TranslationHelper.php, line 26

Class

TranslationHelper
Provides content translation helpers.

Namespace

Drupal\inline_entity_form

Code

public static function prepareEntity(ContentEntityInterface $entity, FormStateInterface $form_state) {
  $form_langcode = $form_state
    ->get('langcode');
  if (empty($form_langcode) || !$entity
    ->isTranslatable()) {
    return $entity;
  }
  $entity_langcode = $entity
    ->language()
    ->getId();
  if (self::isTranslating($form_state) && !$entity
    ->hasTranslation($form_langcode)) {

    // Create a translation from the source language values.
    $source = $form_state
      ->get([
      'content_translation',
      'source',
    ]);
    $source_langcode = $source ? $source
      ->getId() : $entity_langcode;
    if (!$entity
      ->hasTranslation($source_langcode)) {
      $entity
        ->addTranslation($source_langcode, $entity
        ->toArray());
    }
    $source_translation = $entity
      ->getTranslation($source_langcode);
    $entity
      ->addTranslation($form_langcode, $source_translation
      ->toArray());
    $translation = $entity
      ->getTranslation($form_langcode);
    $translation
      ->set('content_translation_source', $source_langcode);

    // Make sure we do not inherit the affected status from the source values.
    if ($entity
      ->getEntityType()
      ->isRevisionable()) {
      $translation
        ->setRevisionTranslationAffected(NULL);
    }
  }
  if ($entity_langcode != $form_langcode && $entity
    ->hasTranslation($form_langcode)) {

    // Switch to the needed translation.
    $entity = $entity
      ->getTranslation($form_langcode);
  }
  return $entity;
}