Array that goes to the forms processor ($form_state)

7 replies [Last post]
denvermatt
Offline
Last seen: 6 years 20 weeks ago
Joined: 2011-07-08

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?

obrignoni
Offline
Last seen: 7 years 4 weeks ago
Joined: 2011-02-28
I do this a lot for debugging

Instead of print_r() try var_export() set to TRUE.

function module_form_submit($form, &$form_state) {
  drupal_set_message(var_export($form_state, TRUE));
}

...should work.

You can also use plain var_dump().

function module_form_submit($form, &$form_state) {
  var_dump($form_state);
}

denvermatt
Offline
Last seen: 6 years 20 weeks ago
Joined: 2011-07-08
That works!

The var_export function worked like a champ! I owe you a beer.

Chris Shattuck
Offline
Last seen: 2 days 9 hours ago
Joined: 2009-08-16
Good thoughts, obrignoni, I

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

denvermatt
Offline
Last seen: 6 years 20 weeks ago
Joined: 2011-07-08
print_r only sortof works

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

denvermatt
Offline
Last seen: 6 years 20 weeks ago
Joined: 2011-07-08
Code Paste

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();
}
}
}
}

obrignoni
Offline
Last seen: 7 years 4 weeks ago
Joined: 2011-02-28
I see what's

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...

drupal_set_message(print_r($form_state, TRUE));

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...

function MODULENAME_debug($object) {
  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... :-)

denvermatt
Offline
Last seen: 6 years 20 weeks ago
Joined: 2011-07-08
You learn something new everyday, right?

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.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.