I'm trying to create a custom form element. I'm confused as to where #value is supposed to get set.

* in the #process function? But that doesn't seem to get called when the form is submitted
* in hook_elements?
* in the theme function?

Does it have something to do with setting #children or #tree?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wesley Tanaka’s picture

oh, but wait... I updated to 4.7.0-beta2 and #process *does* seem to be called when the form is submitted

Wesley Tanaka’s picture

if I have #process => array('fxn' => array())

and
fxn($element)
{
$element['#value'] = 'xyz';
return $element;
}

that #value doesn't seem to get passed along.

Wesley Tanaka’s picture

Title: Still confused about creating a custom element » it doesn't seem possible to set the #value for a custom element from within the element definition code
Category: support » bug

I'm gonna mark this as a bug, though I could be wrong about there being no way to do this.

Wesley Tanaka’s picture

The #process function gets called after global $form_values takes on the value in #value, so there's no way for it to affect the value without touching $form_values itself.

Wesley Tanaka’s picture

Status: Active » Needs review
FileSize
825 bytes
Wesley Tanaka’s picture

Title: it doesn't seem possible to set the #value for a custom element from within the element definition code » setting #value in custom element #process function doesn't get propogated to _validate and _submit
Wesley Tanaka’s picture

Title: setting #value in custom element #process function doesn't get propogated to _validate and _submit » custom form elements can't cleanly set a value for themselves
Wesley Tanaka’s picture

FileSize
1.21 KB

here's a module which helps explain what this bug is about, via an illustrative example.

1. Put the module in your modules directory
2. go to "admin/modules"
3. enable the "bug41023" module
4. go to "41023"
5. press "Go"

Expected behavior:
[a] is set to 'hi there'

Actual behavior:
[a] is not set

Wesley Tanaka’s picture

FileSize
1.23 KB

my sample code wasn't returning $element from the #process function. fixed that.

drewish’s picture

wtanaka, the value gets set but _submit uses posted values. you'll need to have your process function return hidden field.

Wesley Tanaka’s picture

yes, i'm trying to compose those posted values into a uber-combined value.

I didn't put any children in the element because then I'd also need to add a theme function to the example code. Hold on while I expand the example a bit to make it clearer.

chx’s picture

Component: base system » forms system
Status: Needs review » Reviewed & tested by the community

I am ifne with the patch.

Wesley Tanaka’s picture

that's a good thing, since the new example was getting pretty hard to understand.

Wesley Tanaka’s picture

a point from the conversation with Károly

the patch shouldn't affect anything (Károly was initially concerned about it affecting previews) since all it does is move the assignment into $form_values from immediately before the #process call to immediately afterwards.

chx’s picture

Assigned: Unassigned » chx
Priority: Normal » Critical

Well, a #process callback can check for the value of the current element in #value and if for some arcane reason it needs other elements value then it can still access global form_values. Only change is that #value is not yet added to lobal form_values in #process. That's really not a problem.

Now I marked this critical because the patched behaviour is indeed the intended one.

drumm’s picture

From visual inspection I think this has already been applied.

drumm’s picture

Status: Reviewed & tested by the community » Fixed

Found the commit message:

"- Patch #41023 by wtanaka: custom form elements can't cleanly set a value for themselves."

Anonymous’s picture

Status: Fixed » Closed (fixed)