Scroll
Drupal: How to rewrite jQuery Cookie with Javascript Cookie
Drupal 9 and above uses Javascript Cookie library instead of jQuery Cookie:
https://www.drupal.org/node/3104677
https://www.drupal.org/node/3322720
https://www.drupal.org/node/3296086
You can find a lot of patches for contrib modules, for example:
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'));
},
};