Problem/Motivation

Right after I upgraded PHP to version 8.1.4 following error appears :

Deprecated function: unserialize(): Passing null to parameter #1 ($data) of type string is deprecated in Drupal\viewsreference\Plugin\Field\FieldFormatter\ViewsReferenceFieldFormatter->viewElements() (line 75 of modules/viewsreference/src/Plugin/Field/FieldFormatter/ViewsReferenceFieldFormatter.php).

attached Patch, please try to test if it works for you.

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

coaston created an issue. See original summary.

avpaderno’s picture

Title: Deprecated function: unserialize() » Passing null to parameter #1 ($data) of type string to unserialize() is deprecated
anneke_vde’s picture

The solution worked, but the patch couldn't by applied with composer patches.
So here's is the newly created patch

paul.kim’s picture

This error also occurs in src/Plugin/Field/FieldType/ViewsReferenceItem.php, I've added a similar patch here for that issue.

coaston’s picture

Hi Paul,

Thank you for your patch, it looks fine ,however you should merge both patchs #3 and #4 into one file because people will use the latest path #4 but it won't fix the issue which was resolved in #3.

avpaderno’s picture

Issue tags: +PHP 8.1
greggmarshall’s picture

Here's the combined patch

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

rodrigoaguilera’s picture

In my case $values['data'] is not even set in the array so I think using isset is more appropriate to ensure unserialize is not even called whne there is no data.

Opened a merge request.

anybody’s picture

@rodrigoaguilera thanks, that's the only real solution here, all patches are wrong. I'd vote for an !empty() instead of isset() to be even safer!

Confirming this issue!

anybody’s picture

Status: Needs review » Needs work
anybody’s picture

Status: Needs work » Needs review

I made that change.

The issue btw also leads to a warning:

Warning: Undefined array key "data" in Drupal\viewsreference\Plugin\Field\FieldType\ViewsReferenceItem->setValue()

greggmarshall’s picture

I just noticed the warning in #13 as well.

interdruper’s picture

Status: Needs review » Needs work

I also noticed that https://git.drupalcode.org/project/viewsreference/-/merge_requests/23.patch breaks the views filtering when argument is set to feed a contextual filter.

jrochate’s picture

Same here as #15. Path breaks filtering when using viewsreference with argument using paragraphs.

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

neclimdul’s picture

Status: Needs work » Needs review

Yeah, there was a little bug in the merge request. The new commit should fix it.

g.rocchini’s picture

Hello, any news about this issue?
Will be available a release soon or we should use the patch?
Thanks!

grevil’s picture

Status: Needs review » Reviewed & tested by the community

MR by @neclimdul LGTM!

(It also fixes the issue mentioned in #13).

ikeigenwijs’s picture

StatusFileSize
new5.33 KB

current patch, for composer

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

seanb’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks everyone! Had to reroll, so please help test the latest dev version!

Status: Fixed » Closed (fixed)

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

bogdan.dinu’s picture

StatusFileSize
new1.5 KB

The warning still exists on Drupal 10 with php 8.1 and viewsreference 2.0-beta6.
I attached a patch that fixes the issue in \Plugin\Field\FieldType\ViewsReferenceItem and Plugin\Field\FieldFormatter\ViewsReferenceFieldFormatter as they are using the 'data' array key without checking if it is set.
PS: the warning is generated only for the FieldType but I think adding the check for the FieldFormatter doesn't hurt :)

ilfelice’s picture

Howdy,

Patch from #26 applied cleanly on a Drupal 10.1 install with newly installed viewsreference 2.0-beta6.

After applying the patch, the error is gone.

drupalninja99’s picture

When I tried to apply #26 to the latest 2x dev, composer just hangs or says it can't apply the patch.

drupalninja99’s picture

StatusFileSize
new954 bytes

I am attaching a small patch that resolves the error I am getting in ViewsReferenceFieldFormatter.php.

dewancodes’s picture

I tried #27 on drupal 10 and it worked fine for me as error gone. Thanks @drupalninja99.

xeiro’s picture

#3 applies and removes the error on 9.5x / php 8.1.

norman.lol’s picture

#29 makes the views arguments not being respected anymore. Exactly what's described in #3405177: Contextual filters not respected when used in paragraph.

Everybody just needs to update to 2.0-beta7.

anybody’s picture

@seanB: Reading the comments here, should this be reopened or should someone please create a follow-up issue (I'd suggest so!)

steveoriol’s picture

I have the error on viewsreference:2.0.0-beta7 end php 8.2
Deprecated function: unserialize(): Passing null to parameter #1 ($data) of type string is deprecated in Drupal\viewsreference\Plugin\Field\FieldFormatter\ViewsReferenceFieldFormatter->viewElements() (line 98 of modules/contrib/viewsreference/src/Plugin/Field/FieldFormatter/ViewsReferenceFieldFormatter.php).

daletrexel’s picture

I've opened a new issue #3423648: unserialize() of NULL deprecation returns to ViewsReferenceFieldFormatter since there has been a re-emergence of a problem that this issue had resolved when it was closed.

#3380691: Issue 2919092 breaks BC and causes issues for existing sites, which was included in the beta7 release, allows null data to be passed to the unserialize() function again, without checking whether data might be null or not: unserialize($item->getValue()['data'], ['allowed_classes' => FALSE]) As I and others have found, null 'data' is possible in views reference fields (likely those saved a while ago when expectations for this field were looser), so we really need to bring back a sanity check here before calling unseralize().

My new ticket proposes a solution to re-fix this particular instance (line 98 of ViewsReferenceFieldFormatter.php)

(The 'data' field stores the "extra settings" configuration, which is also involved in #3402036: Configuration schema for "Enable extra settings" (title,pagination,...) incorrect. I wouldn't be surprised if this is related, but so far I've been unable to reconstruct what previous scenario allowed an empty 'data' field to begin with. It was likely in Drupal 9, which I no longer have an easy way to test.)

javier_rey’s picture

Without patch number 27, I get the warning mentioned in the issue.
But when applying it I don't get any "value" in the method "public function alterView(ViewExecutable $view, $value)" of my "
ViewsReferenceSetting custom setting.

My version is 2.0-beta7.

batkor’s picture

#36 confirm