diff --git a/config/install/eu_cookie_compliance.settings.yml b/config/install/eu_cookie_compliance.settings.yml index 503304ea8bd8ad3eec6a609ea81dfccc2a3178f3..f4f1d25f00479bf589c215aed4870de032d238d9 100644 --- a/config/install/eu_cookie_compliance.settings.yml +++ b/config/install/eu_cookie_compliance.settings.yml @@ -26,6 +26,10 @@ popup_hide_button_message: 'Hide' popup_info: value: '

We use cookies on this site to enhance your user experience

By clicking any link on this page you are giving your consent for us to set cookies.

' format: 'restricted_html' +mobile_popup_info: + value: '

We use cookies on this site to enhance your user experience

By tapping any link on this page you are giving your consent for us to set cookies.

' + format: 'restricted_html' +mobile_breakpoint: 768 popup_link: '' popup_link_new_window: true popup_position: false diff --git a/config/schema/eu_cookie_compliance.schema.yml b/config/schema/eu_cookie_compliance.schema.yml index 0098b0ee74dbfedf0cc416ec1519548d8b6a578a..891aea359a387aee480c1d53c49e5e01aea50bb7 100644 --- a/config/schema/eu_cookie_compliance.schema.yml +++ b/config/schema/eu_cookie_compliance.schema.yml @@ -38,6 +38,19 @@ eu_cookie_compliance.settings: format: type: string label: 'Popup message - Format' + mobile_popup_info: + type: mapping + label: 'Mobile popup message - requests consent' + mapping: + value: + type: text + label: 'Mobile popup message - Value' + format: + type: string + label: 'Mobile popup message - Format' + mobile_breakpoint: + type: integer + label: 'Mobile breakpoint' popup_agreed_enabled: type: boolean label: 'Enable thank you message' diff --git a/eu_cookie_compliance.module b/eu_cookie_compliance.module index 620cf119257cd0bd1cb794ee3062212ab7e21cae..cd5d6307015bc1e409a126a9185da9baa5a92f85 100644 --- a/eu_cookie_compliance.module +++ b/eu_cookie_compliance.module @@ -140,6 +140,13 @@ function eu_cookie_compliance_page_attachments(&$attachments) { '#agree_button' => $config->get('popup_agree_button_message'), '#disagree_button' => $config->get('popup_disagree_button_message'), ); + $mobile_popup_text_info = str_replace(array("\r", "\n"), '', $config->get('mobile_popup_info.value')); + $mobile_html_info = array( + '#theme' => 'eu_cookie_compliance_popup_info', + '#message' => check_markup($mobile_popup_text_info, $config->get('popup_info.format'), FALSE), + '#agree_button' => $config->get('popup_agree_button_message'), + '#disagree_button' => $config->get('popup_disagree_button_message'), + ); $html_agreed = array( '#theme' => 'eu_cookie_compliance_popup_agreed', '#message' => check_markup($popup_text_agreed, $config->get('popup_agreed.format'), FALSE), @@ -160,6 +167,7 @@ function eu_cookie_compliance_page_attachments(&$attachments) { } $html_info = trim(\Drupal::service('renderer')->renderRoot($html_info)->__toString()); + $mobile_html_info = trim(\Drupal::service('renderer')->renderRoot($mobile_html_info)->__toString()); $html_agreed = trim(\Drupal::service('renderer')->renderRoot($html_agreed)->__toString()); if ($was_debugging) { @@ -183,6 +191,9 @@ function eu_cookie_compliance_page_attachments(&$attachments) { 'popup_clicking_confirmation' => $config->get('popup_clicking_confirmation'), 'popup_scrolling_confirmation' => $config->get('popup_scrolling_confirmation'), 'popup_html_info' => $config->get('popup_enabled') ? $html_info : FALSE, + 'use_mobile_message' => !empty($config->get('use_mobile_message')) ? $config->get('use_mobile_message') : FALSE, + 'mobile_popup_html_info' => $config->get('popup_enabled') ? $mobile_html_info : FALSE, + 'mobile_breakpoint' => !empty($config->get('mobile_breakpoint')) ? $config->get('mobile_breakpoint') : '768', 'popup_html_agreed' => $config->get('popup_agreed_enabled') ? $html_agreed : FALSE, 'popup_height' => !empty($config->get('popup_height')) ? $config->get('popup_height') : 'auto', 'popup_width' => !empty($config->get('popup_width')) ? $config->get('popup_width') : '100%', diff --git a/js/eu_cookie_compliance.js b/js/eu_cookie_compliance.js index a7f66ce9f4ed0abbc3f917454ef9a703533d93bd..00c6099081f65b78025bfae6cf7e9e40ab332b2a 100644 --- a/js/eu_cookie_compliance.js +++ b/js/eu_cookie_compliance.js @@ -59,7 +59,13 @@ Drupal.eu_cookie_compliance.changeStatus(next_status); }); - Drupal.eu_cookie_compliance.createPopup(settings.popup_html_info); + // Detect mobile here and use mobile_popup_html_info, if we have a mobile device. + if (window.matchMedia('(max-width: ' + settings.mobile_breakpoint + 'px)').matches && settings.use_mobile_message) { + Drupal.eu_cookie_compliance.createPopup(settings.mobile_popup_html_info); + } + else { + Drupal.eu_cookie_compliance.createPopup(settings.popup_html_info); + } } else if (status === 1) { Drupal.eu_cookie_compliance.createPopup(settings.popup_html_agreed); diff --git a/src/Form/EuCookieComplianceConfigForm.php b/src/Form/EuCookieComplianceConfigForm.php index 43449bce6fcd22cb308ac7011d93ee5e39236d11..cac9c55c6dd8640a11c8af7b0194f860dd34e661 100644 --- a/src/Form/EuCookieComplianceConfigForm.php +++ b/src/Form/EuCookieComplianceConfigForm.php @@ -117,6 +117,40 @@ class EuCookieComplianceConfigForm extends ConfigFormBase { '#format' => !empty($config->get('popup_info.format')) ? $config->get('popup_info.format') : $default_filter_format, ); + $form['popup_message']['use_mobile_message'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Use a different message for mobile phones'), + '#default_value' => ($config->get('mobile_popup_info.value') != ''), + ); + + $form['popup_message']['container'] = array( + '#type' => 'container', + '#states' => array('visible' => array('input[name="use_mobile_message"]' => array('checked' => true))), + ); + + $form['popup_message']['container']['mobile_popup_info'] = array( + '#type' => 'text_format', + '#title' => $this->t('Mobile popup message - requests consent'), + '#default_value' => $config->get('mobile_popup_info.value'), + '#required' => FALSE, + '#format' => !empty($config->get('mobile_popup_info.format')) ? $config->get('mobile_popup_info.format') : $default_filter_format, + ); + + $form['popup_message']['mobile_breakpoint'] = array( + '#type' => 'number', + '#title' => $this->t('Mobile breakpoint'), + '#default_value' => !empty($config->get('mobile_breakpoint')) ? $config->get('mobile_breakpoint') : '768', + '#field_suffix' => ' ' . $this->t('pixels'), + '#size' => 4, + '#maxlength' => 4, + '#required' => FALSE, + '#description' => $this->t('The mobile message will be used when the window width is below or equal to the given value.'), + '#states' => array( + "visible" => array( + "input[name='use_mobile_message']" => array("checked" => TRUE)), + ), + ); + $form['popup_message']['popup_agree_button_message'] = array( '#type' => 'textfield', '#title' => $this->t('Agree button label'), @@ -348,6 +382,10 @@ class EuCookieComplianceConfigForm extends ConfigFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { + if (trim($form_state->getValue('mobile_popup_info')['value']) == '') { + $form_state->setValue('use_mobile_message', FALSE); + } + $this->config('eu_cookie_compliance.settings') ->set('domain', $form_state->getValue('domain')) ->set('popup_enabled', $form_state->getValue('popup_enabled')) @@ -357,6 +395,9 @@ class EuCookieComplianceConfigForm extends ConfigFormBase { ->set('popup_agree_button_message', $form_state->getValue('popup_agree_button_message')) ->set('popup_disagree_button_message', $form_state->getValue('popup_disagree_button_message')) ->set('popup_info', $form_state->getValue('popup_info')) + ->set('use_mobile_message', $form_state->getValue('use_mobile_message')) + ->set('mobile_popup_info', $form_state->getValue('use_mobile_message') ? $form_state->getValue('mobile_popup_info') : '') + ->set('mobile_breakpoint', $form_state->getValue('mobile_breakpoint')) ->set('popup_agreed_enabled', $form_state->getValue('popup_agreed_enabled')) ->set('popup_hide_agreed', $form_state->getValue('popup_hide_agreed')) ->set('popup_find_more_button_message', $form_state->getValue('popup_find_more_button_message'))