You are here

public function TranslateBlockForm::buildForm in Layout Builder Symmetric Translations 8

Builds the block translation form.

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\layout_builder\SectionStorageInterface $section_storage: The section storage being configured.

int $delta: The delta of the section.

string $region: The region of the block.

string $uuid: The UUID of the block being updated.

Return value

array The form array.

Overrides FormInterface::buildForm

File

src/Form/TranslateBlockForm.php, line 110

Class

TranslateBlockForm
Provides a form to translate a block plugin in the Layout Builder.

Namespace

Drupal\layout_builder_st\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, TranslatableSectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL) {
  $component = $section_storage
    ->getSection($delta)
    ->getComponent($uuid);
  $this->sectionStorage = $section_storage;
  $this->uuid = $component
    ->getUuid();
  $configuration = $component
    ->getPlugin()
    ->getConfiguration();
  $type_definition = $this->typedConfigManager
    ->getDefinition('block.settings.' . $component
    ->getPlugin()
    ->getPluginId());

  /** @var \Drupal\Core\TypedData\DataDefinitionInterface $definition */
  $definition = new $type_definition['definition_class']($type_definition);
  $definition
    ->setClass($type_definition['class']);

  /** @var \Drupal\Core\Config\Schema\Mapping $typed_data */
  $typed_data = $type_definition['class']::createInstance($definition);
  $typed_data
    ->setValue($configuration);
  $translated_config = $this->sectionStorage
    ->getTranslatedComponentConfiguration($this->uuid);
  foreach (array_keys($configuration) as $key) {
    if (!isset($translated_config[$key])) {
      $translated_config[$key] = NULL;
    }
  }
  $form['translation'] = $this
    ->createTranslationElement($section_storage
    ->getSourceLanguage(), $section_storage
    ->getTranslationLanguage(), $typed_data, $translated_config);
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Translate'),
  ];
  if ($this
    ->isAjax()) {
    $form['submit']['#ajax']['callback'] = '::ajaxSubmit';

    // @todo static::ajaxSubmit() requires data-drupal-selector to be the same
    //   between the various Ajax requests. A bug in
    //   \Drupal\Core\Form\FormBuilder prevents that from happening unless
    //   $form['#id'] is also the same. Normally, #id is set to a unique HTML
    //   ID via Html::getUniqueId(), but here we bypass that in order to work
    //   around the data-drupal-selector bug. This is okay so long as we
    //   assume that this form only ever occurs once on a page. Remove this
    //   workaround in https://www.drupal.org/node/2897377.
    $form['#id'] = Html::getId($form_state
      ->getBuildInfo()['form_id']);
  }
  return $form;
}