EDIT: Separated 4th question.
I've just recently started working with custom elements and already came across some weird behavior / questions I'd like to see answered.
1) How does #value_callback
work, what should you return in it, etc. ?
Imagine you have pretty simple custom element without set #element_validate
:
function TST_element_process($element, &$form_statem, $form) {
$element['testing_test'] = array(
'#type' => 'textfield',
'#default_value' => 'ABC',
);
return $element;
}
function TST_value_callback_1($element, $input = FALSE, &$form_state) {
//example of input retrieving magic
return ($input['testing_test'] . '_valc_');
}
function TST_value_callback_2($element, $input = FALSE, &$form_state) {
//example of input retrieving magic
return array($input['testing_test'] . '_valc_');
}
'TST' => array(
'#input' => TRUE,
'#tree' => TRUE,
'#process' => array('TST_element_process'),
//'#element_validate' => array('TST_element_validate'),
'#value_callback' => 'TST_value_callback_1', // or TST_value_callback_2
),
And form:
$ar['inp'] = array('#type' => 'TST');
1A) When you use TST_value_callback_1
, the callback gets called and runs but doesn't alter the result value in any way (in submit the $form_state['values']['inp']['testing_test']
contains pure input -> ABC
).
1B) In case of TST_value_callback_2
the $form_state['values']['inp']['testing_test']
still contains unaltered input but there's also value in $form_state['values']['inp'][0]
which contains the 'right' value (ABC_valc_
).
My questions are:
1.1 what exactly are you supposed to return in #value_callback
and why do the individual callbacks work in the way they do (or don't do in case of the first one).
1.2 is there any way how to make the #value_callback
store the data in 'element root' ($form_state['values']['inp']
), i.e. in the same place as if I used form_set_value in #element_validate
?
2) How do individual #value_callback
work on custom element that is made of other elements?
Let's say you have a custom element (A
) that is made of a nested set of other elements with set #value_callback
.
for($a = 0; $a < 10; $a++) {
$element['indiv_elems'][] = array(
'#type' => 'indiv_element',
);
}
Then a number of things can appear in $form_state[values][<element_name_in_FAPI_array>]
depending on how you approach #value_callback
in the custom event (A
) (and some of those things make little sense to me).
2A) When you don't use #value_callback
in (A
) at all, the $form_state[values][<element_name_in_FAPI_array>]
will contain array of raw inputs (not processed by its #value_callback
function) from each phone_numbers
element.
2B) When you set #value_callback
in (A
) but let it return (let's say) empty array(), the $form_state[values][<element_name_in_FAPI_array>]
will contain array of inputs from each indiv_element
element processed by their (individual #value_callback
function) .
My question is:
2.1) Why do the values not get processed by individual #value_callback
when parent element's #value_callback
is not set. Basically why is there difference between 2A) and 2B).
3) Do I understand this flowchart correctly that #value_callback
is called before #element_validate
?
I'm also aware that it will be quite time consuming to answer this question, so many thanks and love ahead! :)
hook_element_info()
too so it make more sense how you adding#value_callback
.