Freeform: Accessing $fields array via ‘freeform_module_insert_end’ hook
Posted: 11 March 2008 11:16 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  98
Joined  09-18-2005

Hi folks, I’m having difficulty getting at the $fields array within an extension using the ‘freeform_module_insert_end’ hook - help appreciated.

From the docs

“Available through this extension hook is the associative array $fields which contains all of the Freeform fields where the key is the name of the field and the value is the label.”

Within my extension I have a function which is being passed $fields and $entry_id. I only want this function to do stuff for a particular form: “questionnaire”.

Code looks something like this:

function foo ($fields, $entry_id) {
  
if ($fields['form_name']=='questionnaire') {
  
// do stuff
  
}
}

The problem is that $fields[’form_name’] is returning no value, so my stuff doesn’t get done.

Is there something wrong with the syntax I’m using to address the $fields array? I’m somewhat confused about what “the value is the label” means in the context of the $fields array: I thought the label was the display name given to the field for display within CP Home › Modules › Freeform › Manage Entries. I’m no doubt missing something obvious here!

If I remove the condition, it works perfectly. I’m using the latest version of Freeform (2.6.3) and EE 1.6.2 Build: 20080124

Cheers
Ken and David

 Signature 

kennethgray.co.uk

Profile
 
 
Posted: 11 March 2008 08:06 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  608
Joined  03-02-2008

Do this and run the code again:

function foo ($fields, $entry_id) {
    
echo '<pre>';
    
print_r($fields);
    echo
'</pre>';
    exit;
    if (
$fields['form_name']=='questionnaire') {
        
// do stuff
    
}
}

That will print out the array and tell you what’s being passed through $fields. The script should die, though, so hopefully this isn’t in a template.

 Signature 

 
“If you love the truth, you’ll trust it - That is,
you will expect it to be good, beautiful, perfect, orderly, etc…
In the long run, not necessarily in the short run.”
- Abraham Mazlow
 
“Truth is a long-term proposition.
Honesty, day to day, is malleable.
We learn honesty by studying truth,
In all it’s nature, in all it’s hope.”
- Unknown

When posting template code or HTML, use code wrappers to highlight code in posts. Thank you!
Profile
 
 
Posted: 12 March 2008 09:09 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  3
Joined  01-19-2006

Thanks, I’m helping Ken with this one and I see the problem. Now fixed.

Problem: The $fields array accessible to the ‘freeform_module_insert_end’ hook doesn’t contain the values posted by the user, but instead contains the field labels:

Array
(
    
[form_name] => Form Name
    [enquiry]
=> Enquiry
    
... etcetera
)

This explains my confusion re the wording of the Solspace docs on this extension hook!

Fix: using the ‘freeform_module_insert_begin’ hook, BEFORE Freeform has submitted the form to the database, allows us to get at the form data via the $data array:

Array
(

    
[form_name] => contact
    [enquiry]
=> Hello World!
    ...
etcetera
)

Looks as if the $fields array would only be of use if you wanted to modify the appearance of the Freeform admin interface using an extension. For our purpose $data is the way to go!

David

 Signature 

68Eleven: Business-driven, blog-powered web design and consultancy
http://www.68eleven.co.uk

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120472 Total Logged-in Users: 60
Total Topics: 126549 Total Anonymous Users: 34
Total Replies: 665393 Total Guests: 298
Total Posts: 791942    
Members ( View Memberlist )