Problem/Motivation
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.
Steps to reproduce
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()`.
Proposed resolution
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.
Remaining tasks
- Apply the fix in the `cookies_addons_views.module` file
- Test with PHP 8.1 and PHP 8.2 to ensure no more deprecation warnings appear
- Test with various cookie states to ensure functionality remains intact
User interface changes
None.
API changes
None.
Data model changes
None.
Issue fork cookies_addons-3519107
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
kalash-j commentedi am working on it
Comment #5
_shyComment #8
guido_sI also checked the project for php compatibility with phpcs
phpcs --extensions=php,module,install,inc,theme,engine --standard=PHPCompatibility --runtime-set testVersion 8.4 cookies_addons/I checked for all versions from 7.4 to 8.4 and there were no errors or warnings.
So if there still are any issues with php compatibility it will be a runtime error due to wrong parameters, as phpcs can't detect those.
I'll merge it and add a new tagged release.
Comment #9
guido_s