tarte_au_citron-1.0.0-beta1/src/Form/ConfigurationTextsForm.php
src/Form/ConfigurationTextsForm.php
<?php
namespace Drupal\tarte_au_citron\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements a tarte_au_citron text overriding form.
*/
class ConfigurationTextsForm extends AbstractForm {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'tarte_au_citron_configuration_texts';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['tarte_au_citron.texts.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config('tarte_au_citron.texts.settings');
$form['strategy'] = [
'#type' => 'select',
'#title' => $this->t('Strategy'),
'#options' => [
'forced' => $this->t('Forced'),
'custom' => $this->t('Custom'),
],
'#required' => FALSE,
'#empty_value' => '',
'#empty_option' => $this->t('Default'),
'#default_value' => $config->get('strategy'),
];
$availableLanguages = $this->libraryJsDiscover->getTextsAvailableLanguagesConfig();
$langOptions = ['current' => $this->t('Current language')];
foreach ($availableLanguages as $lang => $langObj) {
$langOptions[$lang] = $langObj->getName();
}
$form['forced_lang'] = [
'#type' => 'select',
'#title' => $this->t('Forced language'),
'#options' => $langOptions,
'#required' => FALSE,
'#empty_value' => '',
'#empty_option' => $this->t('Default'),
'#default_value' => $config->get('forced_lang') ?? 'default',
'#states' => [
'visible' => [
':input[name="strategy"]' => ['value' => 'forced'],
],
],
];
$form['texts'] = [
'#type' => 'details',
'#title' => $this->t('Tarte au citron texts configuration'),
'#open' => TRUE,
'#tree' => TRUE,
'#states' => [
'visible' => [
':input[name="strategy"]' => ['value' => 'custom'],
],
],
];
$form['texts']['title_conf'] = [
'#theme' => 'tarte_au_citron_texts_config_form_header',
];
$texts = $this->libraryJsDiscover->getTextsConfig();
foreach ($texts as $key => $text) {
$this->buildElement($form['texts'], $form_state, $key, $text, ['texts']);
}
return parent::buildForm($form, $form_state);
}
/**
* Build a text element.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param string $key
* The key.
* @param array $text
* The text item.
* @param array $parents
* The parents.
*/
protected function buildElement(
array &$form,
FormStateInterface $form_state,
string $key,
array $text,
array $parents = [],
): void {
$config = $this->config('tarte_au_citron.texts.settings');
if ($text['type'] === 'mapping') {
$form[$key] = [
'#type' => 'details',
'#title' => $key,
'#open' => TRUE,
'#tree' => TRUE,
];
$nextParents = array_merge($parents, [$key]);
foreach ($text['children'] as $subkey => $subtext) {
$this->buildElement($form[$key], $form_state, $subkey, $subtext, $nextParents);
}
}
else {
$form[$key] = [
'#type' => $text['type'] === 'text' ? 'textarea' : 'textfield',
'#title' => $text['default_value'],
'#default_value' => $config->get(implode('.', $parents) . '.' . $key),
];
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->config('tarte_au_citron.texts.settings');
$config
->set('strategy', $form_state->getValue('strategy'));
if ($form_state->getValue('strategy') === 'forced') {
$config
->set('forced_lang', $form_state->getValue('forced_lang'));
}
else {
$config
->clear('forced_lang');
}
if ($form_state->getValue('strategy') === 'custom') {
$texts = $form_state->getValue('texts') ?? [];
_tarte_au_citron_filter_text($texts);
$config
->set('texts', $texts);
}
else {
$config
->clear('texts');
}
$config->save();
parent::submitForm($form, $form_state);
}
}
