Drupal 10 Help: Drupal 10 Upkeep and Help Service <a href="/blog/eu-cookie-implementation-in-Drupal 10-8">EU cookie implementation in 8</a>

On this weblog we’re going to discuss one such performance. We had been required to implement EU cookie performance in 8. EU cookie is obligatory for all European web sites. There are lots of options obtainable on-line to implement this function, I used JavaScript for this process and it labored like a attraction. That is the way it works.   First we have to get Drupal Developer required JavaScript code from https Drupal 10 Upkeep and Help Service//cookieconsent.insites.com/ as per our required consent popup. As soon as Drupal Developer code is retrieved, we have to convert this plain JavaScript into Drupal Developer 8 format.   For our implementation we would have liked to create a configuration kind for settings. Together with that we had been additionally required so as to add configuration to vary scroll size after which Drupal Developer cookie popup would disappear. To make it EU particular we needed to get Drupal Developer continent code from Drupal Developer API.   Step 1 Drupal 10 Upkeep and Help Service Create kind.php in src/kind containing type of settings in your Drupal 10 module Drupal 10 Upkeep and Help Service <?php /** * @file * Comprises Drupal 10 moduleFormSettingsForm. */ namespace Drupal 10 moduleForm; use CoreFormConfigFormBase; use CoreFormFormStateInterface; /** * Kind builder for Drupal Developer Drupal 10 module primary settings kind. */ class SettingsForm extends ConfigFormBase { /** * {@inheritdoc} */ public operate getFormId() { return ‘Drupal 10 module_settings_form’; } /** * {@inheritdoc} */ protected operate getEditableConfigNames() { return [‘Drupal 10 module.settings’]; } /** * {@inheritdoc} */ public operate buildForm(array $kind, FormStateInterface $form_state) { $config = $this->config(‘Drupal 10 module .settings’); $kind[‘activate’] = array( ‘#kind’ => ‘checkbox’, ‘#title’ => $this->t(‘Activate Cookie Consent’), ‘#default_value’ => $config->get(‘activate’), ‘#description’ => $this->t(‘Cookie consent notification for web site might be enabled or disabled from right here.’), ); $kind[‘eu_consent_pop_msg’] = array( ‘#kind’ => ‘textarea’, ‘#title’ => $this->t(‘Set message for cookie consent popup’), ‘#default_value’ => $config->get(‘eu_consent_pop_msg’), ‘#description’ => $this->t(‘Cookie consent pop up message might be set from right here.’), ); $kind[‘eu_consent_pop_scroll’] = array( ‘#kind’ => ‘textfield’, ‘#title’ => $this->t(‘Set scroll size (pixels)’), ‘#default_value’ => $config->get(‘eu_consent_pop_scroll’), ‘#description’ => $this->t(‘Set after scrolling what number of pixels Drupal Developer popup ought to disappear. Ex Drupal 10 Upkeep and Help Service 500’), ); return mother or father Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicebuildForm($kind, $form_state); } /** * {@inheritdoc} */ public operate submitForm(array &$kind, FormStateInterface $form_state) { mother or father Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicesubmitForm($kind, $form_state); $config = $this->config(‘Drupal 10 module.settings’); $config->set(‘activate’, $form_state->getValue(‘activate’)); $config->set(‘eu_consent_pop_msg’, $form_state->getValue(‘eu_consent_pop_msg’)); $config->set(‘eu_consent_pop_scroll’, $form_state->getValue(‘eu_consent_pop_scroll’)); $config->save(); } }   Step 2 Drupal 10 Upkeep and Help Service Outline a controller in Drupal 10 module/src/controller. This can have features that retrieve nation code from API. Drupal Development Company config setting determines if EU cookie is enabled or not Drupal 10 Upkeep and Help Service <?php /** * @file * Comprises Drupal 10 moduleControllerEuCookie. */ namespace Drupal 10 moduleController; use CoreControllerControllerBase; use SymfonyComponentHttpFoundationJsonResponse; use GuzzleHttpClient; /** * Class EuCookie. * * @bundle Drupal 10 moduleController */ class EuCookie extends ControllerBase { /** * Autocomplete. */ public operate continent() { $ip_eu_consent = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicerequest()->getClientIp(); $config_status = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceconfig(‘Drupal 10 module .settings’)->get(‘activate’); if($config_status == 1) { $continent_code[‘consent’] = $this Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceeucookie_get_country_code($ip_eu_consent); if ( Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicecurrentUser()->isAnonymous()) { $continent_code[‘isanon’] = true; } else { $continent_code[‘isanon’] = false; } } return JsonResponse Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicecreate($continent_code); } /** * Get continent code from exterior free service for eu consent. */ operate eucookie_get_country_code($ip_eu_consent) { attempt { $uri = ‘http Drupal 10 Upkeep and Help Service//www.geoplugin.internet/json.gp?ip=’ . $ip_eu_consent; $shopper = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicehttpClient([‘base_url’ => $uri]); $request = $client->request(‘GET’, $uri, [‘timeout’ => 5, ‘headers’ => [‘Accept’ => ‘application/json’]]); if ($request->getStatusCode() == 200) { $response = json_decode($request->getBody()); if (empty($response)) { return []; } else { return ($response->geoplugin_continentCode); } } else { return []; } } catch (GuzzleHttpExceptionClientException $e) { $message = $e->getMessage() . ‘. Ensure you supplied appropriate IP to get nation code .’; Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicelogger(‘Drupal 10 module_get_country_code’)->discover($message); return []; } } }   Step 3 Drupal 10 Upkeep and Help Service Add hook web page attachment alter to get variables outlined in config and ship them to js Drupal 10 Upkeep and Help Service <?php operate hook_page_attachments_alter(&$web page) { $ip_eu_consent = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicerequest()->getClientIp(); $config_status = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceconfig(‘Drupal 10 module.settings’)->get(‘activate’); $mesage_consent = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceconfig(‘Drupal 10 module.settings’)->get(‘eu_consent_pop_msg’); $scroll_length = Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceconfig(‘Drupal 10 module.settings’)->get(‘eu_consent_pop_scroll’); if($config_status == 1) {     $web page[‘#attached’][‘library’][] = ‘path to library’; $web page[‘#attached’][‘Drupal 10Settings’][‘message_eu_consent’] = $mesage_consent; $web page[‘#attached’][‘Drupal 10Settings’][‘eu_consent_pop_scroll’] = $scroll_length; } } ?> Step 4 Drupal 10 Upkeep and Help Service Routing file, with path to retrieve settings kind and get Drupal Developer continent code by way of ajax Drupal 10 Upkeep and Help Service Drupal 10 module.admin_settings Drupal 10 Upkeep and Help Service path Drupal 10 Upkeep and Help Service ‘/admin/config/eucookie’ defaults Drupal 10 Upkeep and Help Service _form Drupal 10 Upkeep and Help Service ‘Drupal 10 moduleFormSettingsForm’ _title Drupal 10 Upkeep and Help Service ‘EUCookie settings’ necessities Drupal 10 Upkeep and Help Service _permission Drupal 10 Upkeep and Help Service ‘administer web site configuration’ Drupal 10 module.ajax_continent Drupal 10 Upkeep and Help Service path Drupal 10 Upkeep and Help Service ‘/ajax/continent’ defaults Drupal 10 Upkeep and Help Service _controller Drupal 10 Upkeep and Help Service ‘Drupal 10 moduleControllerEuCookie Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Servicecontinent’ _title Drupal 10 Upkeep and Help Service ‘ajax continent’ necessities Drupal 10 Upkeep and Help Service _permission Drupal 10 Upkeep and Help Service ‘entry content material’ Step 5 Drupal 10 Upkeep and Help Service Add js beneath in customized/js as situation for scroll, get outlined variables from config and show EU cookie popup. (operate($) { ‘use strict’; .behaviors.Drupal 10 moduleeuconsent = { connect Drupal 10 Upkeep and Help Service operate(context, settings) { $(doc).scroll(operate() { var scroll_len = Drupal 10Settings.eu_consent_pop_scroll; var scrollBottom = $(window).scrollTop() + $(window).peak(); scrollBottom > scroll_len ? $(‘.cc-window’).fadeOut() Drupal 10 Upkeep and Help Service $(‘.cc-window’).fadeIn(); }); var msg = Drupal 10Settings..message_eu_consent; $.ajax({url Drupal 10 Upkeep and Help Service “/ajax/continent”, success Drupal 10 Upkeep and Help Service operate(consequence){ if (consequence[‘consent’] == ‘EU’ && consequence[‘isanon’]==true) { window.cookieconsent.initialise({ palette Drupal 10 Upkeep and Help Service { popup Drupal 10 Upkeep and Help Service { background Drupal 10 Upkeep and Help Service ‘#252e39’ }, button Drupal 10 Upkeep and Help Service { background Drupal 10 Upkeep and Help Service ‘#14a7d0’ } }, content material Drupal 10 Upkeep and Help Service { message Drupal 10 Upkeep and Help Service msg, dismiss Drupal 10 Upkeep and Help Service ‘Shut’, hyperlink Drupal 10 Upkeep and Help Service ‘Present Cookie Coverage’ } }); } }}); } }; }(jQuery));   Step 6 Drupal 10 Upkeep and Help Service Exterior js must be outlined in libraries.yml which creates popup Drupal 10 module_lib Drupal 10 Upkeep and Help Service model Drupal 10 Upkeep and Help Service v3.14 css Drupal 10 Upkeep and Help Service theme Drupal 10 Upkeep and Help Service //cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.1/cookieconsent.min.css Drupal 10 Upkeep and Help Service { kind Drupal 10 Upkeep and Help Service exterior } js Drupal 10 Upkeep and Help Service //cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.1/cookieconsent.min.js Drupal 10 Upkeep and Help Service { kind Drupal 10 Upkeep and Help Service exterior, scope Drupal 10 Upkeep and Help Service footer } js/Drupal 10 module.js Drupal 10 Upkeep and Help Service { scope Drupal 10 Upkeep and Help Service footer } dependencies Drupal 10 Upkeep and Help Service – core/jquery – core/Drupal 10 – core/Drupal 10Settings   And therefore Drupal Developer EU Cookie performance shall be applied in your 8 web site. Please do remark when you have any suggestions or if this code doesn’t be just right for you.   Drupal 10 Growth and Help

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal 10 Help: Drupal 10 Upkeep and Help Service <a href="/blog/eu-cookie-implementation-in-Drupal 10-8">EU cookie implementation in 8</a>

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.