Posted by jmm42 on January 8, 2013 at 6:23pm
I'm studying a sample module, and having some difficulties trying to understand where $form_state['values']['aid'] is defined within drupal core (form.inc). I looked throughout the 'artwork' module files and functions, and I cannot find where $form_state['values']['aid'] is generated / defined -- thus, I've come to the conclusion, that it must be somewhere in the 'form.inc'. This is the function within the 'artwork' module that uses the $form_state['values']['aid'] array value:
function artwork_delete_confirm_submit($form, &$form_state) {
if( $form_state['values']['confirm']) {
$artwork = artwork_load($form_state['values']['aid']);
artwork_delete($form_state['values']['aid']);
watchdog('artwork', '@type: deleted %title.', array('@type' => $artwork->type, '%title' => $artwork->title));
$types = artwork_types();
drupal_set_message(t('@type %title has been deleted.', array(
'@type' => $types[$artwork->type]->name,
'%title' => $artwork->title)));
}
$form_state['redirect'] = '<front>';
}
Comments
It is set in
It is set in artwork_delete_confirm() here
$form['aid'] = array('#type' => 'value', '#value' => $artwork->aid);$form_state['values']['confirm'] is set in confirm_form()
We have $form['aid']
I do see the following in artwork_delete_confirm($form, &$form_state, $artwork):
$form['aid'] = array('#type' => 'value', '#value' => $artwork->aid);$form_state['values']['confirm'] is set in confirm_form() ?
I don't see '$form_state['values']['confirm']' however, in 'confirm_form()':
http://api.drupal.org/api/drupal/modules!system!system.module/function/confirm_form/7
If you do a code trace
It's not explicitly stated, but if you do a code trace you'll notice this line:
$form[$name] = array('#type' => 'hidden', '#value' => 1);
See above where $name is passed in with the default value of "confirm."
Thus, by the time the form is returned through this function $form['confirm'] will be set to array('#type' => 'hidden', '#value' => 1);
But, how does
But, how does "$form['confirm']" translate into $form_state['values']['confirm']?
All form elements with a
All form elements with a value end up in $form_state['values']
What about
What about $form_state['values']['confirm'], as well as $form_state['values']['aid'].. where in core is this defined? How are the values assigned?
$form_state['values'] is clearly defined
For example,
$form_state = array(), is defined in line 124 of 'form.inc', in 'drupal_get_form()'.
Then in your hook_form(), you have something like this:
function artwork_form($form, &$form_state, $artwork) {
$form['#id'] = 'artwork-form';
$form['#artwork'] = $artwork;
$form_state['artwork'] = $artwork;
...
}
But, what about:
My current understanding is
My current understanding is that, if you have:
$form['aid'] = array('#type' => 'value','#value' => $artwork-> aid);
drupal somehow translates it to:
$form_state['values']['aid']And, something similar is done for "$form_state['values']['confirm']". For now, I will just accept this, unless, someone knows how the magic is done. I'd rather not replicate this process and try to do a 'code-trace' on this (on something like netbeans).
This will probably come
This will probably come across as a little rude but... Why does it matter? The point of using Drupal's APIs is that it handles these things in the background for you so that you don't need to worry about them. If you find a bug, it's definitely worth digging through and finding it, but the Form API has been around since Drupal 4.7 and is quite refined with few bugs remaining.
Not that there is anything wrong with wanting to know more, it just seems to be a little unproductive to spend so much time on the internals rather than on how to use them.
Jaypan We build websites
Jaypan
I understand your frustration. However, I just want to know the internals of drupal. I assume once I reach a certain level of understanding, I will begin to feel comfortable generalizing, without the need to dig so deep. But, I'm literally reading the last function for this module, and the last page for the chapter of a book I've picked up. So if you could help, I'd appreciate it greatly!
Solution: Found - line 2503 'form.inc'
function form_set_value($element, $value, &$form_state) {drupal_array_set_nested_value($form_state['values'], $element['#parents'], $value, TRUE);
}
Solution (final): Found
Just in case anyone else is curious, I believe 'form.inc: form_builder($form_id, &$element, &$form_state)', on line 1838:
if (!isset($element[$key]['#parents'])) {// Check to see if a tree of child elements is present. If so,
// continue down the tree if required.
$element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key);
}
allows the function 'form_set_value()' to have proper information to make the assignment (http://drupal.org/node/1883450#comment-6918038) -- more specifically, allowing the following to happen:
More detailed explanation: http://drupal.org/node/48643