Keep up to date with Gravity PDF

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:

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 Form 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:

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:

<?php if ( $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 use 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:

<?php if ( $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>
<?php endif; ?>

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.

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

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:

https://gravitypdf.com/pdf/12i410491024123/100/?data=1

If we take a look at the $form_data array from the tutorial above you’ll see a simple structured associative array:

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 for how to access the most common Gravity Form fields.

/* 82 is the ID of our field */
echo $form_data['section_break'][82]['title']; /* not found in the 'fields' sub-key */
echo $form_data['section_break'][82]['description']; /* not found in the 'fields' sub-key */

/*
* 39 is the ID of our field
* The signature details isn't in the 'field' sub-key
*/
echo $form_data['signature_details_id'][39]['img']; /* a <img /> HTML tag that includes the signature */
echo $form_data['signature_details_id'][39]['path']; /* the full path to the signature */
echo $form_data['signature_details_id'][39]['url']; /* the URL to the signature */
echo $form_data['signature_details_id'][39]['width']; /* the signature width in pixels */
echo $form_data['signature_details_id'][39]['height']; /* the signature height in pixels */
/* Output signature if it exists (this prevents a red X showing up in the PDF when the image doesn't exist) */
if ( is_file( $form_data['signature_details_id'][39]['path'] ) ) {
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 */
}

/*
* 3 is the ID of our field
*/
echo $form_data['field'][3][0]; /* The first user-selection in the chain */
echo $form_data['field'][3][1]; /* The second user-selection in the chain */
/* Output all answers, separated by a comma */
echo implode( ', ', $form_data['field'][3] );

/* 310 is the ID of our field */
echo $form_data['field'][310];
/**
* Advanced Product Option Information is available through the parent product field it's assigned to
* For example, if this field is linked to product field #300 and would be accessible via $form_data['products'][300]['quantity']
*/
echo $form_data['products'][300]['quantity'];