3 namespace Drupal\eu_cookie_compliance\Form;
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;
13 * Provides settings for eu_cookie_compliance module.
15 class EuCookieComplianceConfigForm extends ConfigFormBase {
20 * @var \Drupal\Core\Path\PathValidatorInterface
22 protected $pathValidator;
25 * The request context.
27 * @var \Drupal\Core\Routing\RequestContext
29 protected $requestContext;
32 * Constructs an EuCookieComplianceConfigForm object.
34 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35 * The factory for configuration objects.
36 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
38 * @param \Drupal\Core\Routing\RequestContext $request_context
39 * The request context.
41 public function __construct(ConfigFactoryInterface $config_factory, PathValidatorInterface $path_validator, RequestContext $request_context) {
42 parent::__construct($config_factory);
44 $this->pathValidator = $path_validator;
45 $this->requestContext = $request_context;
51 public static function create(ContainerInterface $container) {
53 $container->get('config.factory'),
54 $container->get('path.validator'),
55 $container->get('router.request_context')
62 public function getFormId() {
63 return 'eu_cookie_compliance_config_form';
69 protected function getEditableConfigNames() {
71 'eu_cookie_compliance.settings',
78 public function buildForm(array $form, FormStateInterface $form_state) {
80 $config = $this->config('eu_cookie_compliance.settings');
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.'),
89 $form['eu_cookie_compliance'] = array(
91 '#title' => $this->t('Settings'),
95 $form['eu_cookie_compliance']['popup_enabled'] = array(
96 '#type' => 'checkbox',
97 '#title' => $this->t('Enable popup'),
98 '#default_value' => $config->get('popup_enabled'),
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.'),
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'),
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'),
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'),
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'),
136 '#format' => !empty($config->get('popup_info.format')) ? $config->get('popup_info.format') : filter_default_format(),
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'),
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.'),
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'),
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'),
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'] : '',
173 '#format' => !empty($config->get('popup_agreed')['format']) ? $config->get('popup_agreed')['format'] : filter_default_format(),
176 $form['eu_cookie_compliance']['popup_link'] = array(
178 '#title' => $this->t('Privacy policy link'),
179 '#default_value' => $config->get('popup_link'),
180 '#maxlength' => 1024,
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://.'),
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'),
191 $form['eu_cookie_compliance']['popup_height'] = array(
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'),
198 '#required' => FALSE,
199 '#description' => $this->t('Enter an integer value for a desired height in pixels or leave empty for automatically adjusted height'),
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 %'),
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%'),
213 $form['eu_cookie_compliance']['popup_delay'] = array(
215 '#title' => $this->t('Popup time delay in seconds'),
216 '#default_value' => $config->get('popup_delay'),
217 '#field_suffix' => ' ' . t('seconds'),
223 $form_color_picker_type = 'textfield';
225 if (\Drupal::moduleHandler()->moduleExists('jquery_colorpicker')) {
226 $form_color_picker_type = 'jquery_colorpicker';
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'),
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'),
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'),
250 $form['eu_cookie_compliance']['domains_option'] = array(
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.'),
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.'),
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(
270 '%blog-wildcard' => '/blog/*',
271 '%front' => '<front>'
275 return parent::buildForm($form, $form_state);
281 public function validateForm(array &$form, FormStateInterface $form_state) {
282 parent::validateForm($form, $form_state);
284 // @TODO Validate other form elements settings.
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'))
316 parent::submitForm($form, $form_state);