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

Comments

frank.schram created an issue. See original summary.

andregp’s picture

Assigned: Unassigned » andregp

I'll work on this issue.

andregp’s picture

Version: 7.x-1.19 » 7.x-1.x-dev
Assigned: andregp » Unassigned
Status: Active » Needs review
StatusFileSize
new1.37 KB

Placed optional parameter at the end of function parameters according to Drupal standards.

frank.schram’s picture

Patch #3 solves the error. Thank you!

frank.schram’s picture

I 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

andregp’s picture

Hi @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.

joelpittet’s picture

Status: Needs review » Needs work

Thanks @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?

andregp’s picture

Assigned: Unassigned » andregp

Okay, I'll try this other option.

andregp’s picture

Assigned: andregp » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1001 bytes

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

frank.schram’s picture

Looking good, seems to work, thanks!

joelpittet’s picture

Status: Needs review » Needs work

That 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()?

  if (!isset($view_mode)) {
    $view_mode = 'ctools';
  }
pavel_spn’s picture

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

pavel_spn’s picture

StatusFileSize
new645 bytes

I 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

pavel_spn’s picture

StatusFileSize
new925 bytes
joelpittet’s picture

Status: Needs work » Needs review
andregp’s picture

StatusFileSize
new40.48 KB

Sorry, 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.

andregp’s picture

StatusFileSize
new893 bytes
new1.62 KB

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

joelpittet’s picture

Status: Needs review » Fixed

I do that soooo many times --relative flag missed on git diff. Thanks, I've committed this to the dev branch for the next release.

  • joelpittet committed b4e4426 on 7.x-1.x authored by andregp
    Issue #3254225 by andregp, Pavel_SPN, frank.schram, joelpittet:...

Status: Fixed » Closed (fixed)

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

liam morland’s picture

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