Right now our #value_callback handling is a bit inconsistent. In the case that a form is being submitted, the value callback is passed $element, $input, and $form_state. However in the situation that the form is not being processed, the value callback is called with just $element.

In the #391330: File Field for Core issue, I need the ability to return a value based on the contents of the $form_state alone, since when an AHAH request is made, the form is regenerated from scratch and $form_state['input']. We need to be able to get at this value so that it can be maintained when updating via AHAH.

The patch is super-simple, just pass the $form_state into the #value_callback when getting the default value, just like we do when processing input.

CommentFileSizeAuthor
form_value_callback_form_state.patch813 bytesquicksketch
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Issue tags: +ImageInCore

Tagging.

mattyoung’s picture

subscribe

Dries’s picture

Status: Needs review » Fixed

Looks proper to me. Committed!

quicksketch’s picture

Rock, thanks!

Status: Fixed » Closed (fixed)

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

Alan D.’s picture

Status: Closed (fixed) » Needs work
Issue tags: +Needs documentation

This needs some documentation as it changes the way the value callback is used. (I only looked at the migration guide)

The Drupal 6 example element page shows why:

<?php
function form_type_example_field_value($element, $edit = FALSE) {
  if (func_num_args() == 1) {
     return $element['#default_value'];
  }
  return $edit;
}
?>
quicksketch’s picture

I'm not sure what or where such documentation lives. Where did you pull this example from? I'd say that it should be written like this instead:

function form_type_example_field_value($element, $edit = FALSE) {
  if ($edit === FALSE) {
     return $element['#default_value'];
  }
  return $edit;
}
Alan D.’s picture

In Drupal 6, that would give false positives to fields that have a value set to FALSE. Thus the check on the number of arguments.

This is from the handbook page: http://drupal.org/node/169815 Creating Custom Elements Using Drupal 6.x

quicksketch’s picture

In Drupal 6, that would give false positives to fields that have a value set to FALSE.

That's true, but since HTML isn't capable of sending Boolean values in POST, this won't be a problem in nearly any situation. You'll only have to deal with 0 or an empty string.

Alan D.’s picture

For some reason, I did get this once, but for the life of me, I can not remember the set up. Probably a logical error somewhere else... I'm sure I would have used "===".

But anyway, I think this should be documented somewhere, and since I'm just not sure on the exact place / protocol, I just tagged it. I guess that the $form_state should be used to check the submit value.

Something like:

---------------------------------------------------------------------------------------
Parameters to form_type_HOOK_field_value() have changed

Value callback functions, as defined by an FAPI element #value_callback property or the elements type_HOOK_field_value() callback, now get passed the $form_state as the third parameter.

Drupal 6.x:

<?php
function form_type_example_field_value($element, $edit = FALSE) {
  if ($edit === FALSE) {
     // The form has not been submitted, set the default value here.
  }
  return $edit;
}
?>

Drupal 7.x:

function form_type_example_field_value($element, $edit, $form_state) {
  // ??? I haven't programmed much in Drupal 7 yet, so not sure on the best practice here.
  if (empty($form_state['submitted'])) {
     // The form has not been submitted, set the default value here.
  }
  return $edit;
}


---------------------------------------------------------------------------------------

And I see that the signature for _form_builder_handle_input_element() has changed.

function _form_builder_handle_input_element($form_id, &$element, &$form_state) {
.....

Which means (??) that we can update the $form_state from the value callback too! Very cool! I guess that had something to do with File & Image in core?

jhodgdon’s picture

Looks like we are asking for something to be added to the 6/7 module update guide? Changing tag accordingly. If you want some other documentation to be done, please comment and add back the other tag again.

jhodgdon’s picture

Status: Needs work » Fixed

I added a section to the module update guide about this. I didn't think an example was all that necessary, and there's a link to this issue in it.
http://drupal.org/update/modules/6/7#value-callback-state

Status: Fixed » Closed (fixed)
Issue tags: -ImageInCore, -Needs documentation updates

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