ckeditor_taxonomy_glossary-1.0.0-alpha1/src/Form/GlossarySettingsForm.php
src/Form/GlossarySettingsForm.php
<?php
declare(strict_types=1);
namespace Drupal\ckeditor_taxonomy_glossary\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure glossary settings.
*/
final class GlossarySettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
#[\Override]
public function getFormId(): string {
return 'ckeditor_taxonomy_glossary_settings';
}
/**
* {@inheritdoc}
*/
#[\Override]
protected function getEditableConfigNames(): array {
return ['ckeditor_taxonomy_glossary.settings'];
}
/**
* {@inheritdoc}
*/
#[\Override]
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config('ckeditor_taxonomy_glossary.settings');
$form['tooltip_behavior'] = [
'#type' => 'fieldset',
'#title' => $this->t('Tooltip Behavior'),
];
$form['tooltip_behavior']['show_on_hover'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show tooltips on hover'),
'#description' => $this->t('Display tooltips when users hover over glossary links.'),
'#default_value' => $config->get('show_on_hover') ?? TRUE,
];
$form['tooltip_behavior']['show_on_click'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show tooltips on click'),
'#description' => $this->t('Display tooltips when users click on glossary links.'),
'#default_value' => $config->get('show_on_click') ?? TRUE,
];
$form['tooltip_behavior']['delay'] = [
'#type' => 'number',
'#title' => $this->t('Hover delay (milliseconds)'),
'#description' => $this->t('Time to wait before showing tooltip on hover.'),
'#default_value' => $config->get('delay') ?? 200,
'#min' => 0,
'#max' => 1000,
'#step' => 50,
];
$form['tooltip_behavior']['show_close_button'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show close button'),
'#description' => $this->t('Display a close button (×) in the tooltip for easy dismissal.'),
'#default_value' => $config->get('show_close_button') ?? FALSE,
];
$form['performance'] = [
'#type' => 'fieldset',
'#title' => $this->t('Performance'),
];
$form['performance']['preload_descriptions'] = [
'#type' => 'checkbox',
'#title' => $this->t('Preload term descriptions'),
'#description' => $this->t('Load all term descriptions with the page instead of fetching them on demand. Improves performance but increases initial page size.'),
'#default_value' => $config->get('preload_descriptions') ?? TRUE,
];
$form['performance']['cache_duration'] = [
'#type' => 'select',
'#title' => $this->t('Cache duration for term descriptions'),
'#description' => $this->t('How long to cache term descriptions in the browser.'),
'#options' => [
'0' => $this->t('No cache'),
'300' => $this->t('5 minutes'),
'3600' => $this->t('1 hour'),
'86400' => $this->t('1 day'),
'604800' => $this->t('1 week'),
],
'#default_value' => $config->get('cache_duration') ?? '3600',
];
$form['appearance'] = [
'#type' => 'fieldset',
'#title' => $this->t('Appearance'),
];
$form['appearance']['max_width'] = [
'#type' => 'number',
'#title' => $this->t('Maximum tooltip width (pixels)'),
'#description' => $this->t('Maximum width of the tooltip popup.'),
'#default_value' => $config->get('max_width') ?? 320,
'#min' => 200,
'#max' => 600,
'#step' => 10,
];
$form['appearance']['link_style'] = [
'#type' => 'select',
'#title' => $this->t('Glossary link style'),
'#description' => $this->t('How glossary links should appear in content.'),
'#options' => [
'dotted' => $this->t('Dotted underline'),
'solid' => $this->t('Solid underline'),
'dashed' => $this->t('Dashed underline'),
'highlight' => $this->t('Highlighted background'),
],
'#default_value' => $config->get('link_style') ?? 'dotted',
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
#[\Override]
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config('ckeditor_taxonomy_glossary.settings')
->set('show_on_hover', $form_state->getValue('show_on_hover'))
->set('show_on_click', $form_state->getValue('show_on_click'))
->set('delay', $form_state->getValue('delay'))
->set('show_close_button', $form_state->getValue('show_close_button'))
->set('preload_descriptions', $form_state->getValue('preload_descriptions'))
->set('cache_duration', $form_state->getValue('cache_duration'))
->set('max_width', $form_state->getValue('max_width'))
->set('link_style', $form_state->getValue('link_style'))
->save();
parent::submitForm($form, $form_state);
}
}
