You are here

protected function ParagraphEditForm::init in Paragraphs Edit 8.2

Initializes the form state and the entity before the first form build.

Parameters

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

Overrides ContentEntityForm::init

File

src/Form/ParagraphEditForm.php, line 19

Class

ParagraphEditForm
ParagraphEditForm class.

Namespace

Drupal\paragraphs_edit\Form

Code

protected function init(FormStateInterface $form_state) {
  if ($this->entity
    ->isTranslatable()) {
    $langcode = \Drupal::languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId();
    $form_state
      ->set('langcode', $langcode);
    if (!$this->entity
      ->hasTranslation($langcode)) {
      $manager = \Drupal::service('content_translation.manager');
      $translation_source = $this->entity;
      $host = $this->entity
        ->getParentEntity();
      $host_source_langcode = $host
        ->language()
        ->getId();
      if ($host
        ->hasTranslation($langcode)) {
        $host = $host
          ->getTranslation($langcode);
        $host_source_langcode = $manager
          ->getTranslationMetadata($host)
          ->getSource();
      }
      if ($this->entity
        ->hasTranslation($host_source_langcode)) {
        $translation_source = $this->entity
          ->getTranslation($host_source_langcode);
      }
      $this->entity = $this->entity
        ->addTranslation($langcode, $translation_source
        ->toArray());
      $manager
        ->getTranslationMetadata($this->entity)
        ->setSource($translation_source
        ->language()
        ->getId());
    }
  }
  parent::init($form_state);
}