You are here

public function MergeTranslationsForm::buildForm in Merge translations 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/MergeTranslationsForm.php, line 164

Class

MergeTranslationsForm
The merge translation form.

Namespace

Drupal\merge_translations\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $isTranslationImportAvailable = $this
    ->isTranslationImportAvailable();
  $form['node_translations'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Language'),
      $this
        ->t('Translation'),
      $this
        ->t('Status'),
    ],
    '#rows' => [],
  ];
  foreach ($this->languages
    ->getLanguages() as $key => $language) {
    $language_name = $language
      ->getName();
    $source = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'node',
      '#selection_settings' => [
        'target_bundles' => [
          $this->node
            ->getType(),
        ],
      ],
      '#disabled' => $isTranslationImportAvailable,
      '#maxlength' => 255,
    ];
    $status = '-';
    if ($this->node
      ->getTranslationStatus($key)) {
      $translation = $this->node
        ->getTranslation($key);
      $source = [
        '#markup' => $this
          ->t('<a href="@href">@title</a>', [
          '@href' => $translation
            ->toUrl()
            ->toString(),
          '@title' => $translation
            ->getTitle(),
        ]),
      ];
      $status = $translation
        ->isPublished() ? $this
        ->t('Published') : $this
        ->t('Not published');
      if ($translation
        ->isDefaultTranslation()) {
        $language_name = $this
          ->t('<b>@language (Original language)</b>', [
          '@language' => $language_name,
        ]);
      }
    }
    $form['node_translations'][$key]['language_name'] = [
      '#markup' => $language_name,
    ];
    $form['node_translations'][$key]['node_source'] = $source;
    $form['node_translations'][$key]['status'] = [
      '#markup' => $status,
    ];
  }
  $actions = [
    self::ACTION_DONOTHING => $this
      ->t('Do nothing'),
  ];
  $type = $this->node
    ->getType();
  if ($this->currentUser
    ->hasPermission("bypass node access") || $this->currentUser
    ->hasPermission("delete any {$type} content") || $this->currentUser
    ->hasPermission("delete own {$type} content")) {
    $actions[self::ACTION_REMOVE] = $this
      ->t('Remove node');
  }
  $form['node_source_action'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Action with source node after import'),
    '#options' => $actions,
    '#default_value' => self::ACTION_DONOTHING,
    '#disabled' => $isTranslationImportAvailable,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Import translations'),
    '#button_type' => 'primary',
    '#disabled' => $isTranslationImportAvailable,
  ];
  return $form;
}