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.

Command icon 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

scontzen created an issue. See original summary.

kalash-j’s picture

Assigned: Unassigned » kalash-j

i am working on it

_shy made their first commit to this issue’s fork.

_shy’s picture

Assigned: kalash-j » Unassigned
Status: Active » Needs review

guido_s made their first commit to this issue’s fork.

  • guido_s committed 07c884aa on 1.0.x authored by _shy
    Issue #3519107 by guido_s, scontzen: PHP 8.1+ Deprecation warning:...
guido_s’s picture

Status: Needs review » Fixed

I 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.

guido_s’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.