You are here

public function ConfigTranslationFormBase::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config_translation/src/Form/ConfigTranslationFormBase.php \Drupal\config_translation\Form\ConfigTranslationFormBase::buildForm()
  2. 10 core/modules/config_translation/src/Form/ConfigTranslationFormBase.php \Drupal\config_translation\Form\ConfigTranslationFormBase::buildForm()

Implements \Drupal\Core\Form\FormInterface::buildForm().

Builds configuration form with metadata and values from the source language.

Parameters

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

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

\Drupal\Core\Routing\RouteMatchInterface $route_match: (optional) The route match.

string $plugin_id: (optional) The plugin ID of the mapper.

string $langcode: (optional) The language code of the language the form is adding or editing.

Return value

array The form structure.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Throws an exception if the language code provided as a query parameter in the request does not match an active language.

Overrides FormInterface::buildForm

2 calls to ConfigTranslationFormBase::buildForm()
ConfigTranslationAddForm::buildForm in core/modules/config_translation/src/Form/ConfigTranslationAddForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().
ConfigTranslationEditForm::buildForm in core/modules/config_translation/src/Form/ConfigTranslationEditForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().
2 methods override ConfigTranslationFormBase::buildForm()
ConfigTranslationAddForm::buildForm in core/modules/config_translation/src/Form/ConfigTranslationAddForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().
ConfigTranslationEditForm::buildForm in core/modules/config_translation/src/Form/ConfigTranslationEditForm.php
Implements \Drupal\Core\Form\FormInterface::buildForm().

File

core/modules/config_translation/src/Form/ConfigTranslationFormBase.php, line 129

Class

ConfigTranslationFormBase
Provides a base form for configuration translations.

Namespace

Drupal\config_translation\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {

  /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
  $mapper = $this->configMapperManager
    ->createInstance($plugin_id);
  $mapper
    ->populateFromRouteMatch($route_match);
  $language = $this->languageManager
    ->getLanguage($langcode);
  if (!$language) {
    throw new NotFoundHttpException();
  }
  $this->mapper = $mapper;
  $this->language = $language;

  // ConfigTranslationFormAccess will not grant access if this raises an
  // exception, so we can call this without a try-catch block here.
  $langcode = $this->mapper
    ->getLangcode();
  $this->sourceLanguage = $this->languageManager
    ->getLanguage($langcode);

  // Get base language configuration to display in the form before setting the
  // language to use for the form. This avoids repetitively settings and
  // resetting the language to get original values later.
  $this->baseConfigData = $this->mapper
    ->getConfigData();

  // Set the translation target language on the configuration factory.
  $original_language = $this->languageManager
    ->getConfigOverrideLanguage();
  $this->languageManager
    ->setConfigOverrideLanguage($this->language);

  // Add some information to the form state for easier form altering.
  $form_state
    ->set('config_translation_mapper', $this->mapper);
  $form_state
    ->set('config_translation_language', $this->language);
  $form_state
    ->set('config_translation_source_language', $this->sourceLanguage);
  $form['#attached']['library'][] = 'config_translation/drupal.config_translation.admin';

  // Even though this is a nested form, we do not set #tree to TRUE because
  // the form value structure is generated by using #parents for each element.
  // @see \Drupal\config_translation\FormElement\FormElementBase::getElements()
  $form['config_names'] = [
    '#type' => 'container',
  ];
  foreach ($this->mapper
    ->getConfigNames() as $name) {
    $form['config_names'][$name] = [
      '#type' => 'container',
    ];
    $schema = $this->typedConfigManager
      ->get($name);
    $source_config = $this->baseConfigData[$name];
    $translation_config = $this
      ->configFactory()
      ->get($name)
      ->get();
    if ($form_element = $this
      ->createFormElement($schema)) {
      $parents = [
        'config_names',
        $name,
      ];
      $form['config_names'][$name] += $form_element
        ->getTranslationBuild($this->sourceLanguage, $this->language, $source_config, $translation_config, $parents);
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save translation'),
    '#button_type' => 'primary',
  ];

  // Set the configuration language back.
  $this->languageManager
    ->setConfigOverrideLanguage($original_language);
  return $form;
}