Commit 4549b7ac authored by Sven Berg Ryen's avatar Sven Berg Ryen
Browse files

Issue #3278055 by svenryen: Switch to Coloris color picker

parent 9b963b2d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ services:
        composer config repositories.tugboat vcs $TUGBOAT_ROOT
        # Now we can require this module, specifing the branch name we created
        # above that uses the $TUGBOAT_REPO_ID environment variable.
        composer require drupal/eu_cookie_compliance:dev-$TUGBOAT_REPO_ID
        composer require drupal/coloris:1.0.x-dev drupal/eu_cookie_compliance:dev-$TUGBOAT_REPO_ID
        # Install Drupal on the site.
        vendor/bin/drush \
          --yes \
+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ INSTALLATION

5. If you want to customize the banner background and text color, either type
   in the hex values or simply install
   http://drupal.org/project/jquery_colorpicker.
   http://drupal.org/project/coloris.

6. If you want to theme your banner, override the templates in your theme.

+2 −22
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ function eu_cookie_compliance_help($route_name, RouteMatchInterface $route_match
      $output .= "<label>-&nbsp;</label>" . t("there is also a 'display eu cookie compliance banner' permission that helps you show the banner to the roles you desire.");
      $output .= '<li>' . t('You may want to create a page that would explain how your site uses cookies. Alternatively, if you have a privacy policy, you can link the banner to that page (see next step).') . '</li>';
      $output .= '<li>' . t('Go to the admin/config/system/eu-cookie-compliance page to configure and enable the banner.') . '</li>';
      $output .= '<li>' . t('If you want to customize the banner background and text color, either type in the hex values or simply install http://drupal.org/project/jquery_colorpicker.') . '</li>';
      $output .= '<li>' . t('If you want to customize the banner background and text color, either type in the hex values or simply install http://drupal.org/project/coloris.') . '</li>';
      $output .= '<li>' . t('If you want to theme your banner override the themes in the template file.') . '</li>';
      $output .= '<li>' . t('If you want to show the message in EU countries only, install the Smart IP module: http://drupal.org/project/smart_ip and enable the option on the admin page.') . '</li></ol>';
      $output .= '<p><b>' . t('NOTICE: The module does not audit your cookies nor does it prevent cookies from being set.') . '</b></p>';
@@ -722,8 +722,7 @@ function eu_cookie_compliance_form_alter(&$form, FormStateInterface $form_state,
 *   Form State Interface.
 */
function eu_cookie_compliance_validate_hex(array $element, FormStateInterface $form_state) {
  // Handle version 2 of jquery colorpicker.
  if ((_eu_cookie_compliance_get_jquery_colorpicker_version() === 2)) {
  if (\Drupal::moduleHandler()->moduleExists('coloris')) {
    $element['#value'] = substr($element['#value'], 1);
  }

@@ -732,25 +731,6 @@ function eu_cookie_compliance_validate_hex(array $element, FormStateInterface $f
  }
}

/**
 * Get the current version of jQuery colorpicker, if installed.
 *
 * Hopefully they will not release a version 3 any time soon. :)
 *
 * @return int
 *   Major version of jQuery Colorpicker, or 0 if not installed.
 */
function _eu_cookie_compliance_get_jquery_colorpicker_version() {
  if (\Drupal::service('module_handler')->moduleExists('jquery_colorpicker')) {
    $info = \Drupal::service('extension.list.module')
      ->getExtensionInfo('jquery_colorpicker');
    if ($info) {
      return (strpos($info['version'], '-2.') !== FALSE) ? 2 : 1;
    }
  }
  return 0;
}

/**
 * Check if the user is in the EU.
 */
+48 −11
Original line number Diff line number Diff line
@@ -747,15 +747,18 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {

    $form_color_picker_type = 'textfield';

    if ($this->moduleHandler->moduleExists('jquery_colorpicker')) {
      $form_color_picker_type = 'jquery_colorpicker';
      $jquery_colorpicker_version = _eu_cookie_compliance_get_jquery_colorpicker_version();
      $bg_color = ($jquery_colorpicker_version === 2) ? '#' . $config->get('popup_bg_hex') : $config->get('popup_bg_hex');
      $text_color = ($jquery_colorpicker_version === 2) ? '#' . $config->get('popup_text_hex') : $config->get('popup_text_hex');
    if ($this->moduleHandler->moduleExists('coloris')) {
      $form_color_picker_type = 'coloriswidget';
      $bg_color = '#' . $config->get('popup_bg_hex');
      $text_color = '#' . $config->get('popup_text_hex');
      $text_color_description = $this->t('Change the text color of the banner.');
      $bg_color_description = $this->t('Change the background color of the banner.');
    }
    else {
      $bg_color = $config->get('popup_bg_hex');
      $text_color = $config->get('popup_text_hex');
      $text_color_description = $this->t('Change the text color of the banner. Provide HEX value without the #.');
      $bg_color_description = $this->t('Change the background color of the banner. Provide HEX value without the #.');
    }

    $form['appearance']['containing_element'] = [
@@ -788,11 +791,12 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
      '#description' => $this->t('This may be useful if you want the banner to share the button style of your theme. Note that you will have to configure values like the banner width, text color and background color in your CSS file.'),
    ];

    if ($form_color_picker_type === 'textfield') {
      $form['appearance']['popup_text_hex'] = [
        '#type' => $form_color_picker_type,
        '#title' => $this->t('Text color'),
        '#default_value' => $text_color,
      '#description' => $this->t('Change the text color of the banner. Provide HEX value without the #.'),
        '#description' => $text_color_description,
        '#element_validate' => ['eu_cookie_compliance_validate_hex'],
        '#states' => [
          'visible' => [
@@ -805,7 +809,7 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
        '#type' => $form_color_picker_type,
        '#title' => $this->t('Background color'),
        '#default_value' => $bg_color,
      '#description' => $this->t('Change the background color of the banner. Provide HEX value without the #.'),
        '#description' => $bg_color_description,
        '#element_validate' => ['eu_cookie_compliance_validate_hex'],
        '#states' => [
          'visible' => [
@@ -813,6 +817,38 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
          ],
        ],
      ];
    }
    else {
      $form['appearance']['popup_text_hex'] = [
        '#type' => $form_color_picker_type,
        '#title' => $this->t('Text color'),
        '#default_value' => $text_color,
        '#description' => $text_color_description,
        '#element_validate' => ['eu_cookie_compliance_validate_hex'],
        '#data_theme' => 'polaroid',
        '#alpha' => FALSE,
        '#states' => [
          'visible' => [
            "input[name='use_bare_css']" => ['checked' => FALSE],
          ],
        ],
      ];

      $form['appearance']['popup_bg_hex'] = [
        '#type' => $form_color_picker_type,
        '#title' => $this->t('Background color'),
        '#default_value' => $bg_color,
        '#description' => $bg_color_description,
        '#element_validate' => ['eu_cookie_compliance_validate_hex'],
        '#data_theme' => 'polaroid',
        '#alpha' => FALSE,
        '#states' => [
          'visible' => [
            "input[name='use_bare_css']" => ['checked' => FALSE],
          ],
        ],
      ];
    }

    $form['appearance']['popup_height'] = [
      '#type' => 'number',
@@ -1232,10 +1268,11 @@ class EuCookieComplianceConfigForm extends ConfigFormBase {
      $this->messenger()->addWarning($this->t('The module weight could not be set because the <code>config_readonly</code> module is enabled. To allow setting of module weight, please add <code>core.extension</code> to the <code>config_readonly</code> allowlist.'));
    }

    // Handle version 2 of jQuery colorpicker.
    $jquery_colorpicker_version = _eu_cookie_compliance_get_jquery_colorpicker_version();
    $bg_color = ($jquery_colorpicker_version === 2) ? substr($form_state->getValue('popup_bg_hex'), 1) : $form_state->getValue('popup_bg_hex');
    $text_color = ($jquery_colorpicker_version === 2) ? substr($form_state->getValue('popup_text_hex'), 1) : $form_state->getValue('popup_text_hex');

    if ($this->moduleHandler->moduleExists('coloris')) {
      $bg_color = substr($form_state->getValue('popup_bg_hex'), 1);
      $text_color = substr($form_state->getValue('popup_text_hex'), 1);
    }

    // Save settings.
    $config = $this->config('eu_cookie_compliance.settings');