In file_field_widget_value(), the second argument has a default value, but the third does not. Remove this default value.

Comments

Liam Morland created an issue. See original summary.

liam morland’s picture

Status: Active » Needs review
StatusFileSize
new615 bytes
avpaderno’s picture

The documentation for the #value callback shows different paraments for the callback.

A value function for an element takes $element, $input and $form_state as parameters, and has the form:

function myelement_value($element, $input = FALSE, $form_state = array()) {
  if ($input === FALSE) {
    return isset($element['#default_value']) ? $element['#default_value'] : 0;
  }
}

Maybe the arguments should be changed to be $element, $input = FALSE, $form_state = array(), instead of $element, $input, $form_state.

avpaderno’s picture

Notice that Drupal 8 uses $element, $input, FormStateInterface $form_state because the last argument cannot take a default value that wouldn't cause any error message without the code checking its value. (I imagine what would happen with $element, $input, FormStateInterface $form_state = NULL when the callback receives the default argument.)

In Drupal 7, there isn't that problem, since $form_state is an array, and an empty array as default value does make sense.

liam morland’s picture

I'm not sure file_field_widget_value() would be OK with getting an empty array instead of an actual form state. file_managed_file_value() might not work with that. Is there any use for calling file_field_widget_value() with only $element as an argument?

avpaderno’s picture

Actually, it seems the default value for the second parameter is only used to show/remember that FALSE could be passed as second argument.

    if (!isset($element['#value'])) {

      // Call #type_value without a second argument to request default_value handling.
      if (function_exists($value_callback)) {
        $element['#value'] = $value_callback($element, FALSE, $form_state);
      }

      // Final catch. If we haven't set a value yet, use the explicit default value.
      // Avoid image buttons (which come with garbage value), so we only get value
      // for the button actually clicked.
      if (!isset($element['#value']) && empty($element['#has_garbage_value'])) {
        $element['#value'] = isset($element['#default_value']) ? $element['#default_value'] : '';
      }
    }

That should not be the purpose of default values for parameters. The documentation for the value callback should be changed to show no default values are given for the callback parameters.

avpaderno’s picture

Inside the file.module file there is also file_managed_file_value(), for which the parameters are &$element, $input = FALSE, $form_state = NULL.

Those aren't even the parameters described in the documentation, and they don't make sense, as the value callback will probably never get a null form state, since the value callbacks are called from a function (_form_builder_handle_input_element()) that gets a valid $form_state (and not an empty array or NULL) from form_builder().

avpaderno’s picture

Issue summary: View changes

And to make this more confusing, there are value callbacks that just declare two parameters, like form_type_token_value(). (Truly, this happens for all the form_type_*_value() functions.)

poker10’s picture

Status: Needs review » Closed (outdated)

This was fixed in #3156847: [PHP 8] Parameter order fixes, as it was causing PHP 8 deprecation message. I have added credits for your effort to the mentioned issue.