logo

Paleta - Deixe colorido🎨

Palette — Construtor visual de páginas. Não precisa ser designer.

Demonstração ao vivo Baixar Palette

Scroll

Drupal: como reescrever jQuery Cookie com JavaScript Cookie

16/01/2024, by Ivan

O Drupal 9 e superiores usam a biblioteca Javascript Cookie em vez do jQuery Cookie (links mantidos):

https://www.drupal.org/node/3104677

https://www.drupal.org/node/3322720

https://www.drupal.org/node/3296086

Você pode encontrar muitos patches para módulos contrib, por exemplo:

https://www.drupal.org/project/eu_cookie_compliance/issues/3194270

If you need to upgrade site to Drupal 10, you will need to replace jQuery Cookie library:
my_module.libraries.yml

my_library:
  js:
    js/my_library.js: {}
  dependencies:
    - core/jquery
    - core/jquery.cookie
    - core/drupal

with:

my_library:
  js:
    js/my_library.js: {}
  dependencies:
    - core/drupal
    - core/js-cookie

And update your javascript code:

 Drupal.behaviors.myModule = {
    attach: () => {
      // Set a cookie.
      Cookies.set('cutest', 'red panda');
      // Retrieve a cookie.
      const myCookieValue = Cookies.get('cutest');
      // Remove a cookie.
      Cookies.remove('cutest');
      // Store and retrieve as a JSON object. Use of the getJSON method should be avoided as that will be deprecated in js-cookie 3.0.0.
      Cookies.set('cutest', JSON.stringify({ animal: 'red panda' }));
      const cutest = JSON.parse(Cookies.get('cutest'));
    },
  };