Problem/Motivation

PHP 8.1, Drupal 9.3.2 returns

Deprecated function: Return type of Drupal\google_analytics\Component\Render\GoogleAnalyticsJavaScriptSnippet::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in include() (line 10 of modules/contrib/google_analytics/src/Component/Render/GoogleAnalyticsJavaScriptSnippet.php).

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

VladimirAus created an issue. See original summary.

vladimiraus’s picture

Status: Active » Needs review
patrickmichael’s picture

I am experiencing the same. Drupal 9.3.2, PHP 8.1.1, Google Analytics 4.0.0

vladimiraus’s picture

StatusFileSize
new992.66 KB

Merge request was opened here: https://git.drupalcode.org/issue/google_analytics-3258588/-/merge_reques...
But cannot use Open merge request button

kalpaitch’s picture

Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community

Ok this is quite an important issue for me as it blocks most of my ajax requests (with a verbose error_level), so I'm changing the priority.

I've tested this particular patch and it all works very well for me, I can't see any issues or complications would come from merging this.

patrickmichael’s picture

I have applied the following to src/Component/Render/GoogleAnalyticsJavaScriptSnippet.php

  /**
   * {@inheritdoc}
   */
  public function jsonSerialize(): mixed {
    return $this->__toString();
  }

I have cleared cache
I still see the following in my logs

Deprecated function: Return type of Drupal\google_analytics\Component\Render\GoogleAnalyticsJavaScriptSnippet::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in include() (line 10 of /web/modules/contrib/google_analytics/src/Component/Render/GoogleAnalyticsJavaScriptSnippet.php)

#0 /web/core/includes/bootstrap.inc(346): _drupal_error_handler_real()
#1 /web/modules/contrib/google_analytics/src/Component/Render/GoogleAnalyticsJavaScriptSnippet.php(10): _drupal_error_handler()
#2 /vendor/composer/ClassLoader.php(571): include('...')
#3 /vendor/composer/ClassLoader.php(428): Composer\Autoload\includeFile()
#4 [internal function]: Composer\Autoload\ClassLoader->loadClass()
#5 /web/core/lib/Drupal/Core/Cache/DatabaseBackend.php(167): unserialize()
#6 /web/core/lib/Drupal/Core/Cache/DatabaseBackend.php(122): Drupal\Core\Cache\DatabaseBackend->prepareItem()
#7 /web/core/lib/Drupal/Core/Cache/DatabaseBackend.php(92): Drupal\Core\Cache\DatabaseBackend->getMultiple()
#8 /web/core/modules/page_cache/src/StackMiddleware/PageCache.php(306): Drupal\Core\Cache\DatabaseBackend->get()
#9 /web/core/modules/page_cache/src/StackMiddleware/PageCache.php(124): Drupal\page_cache\StackMiddleware\PageCache->get()
#10 /web/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup()
#11 /web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#12 /web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#13 /vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#14 /web/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle()
#15 /web/index.php(19): Drupal\Core\DrupalKernel->handle()
#16 {main}

Am I missing something?
Edit: this may have been a cache issue on my side. I have cleared cache a few times and I have not been able to reproduce the above error.

la558’s picture

Hello,
I experienced the same issue on D8.1
And like the previous post; multiple CR and time. Before the error went away.

bigbaldy’s picture

Status: Reviewed & tested by the community » Needs work

It may be better to add #[\ReturnTypeWillChange] in front of the function to block the deprecation message. mixed was introduced in PHP 8. The #[\ReturnTypeWillChange] attribute was introduced in PHP 8.1. Google Analytics 4.0.x has a Drupal 9 compatibility which has a minimum compatibility of PHP7.3. The other alternative would be to specify a minimum PHP version in the Google Analytics 4.0.x info file.

bigbaldy’s picture

Patch with#[\ReturnTypeWillChange] attribute for PHP 8.1. For other versions of PHP it is a comment.

bigbaldy’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: google_analytics-php81_deprecation-3258588-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

kalpaitch’s picture

I'd say updating the PHP dependency now might be a touch too premature. Certainly worth considering towards the end of year when D10 and PHP7 end-of-life comes up.

Tests need updating, can get to this later.

Side note, I'm intrigued by #3258588-7: Deprecated function: Return type of Drupal\google_analytics\Component\Render\GoogleAnalyticsJavaScriptSnippet::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be u, @PatrickMichael perhaps worth checking the js is being rendered as expected.

bigbaldy’s picture

For reference this is the Drupal 9.3 core fix for this issue [PHP 8.1] Add ReturnTypeWillChange attribute where necessary.

@kalpaitch I'm not sure this is premature as Drupal 9.3 recommends PHP 8.0 or PHP 8.1 and does not recommend PHP 7.

kalpaitch’s picture

I guess I just mean that it's not necessary to specify a minimum php version to resolve this issue, and to maximise support your solution so far in #9 is the one used in core and works.

fonant’s picture

Patch #9 works for me, and seems to be a sensible trade-off for PHP 7 and PHP 8.1 support.

vzamko’s picture

Patch that fixed all my errors on PHP 8.1 with version 2.x.

kfolsom’s picture

Patch #9 worked for me. PHP 8.1.6, MySQL 8.0.29, D9.3.14, Google Analytics module 4.0.0.

sundhar’s picture

Version: 4.0.0 » 4.x-dev
Status: Needs work » Needs review
StatusFileSize
new672 bytes

Patch that fixed this error on PHP 8.1.

sundhar’s picture

StatusFileSize
new3.79 KB

Patch that fixed this error & deprecated codes on PHP 8.1.

sundhar’s picture

Patch that fixed this error & deprecated codes on PHP 8.1. Few more code standard changes

sundhar’s picture

bigbaldy’s picture

One issue with the patch in #20 is that the patch also needs to be tested with PHP 7.3 to meet the minimum PHP requirements of Drupal 9. The other alternative is to start a new branch for Drupal 10 that has a minimum requirement of PHP 8.1. "mixed" is not available in PHP 7.3.

vuil’s picture

Issue tags: +PHP 8.1
klemendev’s picture

This is a major issue for us as it is seriously filling up log files

Patch #20 works fine on PHP 8.1.

I don't think separate D10 branch is ok, as D9 also supports and recommends PHP 8.1

vladimiraus’s picture

Status: Needs review » Reviewed & tested by the community

Looks good and applies to the latest stable branch.

alphawebgroup’s picture

Status: Reviewed & tested by the community » Needs work

sorry but #20 is failed to apply.

sundhar’s picture

Hi @alphawebgroup ,
This Patch for 4.x-dev branch not for released version 4.0.0

https://www.drupal.org/project/google_analytics/releases/4.x-dev

$ composer require 'drupal/google_analytics:4.x-dev@dev' and try that patch

alphawebgroup’s picture

Status: Needs work » Reviewed & tested by the community

yes... #20 has been applied successfully to 4.x-dev
sorry.. I was trying to apply it to 3.x-dev
thank you
returning RTBC back as it was changed by my mistake

glynster’s picture

The patch #20 resolves the issue! +1 RTBC

simgui8’s picture

#20 fixes multiple deprecation warning here too. Thanks

klemendev’s picture

Based on the number of confirmations of good operation, I think this is ready to be merged

bigbaldy’s picture

Has anyone tested the patch with php 7+ installed? I am using php 8.1 and don't have easy access for testing google_analytics under php 7+. I'm sure there are many sites out there still running Drupal with google_analytics that are still unable to upgrade to php 7+. The "mixed" type will generate errors if run with any php version less than 8.0. Is it the intent to require the current version of google_analytics to require php 8.0 or greater? If so, adding a php requirement to the info file may be appropriate.

klemendev’s picture

I think adding a requirement for 8+ is OK, as D9 requires PHP 8.0 or higher. Source: https://www.drupal.org/docs/system-requirements/php-requirements

bigbaldy’s picture

@KlemenDEV I think you are misreading the requirements for D9. Even though php 7.4+ is not recommended it is still listed as supported in the table you reference. I am not objecting to the patch but I'm sure there are still plenty of sites running D9 and google_analytics that will start receiving errors with the #20 patch.

bserem’s picture

Tests have passed with PHP 7.4 though, do we need more than that?

PHP 7.4 though is not recommended any more (so it is mostly ok for a module to go forward) and is reaching EOL soon.

gaele’s picture

PHP 7.4 community support is provided until 28 November 2022, four months from now. However, PHP 7.4 is part of Ubuntu 20.04 LTS, supported until 2025, and Debian 11, supported until 2026.

londova’s picture

#gaele, #bigbaldy

What is the point to delay the update, while for PHP 7.4 there is available version 8.x-3.1?
At the end - if you consider it is really important to support the PHP 7.4 as well - why you don't do this test yourself?
It sounds like "You want it" and looking for "who can do".

bigbaldy’s picture

@Lodova,

I submitted a patch that works with both PHP 7.4 and 8.0+. Another patch was submitted that uses features specific to PHP 8.1 that was gathering the most support from reviewers. At the time my websites were running PHP 7.4 with Drupal 9. I was in the process of converting to PHP 8.1 and Google Analytics was breaking. This was not only a PHP issue but also an issue with Google Analytics (Google) configuration changes. I have not changed the "Reviewed & tested by the Community". I am only pointing out issues that may break for sites stuck with using PHP 7.4. It is up to the site maintainers to decide what direction they wish to take with Google Analytics version 4.x.

gaele’s picture

@Londova I didn't add an opinion, I added information. As bigbaldy said, it is up to the (module -, I guess?) maintainers to decide.

(I'm using PHP 8.1 myself.)

bserem’s picture

The newer version can add a dependency on php 8.1, so it want be automatically updated to all installations once they run `composer update`

bkosborne’s picture

This issue got derailed quickly! I'm not the maintainer, and I don't think they've chimed in yet, but I suspect they will want:

  1. The scope of the patch to address the original issue summary only
  2. To not introduce an incompatibility with sites running PHP 7.x, of which there are still many

The most recent patch will NOT work for sites running PHP 7.x because using the "mixed" return type was added in PHP 8.0.

The patch in #9 resolves the original issue and is compatible with both PHP 7 and PHP 8. The rest of the stuff in the later patches is unrelated. There's code style updates in there and other data being cast to strings, which may be an issue, but are not reflective of what this issue was created for.

rymcveigh’s picture

I propose escalating the priority of this issue now that PHP 8.0 or higher is now recommended for Drupal 9.4.

adrianm6254’s picture

This patch #20 works good for me.

Environment:
Drupal 9.4.5
php 8.1.6

fmb’s picture

I agree with @bkosborne the right patch for this issue is #9.

ericdsd’s picture

I agree with @FMB and @bkosborne, this patch does no harm and allows to fix the issue with no backward compatibility issue.
By the way i don't get why it didn't pass the tests, as it's only a comment addition (seen from php 7.4 perspective).

bigbaldy’s picture

@ericdsd,

I believe the test failures are a result of Drupal coding standards not liking inline Doc Blocks. Though, that is what the PHP 8.1 standard uses to resolve this issue.

brtamas’s picture

Patch for version 2.5

japerry’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

This will get resolved in the D10 readiness issue here #3287765: Automated Drupal 10 compatibility fixes -- tempted to mark this as a duplicate, but could keep it around for earlier versions of GA which will not be D10 compatible (2.x and 3.x)

japerry’s picture

Status: Patch (to be ported) » Fixed

Also, note, the patch in #9 is what will get commited. Drupal 9 will always be Drupal 7.4+ compatible, so we're not introducing the `mixed` type to this module.

  • japerry committed b703904 on 4.x
    Issue #3258588 by Jeya sundhar, VladimirAus, bigbaldy, vzamko, brtamas:...
klemendev’s picture

Does 4.0.2 contain this fix?

chris matthews’s picture

It was included in 4.0.1

kenrbnsn’s picture

I'm still seeing this issue in v4.0.2

Deprecated function: Return type of Drupal\google_analytics\Component\Render\GoogleAnalyticsJavaScriptSnippet::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in include() (line 10 of /.../modules/google_analytics/src/Component/Render/GoogleAnalyticsJavaScriptSnippet.php)

Drupal 9.4.5
PHP 8.1.2

japerry’s picture

Hmm, this is concerning, as the change is definitely there: https://git.drupalcode.org/project/google_analytics/-/blob/4.x/src/Compo...

I haven't been able to reproduce yet, but it seems like 2 people here have seen it?

fathima.asmat’s picture

I can confirm that I can no longer produce the issue on 4.0.2. #[\ReturnTypeWillChange] is the key change for the method in question, +1.

fabiansierra5191’s picture

I attached the patch file for version 8.x-3.1

Status: Fixed » Closed (fixed)

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

rajab natshah’s picture

Thanks for this fix
#57 is working with 8.x-3.x

Hoping to consider committing the fix to the 8.x-3.x branch

wylbur’s picture

There's already a separate issue and patch addressing this in 8.x-3.1

https://www.drupal.org/project/google_analytics/issues/3312418

fabiansierra5191’s picture