You are here

public function StringoverridesAdminForm::buildForm in String Overrides 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/StringoverridesAdminForm.php, line 23

Class

StringoverridesAdminForm
Class StringoverridesAdminForm.

Namespace

Drupal\stringoverrides\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $language = 'default') {
  $strings = $this
    ->getCurrentTranslations($language);
  $form['lang'] = [
    '#type' => 'value',
    '#value' => $language,
  ];
  $form['translations_table'] = [
    '#type' => 'table',
    '#header' => [
      'Enabled',
      'Original',
      'Replacement',
      'Context',
    ],
    '#title' => $this
      ->t('Translations'),
    '#attributes' => [
      'id' => 'stringoverrides-wrapper',
    ],
  ];
  $storage = $form_state
    ->getStorage();
  if (empty($storage['number-of-rows'])) {
    $storage['number-of-rows'] = count($strings) + 1;
    $form_state
      ->setStorage($storage);
  }
  for ($i = 0; $i < $storage['number-of-rows']; $i++) {

    // Add 4 input elements to table row.
    $form['translations_table'][$i] = $this
      ->buildFormTranslationRow($strings, $i);
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['add-row'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add extra row'),
    '#submit' => [
      '::addExtraRow',
    ],
    '#ajax' => [
      'callback' => '::addExtraRowAjaxCallback',
      'wrapper' => 'stringoverrides-wrapper',
    ],
  ];
  $form['actions']['remove'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Remove disabled strings'),
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save configuration'),
  ];
  return $form;
}