Gravity Forms merge tags and conditional shortcodes are useful PDF-building tools, but there are drawbacks. For instance, you cannot create nested conditionals or do any post-processing to the entry data β you aren't able to determine the age of someone by their date of birth, or convert a field to upper case. To achieve this behaviour we need to utilise PHP and the $form_data associative array β a formatted version of the $entry object.
First, we're going to convert the template we did in the Part 2 exercise from merge tags/conditional shortcodes to PHP. Then we'll look at the two post-processing use-cases we discussed in the introduction. Finally, we'll take a closer look at the $form_data array.
Our Hello World template included merge tags for field #1 and field #3 β our name field and drop down, respectively. Their equivalent access keys in the $form_data array are $form_data['field'][1]['first'] and form_data['field'][3]. So we'll update the PDF template to:
<p>You're from <?= $form_data['field'][3]; ?>, <?= $form_data['field'][1]['first'];?>? How cool is that!</p>
info
The $form_data array is grouped into a number of different sub-arrays, but the most common is $form_data['field']. As the name suggests, it contains the field data for most Gravity Forms field β add-ons that add new fields are stored in separate sub-arrays.
To keep your code cleaner you could set these values as variables earlier in the template and output the variable names in the template instead:
<?php
$location=$form_data['field'][3];
$name=$form_data['field'][1]['first'];
?>
<p>You're from <?=$location;?>,<?=$name;?>? How cool is that!</p>
If you're accessing a lot of fields from $form_data['field'] you might like to assign it a shorter variable name:
<?php
$f=$form_data['field'];
$location=$f[3];
$name=$f[1]['first'];
?>
<p>You're from <?=$location;?>,<?=$name;?>? How cool is that!</p>
When doing conditionals with the $form_data array, if your string comparison contains any of the following characters <, >, ", ' or & you'll need to use the WordPress function esc_html() to get the desired result.
Example: if ( $form_data['field'][5] === esc_html( 'Honey & Spice' ) ):'
The conditional shortcodes we used in our Hello World template are basic IF x = y THEN logic. Now we have access to the location field in PHP it's trivial to replace in PHP:
<?phpif($location==='Earth'):?>
<p>The birth-rate on Earth has dropped almost 25% in the past 50 years due to colonisation of the solar system.</p>
<?php endif;?>
<?php if($location==='Moon'):?>
<p>The lunar colony was first established in 2115 with a population of 200. Now it supports over 900,000 people.</p>
<?php endif;?>
<?php if($location==='Mars'):?>
<p>Mars was the second body to be colonised in the solar system in 2135,20 years after the moon.</p>
<?php endif;?>
<?php if($location==='Titan'):?>
<p>Titan's colony is only recently established. You're one of only 500 people currently living there!</p>
<?php endif;?>
You could have also used a switch statement if you wanted:
switch($location){
case'Earth':
echo'<p>The birth-rate on Earth has dropped almost 25% in the past 50 years due to colonisation of the solar system.</p>';
break;
case'Moon':
echo'<p>The lunar colony was first established in 2115 with a population of 200. Now it supports over 900,000 people.</p>';
break;
case'Mars':
echo'<p>Mars was the second body to be colonised in the solar system in 2135, 20 years after the moon.</p>';
break;
case'Titan':
echo"<p>Titan's colony is only recently established. You're one of only 500 people currently living there!</p>";
break;
}
And IF/ELSE conditions are simple too:
<?phpif($location==='Earth'):?>
<p>The birth-rate on Earth has dropped almost 25% in the past 50 years due to colonisation of the solar system.</p>
<?php else:?>
<p>Planning a vacation? Earth has thousands of beautiful destinations.</p>
With the full power of PHP at your fingertips post-processing Gravity Form data becomes easy. Case in point, it's relatively simple to determine the age of someone using their date of birth.
Let's go back to the Hello World sample form and add a date field. For simplicities sake, name it Date of Birth and change the Date Format to dd-mm-yyyy then save your form (the date format is important to prevent ambiguity when using PHP's strtotime() function). Finally, submit a new entry and enter a date of birth.
Now let's add our PHP logic to display the age in your PDF template:
<?php
$dob=$form_data['field'][4];/* change the ID if your date field has something different */
<p>Congratulations! You're <?=$age;?> years old.</p>
Another example is converting case. It's simple to convert entry data to upper case, lower case or sentence case. Just run the field through a PHP function like mb_strtoupper() or strtoupper() (we recommend using the multibyte PHP functions where possible).
<?php
$location=mb_strtoupper($form_data['field'][3],'UTF-8');/* strtoupper( $form_data['field'][3] ) is also suitable */
?>
<?=$location;?>
PDF templates are just PHP files that are loaded in WordPress. Anything you can do in WordPress you can do in the PDF templates. For instance, you can output entire posts in a PDF:
<?php
$post=get_post(120);/* get the post with an ID of 120 */
echoapply_filters('the_title',$post->post_title);/* output the post title */
echoapply_filters('the_content',$post->post_content);/* output the post content */
The $form_data array is used to access the Gravity Form entry information, but, unlike merge tags, there's no selector to show you what's actually in the array. That's why we've added a data URL parameter which shows you the complete contents of the $form_data array.
To see the array, first view a PDF in your admin area. When it loads add ?data=1 to the URL and reload. The address should look similar to this:
As we mentioned earlier, our main sub-key is $form_data['field'], but there are other important details like the submission date $form_data['date_created'] and the entry owner's user ID $form_data['misc']['created_by']. Each Gravity Form is unique and will create a different $form_data array. Gravity Form Add-Ons like Survey and Signature also add their own unique sub-keys in the array.
You will also notice in our $form_data['field'] array there are three different references (array keys) for the same field. The keys without any ID are present for legacy reasons, while keys with the ID-and-field-combination make it easier to distinguish what field you are trying to reference. With that said, we recommend accessing the field data using the ID in your templates.
The $form_data variable is an associative array, so more experienced PHP developers should have no problem accessing the information they need. However, large forms can make this array intimidating for first time template builders. Below are examples of how to access the most common Gravity Forms fields.
echo'<img src="'.$form_data['signature_details_id'][39]['path'].'" width="150" />';/* best to use the path to reference the image and check it exists on the server first */