Required parameter &$form_state follows optional parameter $input

Created on 30 March 2024, 6 months ago
Updated 3 April 2024, 6 months ago

Problem/Motivation

Running the module with PHP 8 shows the following note:

Deprecated: Optional parameter $input declared before required parameter $form_state is implicitly treated as a required parameter in /code/sites/all/modules/contrib/switch/switch.module on line 126

The message is caused by the following function definition:

function switch_value_callback($element, $input = FALSE, &$form_state)

&$form_state is put after a parameter with a default value ($input) which is deprecated in PHP 8.

Either $input should be left without default value or the parameters should be re-ordered.

See https://www.drupal.org/project/subpathauto/issues/3258976 🐛 Required parameter $original_path follows optional parameter $path RTBC for similar issue in Subpathauto module and how it was solved there.

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇺🇸United States SKrossa

Live updates comments and jobs are added and updated live.
  • PHP 8.0

    The issue particularly affects sites running on PHP version 8.0.0 or later.

Sign in to follow issues

Comments & Activities

  • Issue created by @SKrossa
  • Assigned to abhishek_virasat
  • Issue was unassigned.
  • Status changed to Needs review 6 months ago
  • 🇮🇳India dineshkumarbollu

    created patch by declaring optional parameters after required parameters.

  • 🇺🇸United States SKrossa

    The patch in #4 may cause issues if the function is called by another module, as the variables would be in the wrong order.

    Based on info at Migrating from PHP 7.4.x to PHP 8.0.x on this deprecated feature (https://www.php.net/manual/en/migration80.deprecated.php), "If a parameter with a default value is followed by a required parameter, the default value has no effect. This is deprecated as of PHP 8.0.0 and can generally be resolved by dropping the default value, without a change in functionality"

    So the. patch should simply change

    function switch_value_callback($element, $input = FALSE, &$form_state){

    to

    function switch_value_callback($element, $input, &$form_state){

    And it would function just the same as before, as in PHP 7.4 the default value of FALSE was already being ignored, anyway.

    I will try to create a patch that does this later (when I have a bit more time to figure out how one does that!)

Production build 0.71.5 2024