Array that goes to the forms processor ($form_state)
I'm having a heckuva time pulling data out of a form I'm creating in my module. I've found that the $form_state variable contains the data (in some way), and I've found that for things like text fields, I can get to the value of the field by doing:
$form_state['values']['{fieldname}']
However, part of the functionality of my form is that the fields are dynamically generated and they are populating a "checkboxes" array in the form which appears to have a different structure from normal key-value pairings. So, the bottom line is that I don't know the field names (since they can change), and I haven't figured out how to see the array that gets sent to the forms processor.
If I could krumo or print_r the form submission, then I could see the structure of the array going to the form processor. However, if I do:
function module_form_submit($form, &$form_state) {
print_r($form_state);
}
then it doesn't print out anything. If I do:
function module_form_submit($form, &$form_state) {
drupal_set_message(print_r($form_state));
}
then it prints a "1" to the screen (not the array). Anyone know of a way to get a peek at the array that goes to the form processor?
The var_export function worked like a champ! I owe you a beer.
Good thoughts, obrignoni, I also use var_dump() instead, but print_r() should be working as well.
Have you made sure there are values in all of inputs and that the checkboxes are checked, just in case some items don't show in the 'values' array unless something is actually in there?
If you post the actual code, it may help to debug it.
Cheers!
Chris
Agreed Chris. It's weird. If I already know the specific node's key in the array that I want, then I could pull the contents of that. However, for some reason print_r wasn't able to just print out the entire array; only the single node that I specified. I figured it was a bug.
Last night I was able to use array_keys and crawl into the arrays a bit, one layer at a time and print off the keys for the subordinate arrays. Repeating that process I got some info, but I quickly discovered that this form array is very complex and has many dimensions with esoteric keys like "1". Without the high-level view it was hard to find what I was looking for.
I don't mind pasting down the code at all if it would help others. However that machine is not connected so I'll do a separate reply with the code.
Matt
Following are the page, form, and form processing functions. The print_r() function doesn't seem to be printing out the form array that goes to the form processor for some reason.
*** PAGE FUNCTION ***
function module_view_settings_admin() {
$build = array(
'header_text' => array(
'#type' => 'markup',
'#markup' => 'Some text here.'
),
'module_settings_form' => drupal_get_form('module_view_settings_form'),
);
return $build;
}
*** FORM FUNCTION ***
function module_view_settings_form() {
//return an array of the views in this implementation
$result = db_query("SELECT vid, name FROM {views_view}");
$views = $result->fetchAllAssoc('vid');
$viewKeys = array_keys($views);
$ary_viewlist = array();
for ($i=0;$i<count($views);$i++) {
$ary_viewlist[$views[$viewKeys[$i]]->vid] = $views[$viewKeys[$i]]->name;
}
$form['viewcheckboxes'] = array(
'#type' => 'checkboxes',
'#title' => 'Views',
'#options' => $ary_viewlist,
'#required' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
*** FORM PROCESSING FUNCTION ***
function module_view_settings_form_submit($form, &$form_state) {
/* THE FOLLOWING LINE DOES NOT PRINT THE ENTIRE ARRAY */
//drupal_set_message(print_r($form_state));
//Add settings data to the module_settings table
$aryLabels = array_keys($form_state['values']['viewcheckboxes']);
for ($i=0;$i<count($aryLabels);$i++) {
$aryHiddenFormFields = array("op","form_id","form_token","form_build_id","submit");
if (!in_array($aryLabels[$i],$aryHiddenFormFields)) {
$view_value = $form_state['values']['viewcheckboxes'][$aryLabels[$i]];
$view_id = $aryLabels[$i];
if ((isset($view_value)) && ($view_value != '0')) {
$trackView = db_insert('module_settings')
->fields(array(
'str_setting_id' => "",
'str_setting_name' => "track_view",
'str_setting_value' => $view_value,
'str_setting_type' => "view"
))
->execute();
}
}
}
}
I see what's happening...
drupal_set_message() expects a string. By default, print_r() prints the result on the page and breaks drupal_set_message() execution. If you set print_r() to TRUE it will pass the array as a string in the same manner as var_export() like this...
Note the second parameter set to TRUE. http://php.net/manual/en/function.print-r.php "When the return parameter is TRUE, this function will return a string."
On my MODULENAME.module file i created a helper function to make my life easier like this...
drupal_set_message(print_r($object, TRUE));
}
//Then i can call...
MODULENAME_debug($form_state);
//or any other object or array
This is useful to debug from any part of your application...
As for the beer... it's my pleasure. I'm passionate about learning PHP and Drupal... :-)
I've been writing sites in PHP for almost 8 years and I never bothered to look up print_r() to see that I could return the result as a string. I've just always used it blind.
Thanks for the tip!
M
P.S. Re: the beer... I'll just have one in your honor then.
Instead of print_r() try var_export() set to TRUE.
drupal_set_message(var_export($form_state, TRUE));
}
...should work.
You can also use plain var_dump().
var_dump($form_state);
}