Problem/Motivation
For views with an exported array, the JSON output includes the following error:
Return type of Drupal\rest_views\RenderableData::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in <b>/var/www/html/web/modules/contrib/rest_views/src/RenderableData.php</b> on line <b>73</b><br />
In PHP 8.1 a tentative return type was added to the method: JsonSerializable::jsonSerialize()
Steps to reproduce
Create a view.
Add a field that will contain multiple rendered entities as an array
View the resulting output
Suggested Solution
Add #[\ReturnTypeWillChange] above line 73, or perhaps more ideally, change line 73 to read
public function jsonSerialize(): array {
| Comment | File | Size | Author |
|---|
Issue fork rest_views-3393017
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
the_double_a commentedHere is a suggested patch.
Comment #3
marios anagnostopoulos commentedAdding a patch with the array return type as suggested by the OP
Comment #4
marios anagnostopoulos commentedQueued for test and changing to needs review.
Comment #6
benjifisherI have not tested, but I have reviewed this issue. Either of the patches in #2 or #3 should resolve the problem for PHP 8.1+, and either patch should be compatible with lower versions of PHP as well.
Since the return type is known, I prefer to add a return type rather than an annotation to skip the check.
When implementing an interface in PHP, it is always allowed to specify a more restrictive return type than what the interface says. So a return type of
arrayshould work with PHP 8.1+ (where the return type ismixed) and with earlier versions (no return type).I have added a MR based on the patch in #3, and I am hiding the two patches.
Comment #9
nicxvan commentedThis looks good, thanks for the review and MR conversion!
Comment #10
nicxvan commentedComment #12
nicxvan commented