f342e80e2978272a77c498235265a73cc5c43945
[yaffs-website] / web / modules / contrib / eu_cookie_compliance / src / Form / EuCookieComplianceConfigForm.php
1 <?php
2
3 namespace Drupal\eu_cookie_compliance\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\Core\Path\PathValidatorInterface;
9 use Drupal\Core\Routing\RequestContext;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides settings for eu_cookie_compliance module.
14  */
15 class EuCookieComplianceConfigForm extends ConfigFormBase {
16
17   /**
18    * The path validator.
19    *
20    * @var \Drupal\Core\Path\PathValidatorInterface
21    */
22   protected $pathValidator;
23
24   /**
25    * The request context.
26    *
27    * @var \Drupal\Core\Routing\RequestContext
28    */
29   protected $requestContext;
30
31   /**
32    * Constructs an EuCookieComplianceConfigForm object.
33    *
34    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35    *   The factory for configuration objects.
36    * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
37    *   The path validator.
38    * @param \Drupal\Core\Routing\RequestContext $request_context
39    *   The request context.
40    */
41   public function __construct(ConfigFactoryInterface $config_factory, PathValidatorInterface $path_validator, RequestContext $request_context) {
42     parent::__construct($config_factory);
43
44     $this->pathValidator = $path_validator;
45     $this->requestContext = $request_context;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container) {
52     return new static(
53       $container->get('config.factory'),
54       $container->get('path.validator'),
55       $container->get('router.request_context')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getFormId() {
63     return 'eu_cookie_compliance_config_form';
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function getEditableConfigNames() {
70     return [
71       'eu_cookie_compliance.settings',
72     ];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function buildForm(array $form, FormStateInterface $form_state) {
79
80     $config = $this->config('eu_cookie_compliance.settings');
81
82     $form['domain'] = array(
83       '#type' => 'textfield',
84       '#title' => $this->t('Domain'),
85       '#default_value' => $config->get('domain'),
86       '#description' => $this->t('Sets the domain of the cookie to a specific url.  Used when you need consistency across domains.  This is language independent.'),
87     );
88
89     $form['eu_cookie_compliance'] = array(
90       '#type' => 'details',
91       '#title' => $this->t('Settings'),
92       '#open' => TRUE,
93     );
94
95     $form['eu_cookie_compliance']['popup_enabled'] = array(
96       '#type' => 'checkbox',
97       '#title' => $this->t('Enable popup'),
98       '#default_value' => $config->get('popup_enabled'),
99     );
100
101     $form['eu_cookie_compliance']['popup_clicking_confirmation'] = array(
102       '#type' => 'checkbox',
103       '#title' => $this->t('Consent by clicking'),
104       '#default_value' => $config->get('popup_clicking_confirmation'),
105       '#description' => $this->t('By default by clicking any link on the website the visitor accepts the cookie policy. Uncheck this box if you do not require this functionality. You may want to edit the pop-up message below accordingly.'),
106     );
107
108     $form['eu_cookie_compliance']['popup_position'] = array(
109       '#type' => 'checkbox',
110       '#title' => $this->t('Place the pop-up at the top of the website'),
111       '#default_value' => $config->get('popup_position'),
112       '#description' => $this->t('By default the pop-up appears at the bottom of the website. Tick this box if you want it to appear at the top'),
113     );
114
115     $form['eu_cookie_compliance']['popup_agree_button_message'] = array(
116       '#type' => 'textfield',
117       '#title' => $this->t('Agree button message'),
118       '#default_value' => $config->get('popup_agree_button_message'),
119       '#size' => 30,
120       '#required' => TRUE,
121     );
122
123     $form['eu_cookie_compliance']['popup_disagree_button_message'] = array(
124       '#type' => 'textfield',
125       '#title' => $this->t('Disagree button message'),
126       '#default_value' => $config->get('popup_disagree_button_message'),
127       '#size' => 30,
128       '#required' => TRUE,
129     );
130
131     $form['eu_cookie_compliance']['popup_info'] = array(
132       '#type' => 'text_format',
133       '#title' => $this->t('Popup message - requests consent'),
134       '#default_value' => $config->get('popup_info.value'),
135       '#required' => TRUE,
136       '#format' => !empty($config->get('popup_info.format')) ? $config->get('popup_info.format') : filter_default_format(),
137     );
138
139     $form['eu_cookie_compliance']['popup_agreed_enabled'] = array(
140       '#type' => 'checkbox',
141       '#title' => $this->t('Enable thank you message'),
142       '#default_value' => $config->get('popup_agreed_enabled'),
143     );
144
145     $form['eu_cookie_compliance']['popup_hide_agreed'] = array(
146       '#type' => 'checkbox',
147       '#title' => $this->t('Clicking hides thank you message'),
148       '#default_value' => $config->get('popup_hide_agreed'),
149       '#description' => $this->t('Clicking a link hides the thank you message automatically.'),
150     );
151
152     $form['eu_cookie_compliance']['popup_find_more_button_message'] = array(
153       '#type' => 'textfield',
154       '#title' => $this->t('Find more button message'),
155       '#default_value' => $config->get('popup_find_more_button_message'),
156       '#size' => 30,
157       '#required' => TRUE,
158     );
159
160     $form['eu_cookie_compliance']['popup_hide_button_message'] = array(
161       '#type' => 'textfield',
162       '#title' => $this->t('Hide button message'),
163       '#default_value' => $config->get('popup_hide_button_message'),
164       '#size' => 30,
165       '#required' => TRUE,
166     );
167
168     $form['eu_cookie_compliance']['popup_agreed'] = array(
169       '#type' => 'text_format',
170       '#title' => $this->t('Popup message - thanks for giving consent'),
171       '#default_value' => !empty($config->get('popup_agreed')['value']) ? $config->get('popup_agreed')['value'] : '',
172       '#required' => TRUE,
173       '#format' => !empty($config->get('popup_agreed')['format']) ? $config->get('popup_agreed')['format'] : filter_default_format(),
174     );
175
176     $form['eu_cookie_compliance']['popup_link'] = array(
177       '#type' => 'url',
178       '#title' => $this->t('Privacy policy link'),
179       '#default_value' => $config->get('popup_link'),
180       '#maxlength' => 1024,
181       '#required' => TRUE,
182       '#description' => $this->t('Enter link to your privacy policy or other page that will explain cookies to your users, internal/external links should start with http:// or https://.'),
183     );
184
185     $form['eu_cookie_compliance']['popup_link_new_window'] = array(
186       '#type' => 'checkbox',
187       '#title' => $this->t('Open privacy policy link in a new window'),
188       '#default_value' => $config->get('popup_link_new_window'),
189     );
190
191     $form['eu_cookie_compliance']['popup_height'] = array(
192       '#type' => 'number',
193       '#title' => $this->t('Popup height in pixels'),
194       '#default_value' => !empty($config->get('popup_height')) ? $config->get('popup_height') : '',
195       '#field_suffix' => ' ' . $this->t('pixels'),
196       '#size' => 5,
197       '#maxlength' => 5,
198       '#required' => FALSE,
199       '#description' => $this->t('Enter an integer value for a desired height in pixels or leave empty for automatically adjusted height'),
200     );
201
202     $form['eu_cookie_compliance']['popup_width'] = array(
203       '#type' => 'textfield',
204       '#title' => $this->t('Popup width in pixels or a percentage value'),
205       '#default_value' => $config->get('popup_width'),
206       '#field_suffix' => ' ' . t('px or %'),
207       '#size' => 5,
208       '#maxlength' => 5,
209       '#required' => TRUE,
210       '#description' => $this->t('Set the width of the popup. This can be either an integer value or percentage of the screen width. For example: 200 or 50%'),
211     );
212
213     $form['eu_cookie_compliance']['popup_delay'] = array(
214       '#type' => 'number',
215       '#title' => $this->t('Popup time delay in seconds'),
216       '#default_value' => $config->get('popup_delay'),
217       '#field_suffix' => ' ' . t('seconds'),
218       '#size' => 5,
219       '#maxlength' => 5,
220       '#required' => TRUE,
221     );
222
223     $form_color_picker_type = 'textfield';
224
225     if (\Drupal::moduleHandler()->moduleExists('jquery_colorpicker')) {
226       $form_color_picker_type = 'jquery_colorpicker';
227     }
228
229     $form['eu_cookie_compliance']['popup_bg_hex'] = array(
230       '#type' => $form_color_picker_type,
231       '#title' => $this->t('Background Color'),
232       // Garland colors :).
233       '#default_value' => $config->get('popup_bg_hex'),
234       '#description' => $this->t('Change the background color of the popup. Provide HEX value without the #'),
235       '#element_validate' => array('eu_cookie_compliance_validate_hex'),
236     );
237
238     $form['eu_cookie_compliance']['popup_text_hex'] = array(
239       '#type' => $form_color_picker_type,
240       '#title' => $this->t('Text Color'),
241       '#default_value' => $config->get('popup_text_hex'),
242       '#description' => $this->t('Change the text color of the popup. Provide HEX value without the #'),
243       '#element_validate' => array('eu_cookie_compliance_validate_hex'),
244     );
245     // Adding option to add/remove popup on specified domains
246     $exclude_domains_option_active = array(
247       0 => $this->t('Add'),
248       1 => $this->t('Remove'),
249     );
250     $form['eu_cookie_compliance']['domains_option'] = array(
251       '#type' => 'radios',
252       '#title' => $this->t('Add/Remove popup on specified domains'),
253       '#default_value' => $config->get('domains_option'),
254       '#options' => $exclude_domains_option_active,
255       '#description' => $this->t('Specify if you want to add or remove popup on the listed below domains.'),
256     );
257     $form['eu_cookie_compliance']['domains_list'] = array(
258       '#type' => 'textarea',
259       '#title' => $this->t('Domains list'),
260       '#default_value' => $config->get('domains_list'),
261       '#description' => $this->t('Specify domains with protocol (e.g. http or https). Enter one domain per line.'),
262     );
263
264     $form['eu_cookie_compliance']['exclude_paths'] = array(
265       '#type' => 'textarea',
266       '#title' => $this->t('Exclude paths'),
267       '#default_value' => !empty($config->get('exclude_paths')) ? $config->get('exclude_paths') : '',
268       '#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
269         '%blog' => '/blog',
270         '%blog-wildcard' => '/blog/*',
271         '%front' => '<front>'
272       )),
273     );
274
275     return parent::buildForm($form, $form_state);
276   }
277
278   /**
279    * {@inheritdoc}
280    */
281   public function validateForm(array &$form, FormStateInterface $form_state) {
282     parent::validateForm($form, $form_state);
283
284     // @TODO Validate other form elements settings.
285   }
286
287   /**
288    * {@inheritdoc}
289    */
290   public function submitForm(array &$form, FormStateInterface $form_state) {
291     $this->config('eu_cookie_compliance.settings')
292       ->set('domain', $form_state->getValue('domain'))
293       ->set('popup_enabled', $form_state->getValue('popup_enabled'))
294       ->set('popup_clicking_confirmation', $form_state->getValue('popup_clicking_confirmation'))
295       ->set('popup_position', $form_state->getValue('popup_position'))
296       ->set('popup_agree_button_message', $form_state->getValue('popup_agree_button_message'))
297       ->set('popup_disagree_button_message', $form_state->getValue('popup_disagree_button_message'))
298       ->set('popup_info', $form_state->getValue('popup_info'))
299       ->set('popup_agreed_enabled', $form_state->getValue('popup_agreed_enabled'))
300       ->set('popup_hide_agreed', $form_state->getValue('popup_hide_agreed'))
301       ->set('popup_find_more_button_message', $form_state->getValue('popup_find_more_button_message'))
302       ->set('popup_hide_button_message', $form_state->getValue('popup_hide_button_message'))
303       ->set('popup_agreed', $form_state->getValue('popup_agreed'))
304       ->set('popup_link', $form_state->getValue('popup_link'))
305       ->set('popup_link_new_window', $form_state->getValue('popup_link_new_window'))
306       ->set('popup_height', $form_state->getValue('popup_height'))
307       ->set('popup_width', $form_state->getValue('popup_width'))
308       ->set('popup_delay', $form_state->getValue('popup_delay'))
309       ->set('popup_bg_hex', $form_state->getValue('popup_bg_hex'))
310       ->set('popup_text_hex', $form_state->getValue('popup_text_hex'))
311       ->set('domains_option', $form_state->getValue('domains_option'))
312       ->set('domains_list', $form_state->getValue('domains_list'))
313       ->set('exclude_paths', $form_state->getValue('exclude_paths'))
314       ->save();
315
316     parent::submitForm($form, $form_state);
317   }
318
319 }