- Issue created by @scontzen
When using the Cookies Addons Views module with PHP 8.1 or higher, a deprecation warning appears in the logs:
Deprecated function: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in _cookies_addons_views_is_restricted() (line 110 of modules/contrib/cookies_addons/modules/cookies_addons_views/cookies_addons_views.module)
The issue occurs because the module passes a potential null value directly to `json_decode()`. In PHP 8.1+, passing null to the first parameter of `json_decode()` is deprecated and will eventually become an error in future PHP versions.
1. Install and enable the Cookies Addons Views module
2. Use PHP 8.1 or higher
3. Visit any page where views are rendered
4. Check the error logs for deprecation warnings
The issue occurs in the `_cookies_addons_views_is_restricted()` function where the code attempts to decode the "cookiesjsr" cookie value without checking if it exists:
$cookiesJson = \Drupal::request()->cookies->get('cookiesjsr');
$cookies = json_decode($cookiesJson, TRUE);
If the "cookiesjsr" cookie doesn't exist, the `$cookiesJson` variable will be null, triggering the deprecation warning when passed to `json_decode()`.
Check if the cookie value exists before attempting to decode it:
$cookiesJson = \Drupal::request()->cookies->get('cookiesjsr');
$cookies = !empty($cookiesJson) ? json_decode($cookiesJson, TRUE) : [];
This ensures that `json_decode()` is only called with a valid string value and provides an empty array as a fallback when the cookie is not set.
None.
None.
None.
Active
1.2
Cookies Addons Views