3 namespace Drupal\search_api_synonym\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\search_api_synonym\Export\ExportPluginManager;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Class SynonymSettingsForm.
14 * @package Drupal\search_api_synonym\Form
16 * @ingroup search_api_synonym
18 class SynonymSettingsForm extends ConfigFormBase {
21 * An array containing available export plugins.
25 protected $availablePlugins = [];
28 * Constructs a VacancySourceForm object.
30 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
31 * The factory for configuration objects.
32 * @param \Drupal\search_api_synonym\Export\ExportPluginManager $manager
33 * The synonyms export plugin manager.
35 public function __construct(ConfigFactoryInterface $config_factory, ExportPluginManager $manager) {
36 parent::__construct($config_factory);
38 foreach ($manager->getAvailableExportPlugins() as $id => $definition) {
39 $this->availablePlugins[$id] = $manager->createInstance($id);
46 public static function create(ContainerInterface $container) {
48 $container->get('config.factory'),
49 $container->get('plugin.manager.search_api_synonym.export')
56 public function getFormId() {
57 return 'search_api_synonym_settings';
63 protected function getEditableConfigNames() {
64 return ['search_api_synonym.settings'];
70 public function buildForm(array $form, FormStateInterface $form_state) {
71 $config = $this->config($this->getEditableConfigNames()[0]);
74 $cron = $config->get('cron');
76 '#title' => $this->t('Cron settings'),
83 foreach ($this->availablePlugins as $id => $source) {
84 $definition = $source->getPluginDefinition();
85 $options[$id] = $definition['label'];
87 $form['cron']['plugin'] = [
89 '#title' => $this->t('Synonym export plugin'),
90 '#description' => $this->t('Select the export plugin being used by cron.'),
91 '#default_value' => $cron['plugin'] ? $cron['plugin'] : '',
92 '#options' => $options,
95 $options = [900, 1800, 3600, 10800, 21600, 43200, 86400, 604800];
96 $form['cron']['interval'] = [
98 '#title' => $this->t('Export synonyms every'),
99 '#description' => $this->t('How often should Drupal export synonyms?'),
100 '#default_value' => $cron['interval'] ? $cron['interval'] : 86400,
101 '#options' => array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($options, $options)),
104 $form['cron']['type'] = [
106 '#title' => $this->t('Type'),
107 '#description' => $this->t('Which synonym type should be exported by cron?'),
108 '#default_value' => $cron['type'] ? $cron['type'] : 'all',
110 'all' => $this->t('All'),
111 'synonym' => $this->t('Synonyms'),
112 'spelling_error' => $this->t('Spelling errors')
116 $form['cron']['filter'] = [
118 '#title' => $this->t('Filter'),
119 '#description' => $this->t('Which filters should be used when selecting synonyms.'),
120 '#default_value' => $cron['filter'] ? $cron['filter'] : 'none',
122 'none' => $this->t('No filter'),
123 'nospace' => $this->t('Synonyms without spaces in the word'),
124 'onlyspace' => $this->t('Synonyms with spaces in the word')
128 $form['cron']['separate_files'] = [
129 '#type' => 'checkbox',
130 '#title' => $this->t('Separate files'),
131 '#description' => $this->t('Export synonyms with and without spaces into separate files.'),
132 '#default_value' => $cron['separate_files'] ? $cron['separate_files'] : '',
135 ':radio[name="cron[filter]"]' => ['value' => 'none'],
140 $form['cron']['export_if_changed'] = [
141 '#type' => 'checkbox',
142 '#title' => $this->t('Only export if changes'),
143 '#description' => $this->t('Only export synonyms if their is either new or changed synonyms since last export.'),
144 '#default_value' => $cron['export_if_changed'] ? $cron['export_if_changed'] : FALSE,
147 return parent::buildForm($form, $form_state);
153 public function validateForm(array &$form, FormStateInterface $form_state) {
154 parent::validateForm($form, $form_state);
160 public function submitForm(array &$form, FormStateInterface $form_state) {
161 $this->config($this->getEditableConfigNames()[0])
162 ->set('cron', $form_state->getValue('cron'))
165 parent::submitForm($form, $form_state);