0

Short question: In Freeform notification templates... where does it get its field data from? What's the best way to manipulate field data before the notification template gets a hold of it? Is there a custom FieldType method to use to process that data for the notification template?

Long question: There may be a better way to go about this but I'm attempting to build a custom FieldType for Freeform to accommodate inputs with multi-dimensional arrays. The user would be able to click an 'add employer' button to keep adding more employers to their work history. The reason why I'm going this route is to prevent having to create a hundred fields with names 'employer_business_1', 'employer_zip_1', 'employer_business_2', etc.

I've got a custom FieldType working great when placing the field data into the database, but I can't seem to get the data into a notification template. Where is the notification template getting its data from? My pipe separated data never makes it into my notification template.

I've embedded a template into my notification template so I can use PHP to manipulate the field data but it simply comes in as 'ArrayArray'.

An example of the form inputs:

<div class="employer_wrapper">

<label for="business">Place of Employment:</label>
<input type="text" id="business" name="employer_array[0][business]" value="" />

<label for="occupation">Employment Title or Position:</label>
<input type="text" id="occupation" name="employer_array[0][occupation]" value="" />

<label for="supervisor">Name and Title of Supervisor:</label>
<input type="text" id="supervisor" name="employer_array[0][supervisor]" value="" />

<label for="work_phone">Telephone:</label>
<input type="text" id="work_phone" name="employer_array[0][work_phone]" value="" />

<label for="employed_from">Employed From:</label>
<input type="date" id="employed_from" name="employer_array[0][employed_from]" value="" />

<label for="employed_to">Employed To:</label>
<input type="date" id="employed_to" name="employer_array[0][employed_to]" value="" />

<label for="starting_salary">Starting Salary:</label>
<input type="text" id="starting_salary" name="employer_array[0][starting_salary]" value="" />

<label for="ending_salary">Ending Salary:</label>
<input type="text" id="ending_salary" name="employer_array[0][ending_salary]" value="" />

<label for="address">Business Address:</label>
<input type="text" id="address" name="employer_array[0][address]" value="" />

<label for="city">City:</label>
<input type="text" id="city" name="employer_array[0][city]" value="" />

<label for="state">State:</label>
<input type="text" id="state" name="employer_array[0][state]" value="" />

<label for="zip">ZIP:</label>
<input type="text" id="zip" name="employer_array[0][zip]" value="" />

<label for="reason_for_leaving">Reason for Leaving:</label>
<textarea type="text" rows="5" name="reason_for_leaving" name="employer_array[0][reason_for_leaving]"></textarea>

<label for="business">Place of Employment:</label>
<input type="text" id="business" name="employer_array[1][business]" value="" />

<label for="occupation">Employment Title or Position:</label>
<input type="text" id="occupation" name="employer_array[1][occupation]" value="" />

<label for="supervisor">Name and Title of Supervisor:</label>
<input type="text" id="supervisor" name="employer_array[1][supervisor]" value="" />

<label for="work_phone">Telephone:</label>
<input type="text" id="work_phone" name="employer_array[1][work_phone]" value="" />

<label for="employed_from">Employed From:</label>
<input type="date" id="employed_from" name="employer_array[1][employed_from]" value="" />

<label for="employed_to">Employed To:</label>
<input type="date" id="employed_to" name="employer_array[1][employed_to]" value="" />

<label for="starting_salary">Starting Salary:</label>
<input type="text" id="starting_salary" name="employer_array[1][starting_salary]" value="" />

<label for="ending_salary">Ending Salary:</label>
<input type="text" id="ending_salary" name="employer_array[1][ending_salary]" value="" />

<label for="address">Business Address:</label>
<input type="text" id="address" name="employer_array[1][address]" value="" />

<label for="city">City:</label>
<input type="text" id="city" name="employer_array[1][city]" value="" />

<label for="state">State:</label>
<input type="text" id="state" name="employer_array[1][state]" value="" />

<label for="zip">ZIP:</label>
<input type="text" id="zip" name="employer_array[1][zip]" value="" />

<label for="reason_for_leaving">Reason for Leaving:</label>
<textarea type="text" rows="5" name="reason_for_leaving" name="employer_array[1][reason_for_leaving]"></textarea>

I'm using the save() method in my FieldType to turn the data into "|" separated text and store in the database:

public function save($data)
{

    if (is_array($data))

    {
        $data_string = "";

        for ($i=0; $i < count($data); $i++)
        {

            foreach ($data[$i] as $key => $val)
            {

                $data_string .= $key . "_" . $i . ": " . $val . "|";

            }
        }
    }

    $data = $data_string;

    return $data;

}

Since the notification template doesn't seem to pull that field data from the database table, what's the best way to manipulate that data before the notification template gets a hold of it? I don't understand where the notifications are pulling the data from and which function to use to process that data.

This is very similar to this post from 2 years ago except I'm attempting to utilize multi-dimensional arrays instead of a single array.

0

Incase anyone else runs into this matter in question, I did make it work. Now, I feel a little dirty with how I came about it but it's functioning great so far.

So, I ended up creating a custom FieldType called JSON, which uses the save method.

/**
 * store our data as json in the database
 * @access  public
 * @param   string data input
 * @return  string json
*/
public function save($data)
{
    // escape Tags, Apostrophes, Quotes, Ampersands, and Unicode
    $data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);

    return $data;
}

For the notification email, I embedded an EE template within my Freeform notification in order to work with the JSON field data. The template has PHP Parsing set to Output. I then used a {exp:query} tag to query the form's database table to pull in the saved JSON, decode and parse it into my template. (I also passed in the {freeform_entry_id} to the embedded template to assist with the query.)

Notification Template:

{embed="freeform/employment_application" entry_id="{freeform_entry_id}"}

Embedded Template:

{exp:query sql="SELECT form_field_126 FROM exp_freeform_form_entries_31 WHERE entry_id={embed:entry_id} ORDER BY entry_id DESC LIMIT 1;"}

<?php 

    // grab our stored JSON field
    $data = '{form_field_126}';

    // decode the json
    $data = json_decode($data, TRUE);

    // count number of employers and use for array
    $count = 1;

    for ($i=0; $i < count($data); $i++) {

        echo "<div style='border: 1px solid #DDDDDD; padding: 5px; margin: 5px;'";
        echo "<p><strong>Employer #$count</strong></p>";
        echo "<p>Employer: " . $data[$i]['employer'] . "<br />";
        echo "Occupation: " . $data[$i]['occupation'] . "<br />";
        echo "Supervisor: " . $data[$i]['supervisor'] . "<br />";
        echo "Phone#: " . $data[$i]['work_phone'] . "<br />";
        echo "Employed from: " . $data[$i]['employed_from'] . "<br />";
        echo "Employed to: " . $data[$i]['employed_to'] . "<br />";
        echo "Starting salary: " . $data[$i]['starting_salary'] . "<br />";
        echo "Ending salary: " . $data[$i]['ending_salary'] . "<br />";
        echo "Address: " . $data[$i]['address'] . "<br />";
        echo "City: " . $data[$i]['city'] . "<br />";
        echo "State: " . $data[$i]['state'] . "<br />";
        echo "Zip: " . $data[$i]['zip'] . "<br />";
        echo "Reason for leaving: " . $data[$i]['reason_for_leaving'] . "</p>";
        echo "</div>";

        $count++;

    }

?>

{/exp:query}

And there you have it. Multi-dimensional form inputs with Freeform using a JSON FieldType.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.