You are here

protected function ParagraphEditForm::init in Paragraphs table 8

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 17

Class

ParagraphEditForm
Paragraph Edit Form class.

Namespace

Drupal\paragraphs_table\Form

Code

protected function init(FormStateInterface $form_state) {
  $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);
}