Got the error below when loading a D7 site in PHP 8.0
Deprecated: Required parameter $formatter follows optional parameter $view_mode in C:\wampserver\www\rapport1\sites\all\modules\ctools\includes\fields.inc on line 21
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | interdiff-3254225_14_17.txt | 1.62 KB | andregp |
| #17 | 3254225-17.patch | 893 bytes | andregp |
| #14 | ctools-3254225-14.patch | 925 bytes | pavel_spn |
Comments
Comment #2
andregp commentedI'll work on this issue.
Comment #3
andregp commentedPlaced optional parameter at the end of function parameters according to Drupal standards.
Comment #4
frank.schram commentedPatch #3 solves the error. Thank you!
Comment #5
frank.schram commentedI am still seeing this error below now:
DEPRECATED: REQUIRED PARAMETER $PLUGIN FOLLOWS OPTIONAL PARAMETER $DATA IN ...SITES\ALL\MODULES\CTOOLS\PLUGINS\CONTEXTS\ENTITY.INC ON LINE 58
Comment #6
andregp commentedHi @frank.schram. So the message you're talking about on comment #5 is not related to this issue (it is from another file of the code).
You could create another issue for this but it won't be necessary as this referred line (line 56 on ctools/plugins/contexts/entity.inc) was already fixed on the 7.x-1.x-dev branch, which means the fix will be available on the next update already.
Comment #7
joelpittetThanks @andregp, the proposed change is an API break (no matter how small) instead could you make the last 2 parameters optional, or make the one $view_mode parameter default in the body of the function? Or if you have another proposal?
Comment #8
andregp commentedOkay, I'll try this other option.
Comment #9
andregp commentedSo I made the $view_mode parameter default in the body of the function. The @param comment says: We're building a new view mode for this function. Defaults to ctools, but we expect developers to actually name this something meaningful. So the code now apply the default value 'ctools' if the $view_mode is either NULL or a string of empty spaces.
Comment #10
frank.schram commentedLooking good, seems to work, thanks!
Comment #11
joelpittetThat will produce a new PHP 8 notice if someone did pass in NULL. See example:
https://3v4l.org/l0o8U
But closer, how about !isset() or is_null()?
Comment #12
pavel_spn commentedI should notice that the ctools_fields_fake_field_instance() function is also used in the views module (https://git.drupalcode.org/project/views/-/blob/7.x-3.25/modules/field/v...) and just moving parameters (as proposed in comment #3) will break the functionality.
Comment #13
pavel_spn commentedI propose to add a check for the $view_mode variable. Patch attached.
Added: patch 'ctools-3254225-13.patch' is not fully please use ctools-3254225-14.patch from comment #14
Comment #14
pavel_spn commentedComment #15
joelpittetComment #16
andregp commentedSorry, regarding comment #12, I just learned latter that reordering the parameters of an existing function is considered a bad practice.
(See these comments: https://www.drupal.org/project/views_php/issues/3254385#comment-14340139 https://www.drupal.org/project/ctools/issues/3254225#comment-14341110)
So, the code on patch #14 is the best approach. Unfortunately, for some reason, it failed to apply. Maybe it's just a bug on the Drupal site testing (happens sometimes) so I added a retest.
Comment #17
andregp commentedI found why the #14 patch wasn't applying. The patch was created from an upper folder instead of the current module folder, this messed the path to the file.
I simply copied #14 code and recreated a patch from the right folder.
Comment #18
joelpittetI do that soooo many times
--relativeflag missed ongit diff. Thanks, I've committed this to the dev branch for the next release.Comment #21
liam morlandThis commit may be fine, but it does make a small change to what the code does. Previously, if $view_mode was set to NULL when the function is invoked, $view_mode would be NULL in the function. Now it will be 'ctools'.
PHP deprecation messages like this can be fixed without any side-effects by just removing the default value from the function signature.