tarte_au_citron-1.0.0-beta1/src/Form/ConfigurationJsForm.php
src/Form/ConfigurationJsForm.php
<?php
namespace Drupal\tarte_au_citron\Form;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements a tarte_au_citron configuration form.
*/
class ConfigurationJsForm extends AbstractForm {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'tarte_au_citron_configuration_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['tarte_au_citron.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
// Chosen settings:
$config = $this->configFactory->get('tarte_au_citron.settings');
$form['tacConfig'] = [
'#type' => 'details',
'#title' => $this->t('Tarte au citron JS library configuration'),
'#open' => TRUE,
'#tree' => TRUE,
];
$form['tacConfig']['title_conf'] = [
'#theme' => 'tarte_au_citron_config_form_header',
];
$jsConfig = $this->libraryJsDiscover->getJsConfig();
foreach ($jsConfig as $key => $def) {
$form['tacConfig'][$key] = [
'#title' => $key,
'#type' => $def['type'] === 'boolean' ? 'checkbox' : 'textfield',
'#default_value' => $config->get('tacConfig.' . $key),
];
}
$form['title_service'] = [
'#type' => 'markup',
'#markup' => '<h2><b>' . $this->t('Services') . '</b></h2>',
];
$form['services'] = [
'#title' => $this->t('Services enabled'),
'#description' => $this->t('Services enabled.'),
'#type' => 'checkboxes',
'#options' => $this->servicesManager->getServicesOptionList(),
'#default_value' => $config->get('services'),
];
$form['services_settings'] = [
'#type' => 'container',
'#title' => $this->t('Services settings'),
'#tree' => TRUE,
];
foreach ($this->servicesManager->getServices() as $service) {
$children = $service->settingsForm($form, $form_state);
if (empty($children)) {
continue;
}
$htmlId = Html::getId($service->getPluginId());
$form['services_settings'][$service->getPluginId()] = [
'#type' => 'fieldset',
'#title' => $service->getPluginTitle(),
'#states' => [
'visible' => [
':input[id="edit-services-' . $htmlId . '"]' => ['checked' => TRUE],
],
],
];
foreach ($children as &$current_child) {
if (!empty($current_child['#required'])) {
unset($current_child['#required']);
$current_child['#states'] = [
'required' => [
':input[id="edit-services-' . $service->getPluginId() . '"]' => ['checked' => TRUE],
],
];
}
}
$form['services_settings'][$service->getPluginId()] += $children;
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
$values = $form_state->getValue('tacConfig');
$jsConfig = $this->libraryJsDiscover->getJsConfig();
foreach ($values as $key => $value) {
if (empty($value) || empty($jsConfig[$key]['isUrl']) || !$jsConfig[$key]['isUrl']) {
continue;
}
if ((mb_substr($value, 0, 1) !== '/' && !UrlHelper::isValid($value, TRUE)) || !UrlHelper::isValid($value)) {
$form_state->setError($form['tacConfig'][$key], $this->t('Url is not valid.'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->config('tarte_au_citron.settings');
$enabledServices = array_filter($form_state->getValue('services'));
$servicesConfigurations = array_intersect_key($form_state->getValue('services_settings') ?? [], $enabledServices);
$config
->set('tacConfig', $form_state->getValue('tacConfig'))
->set('services', $enabledServices)
->set('services_settings', $servicesConfigurations);
$config->save();
$this->cache->invalidate('tarte_au_citron:configurations_texts');
parent::submitForm($form, $form_state);
}
}
