Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Form / SynonymSettingsForm.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Form;
4
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;
10
11 /**
12  * Class SynonymSettingsForm.
13  *
14  * @package Drupal\search_api_synonym\Form
15  *
16  * @ingroup search_api_synonym
17  */
18 class SynonymSettingsForm extends ConfigFormBase {
19
20   /**
21    * An array containing available export plugins.
22    *
23    * @var array
24    */
25   protected $availablePlugins = [];
26
27   /**
28    * Constructs a VacancySourceForm object.
29    *
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.
34    */
35   public function __construct(ConfigFactoryInterface $config_factory, ExportPluginManager $manager) {
36     parent::__construct($config_factory);
37
38     foreach ($manager->getAvailableExportPlugins() as $id => $definition) {
39       $this->availablePlugins[$id] = $manager->createInstance($id);
40     }
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static(
48       $container->get('config.factory'),
49       $container->get('plugin.manager.search_api_synonym.export')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getFormId() {
57     return 'search_api_synonym_settings';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   protected function getEditableConfigNames() {
64     return ['search_api_synonym.settings'];
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function buildForm(array $form, FormStateInterface $form_state) {
71     $config = $this->config($this->getEditableConfigNames()[0]);
72
73     // Add cron settings
74     $cron = $config->get('cron');
75     $form['cron'] = [
76       '#title' => $this->t('Cron settings'),
77       '#type' => 'details',
78       '#open' => TRUE,
79       '#tree' => TRUE,
80     ];
81
82     $options = [];
83     foreach ($this->availablePlugins as $id => $source) {
84       $definition = $source->getPluginDefinition();
85       $options[$id] = $definition['label'];
86     }
87     $form['cron']['plugin'] = [
88       '#type' => 'select',
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,
93     ];
94
95     $options = [900, 1800, 3600, 10800, 21600, 43200, 86400, 604800];
96     $form['cron']['interval'] = [
97       '#type' => 'select',
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)),
102     ];
103
104     $form['cron']['type'] = [
105       '#type' => 'radios',
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',
109       '#options' => [
110         'all' => $this->t('All'),
111         'synonym' => $this->t('Synonyms'),
112         'spelling_error' => $this->t('Spelling errors')
113       ],
114     ];
115
116     $form['cron']['filter'] = [
117       '#type' => 'radios',
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',
121       '#options' => [
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')
125       ],
126     ];
127
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'] : '',
133       '#states' => [
134         'visible' => [
135           ':radio[name="cron[filter]"]' => ['value' => 'none'],
136         ],
137       ],
138     ];
139
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,
145     ];
146
147     return parent::buildForm($form, $form_state);
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function validateForm(array &$form, FormStateInterface $form_state) {
154     parent::validateForm($form, $form_state);
155   }
156
157   /**
158    * {@inheritdoc}
159    */
160   public function submitForm(array &$form, FormStateInterface $form_state) {
161     $this->config($this->getEditableConfigNames()[0])
162       ->set('cron', $form_state->getValue('cron'))
163       ->save();
164
165     parent::submitForm($form, $form_state);
166   }
167
168 }