Scroll
Drupal: Wie man jQuery Cookie durch Javascript Cookie ersetzt
Drupal 9 und höher verwendet die Javascript Cookie-Bibliothek anstelle von jQuery Cookie:
https://www.drupal.org/node/3104677
https://www.drupal.org/node/3322720
https://www.drupal.org/node/3296086
Sie finden viele Patches für Contrib-Module, zum Beispiel:
https://www.drupal.org/project/eu_cookie_compliance/issues/3194270
Wenn Sie Ihre Website auf Drupal 10 aktualisieren möchten, müssen Sie die jQuery Cookie-Bibliothek ersetzen:
my_module.libraries.yml
my_library:
js:
js/my_library.js: {}
dependencies:
- core/jquery
- core/jquery.cookie
- core/drupal
ersetzt durch:
my_library:
js:
js/my_library.js: {}
dependencies:
- core/drupal
- core/js-cookie
Und aktualisieren Sie Ihren JavaScript-Code:
Drupal.behaviors.myModule = {
attach: () => {
// Cookie setzen.
Cookies.set('cutest', 'red panda');
// Cookie auslesen.
const myCookieValue = Cookies.get('cutest');
// Cookie löschen.
Cookies.remove('cutest');
// Speichern und Auslesen als JSON-Objekt. Die Verwendung der getJSON-Methode sollte vermieden werden, da diese in js-cookie 3.0.0 veraltet sein wird.
Cookies.set('cutest', JSON.stringify({ animal: 'red panda' }));
const cutest = JSON.parse(Cookies.get('cutest'));
},
};