View source
<?php
namespace Drupal\layout_builder_st\Form;
use Drupal\Component\Utility\Html;
use Drupal\config_translation\Form\ConfigTranslationFormBase;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TraversableTypedDataInterface;
use Drupal\layout_builder\Controller\LayoutRebuildTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder_st\TranslatableSectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TranslateBlockForm extends FormBase {
use AjaxFormHelperTrait;
use LayoutRebuildTrait;
protected $sectionStorage;
protected $uuid;
protected $layoutTempstoreRepository;
protected $moduleHandler;
protected $typedConfigManager;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ModuleHandlerInterface $module_handler, TypedConfigManagerInterface $typed_config_manager) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->moduleHandler = $module_handler;
$this->typedConfigManager = $typed_config_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'), $container
->get('module_handler'), $container
->get('config.typed'));
}
public function getFormId() {
return 'layout_builder_block_translation';
}
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());
$definition = new $type_definition['definition_class']($type_definition);
$definition
->setClass($type_definition['class']);
$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';
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
}
return $form;
}
protected function createTranslationElement(LanguageInterface $source_language, LanguageInterface $translation_language, TraversableTypedDataInterface $typed_data, array $translated_configuration) {
if ($this->moduleHandler
->moduleExists('config_translation')) {
$form_element = ConfigTranslationFormBase::createFormElement($typed_data);
$element_build = $form_element
->getTranslationBuild($source_language, $translation_language, $typed_data
->getValue(), $translated_configuration, []);
}
else {
if (($label_data = $typed_data
->get('label')) && $label_data instanceof StringData) {
$element_build['label']['source'] = [
'#type' => 'item',
'#title' => $this
->t('Label'),
'#markup' => $label_data
->getValue() ?: '(' . $this
->t('Empty') . ')',
'#parents' => [
'source',
'label',
],
];
$element_build['label']['translation'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#default_value' => isset($translated_configuration['label']) ? $translated_configuration['label'] : '',
'#parents' => [
'translation',
'label',
],
];
}
}
$element_build['#tree'] = TRUE;
return $element_build;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$section_storage = $this->sectionStorage;
$section_storage
->setTranslatedComponentConfiguration($this->uuid, $form_state
->getValue('translation'));
$this->layoutTempstoreRepository
->set($this->sectionStorage);
$form_state
->setRedirectUrl($this->sectionStorage
->getLayoutBuilderUrl());
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
return $this
->rebuildAndClose($this->sectionStorage);
}
}