Skip to main content

gfpdf_field_class

Description

This is a complex filter used to override how a supported field is rendered in Gravity PDF. For more simple cases, the gfpdf_pdf_field_content filter will be the better option.

When rendering PDFs, we split the Gravity Forms fields display into its own class, which all extend our common abstract class GFPDF\Helper\Helper_Abstract_Fields. The most common method to override is value() or html(), as they control the retrieval of the field's entry information and the HTML output. However, you can completely change all properties of a field if you desire.

In the usage section we show you how to override an existing field's value() method to change the output. But for a more in-depth look at how to extend the GFPDF\Helper\Helper_Abstract_Fields class we recommend you review the field classes found in the plugin's /src/helper/fields/ directory.

You can also use the filter gfpdf_field_class_$type, where type refers to the Gravity Forms field type (textarea, select, signature etc).

Parameters

$class | object

  • The current field class loaded. These classes are located in src/helper/fields/

$field | array

  • The current Gravity Forms field being processed.

$entry | array

  • The raw Gravity Forms Entry array.

$form | array

  • The current Gravity Forms array

Usage

The following snippet shows you how to use the filter to override the Textarea class. The actual filter is easy to use, but you'll also need to create a separate file for your field override class:

add_filter( 'gfpdf_field_class', function( $class, $field, $entry ) {
/* Only override the textarea field */
if ( 'textarea' == $field['type'] ) {
/* Include the new field class which will override the current textarea class */
require_once( __DIR__ . '/Prefix_Allcaps_Textarea_Field.php' );

/* Initialise the new class so it can be returned */
$class = new GFPDF\Helper\Fields\Prefix_Allcaps_Textarea_Field( $field, $entry, GPDFAPI::get_form_class(), GPDFAPI::get_misc_class() );
}

return $class;
}, 10, 3 );

Alternatively we could have used the gfpdf_field_class_$type filter and removed the IF statement:

add_filter( 'gfpdf_field_class_textarea', function( $class, $field, $entry ) {
/* Include the new field class which will override the current textarea class */
require_once( __DIR__ . '/Prefix_Allcaps_Textarea_Field.php' );

/* Initialise the new class so it can be returned */
$class = new GFPDF\Helper\Fields\Prefix_Allcaps_Textarea_Field( $field, $entry, GPDFAPI::get_form_class(), GPDFAPI::get_misc_class() );

return $class;
}, 10, 3 );

And here's the accompanying Prefix_Allcaps_Textarea_Field.php file which should be in the same directory as the file the gfpdf_field_class filter above is placed:

<?php

/**
* The following class should be placed in a separate PHP file in the same directory as the file you included the `gfpdf_field_class` filter.
* We'll override the current Textarea_Field class with our new Prefix_Allcaps_Textarea_Field class.
* This will allow us to force allcaps on all textarea fields in the PDFs
*/
namespace GFPDF\Helper\Fields;

/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Controls the display and output of the textarea field
*
*/
class Prefix_Allcaps_Textarea_Field extends Field_Textarea {

/**
* Get the standard GF value of this field
*
* @return string|array
*
* @since 4.0
*/
public function value() {

if ( $this->has_cache() ) {
return $this->cache();
}

$this->cache( nl2br( mb_strtoupper( $this->get_value() ) ) );

return $this->cache();
}
}

For developers interested in adding support for new fields you don't need to use this filter. Instead, create a class that extends GFPDF\Helper\Helper_Abstract_Fields and then require it during the init action. The class you create needs to adhere to the appropriate naming convention to be autoloaded by Gravity PDF (capitalize the first character in $field->type, and repeat after every underscore). Refer to Helper_Misc::get_field_class($type) to see the logic used for the autoloader.

/**
* Register a new field with Gravity PDF
*/
add_action( 'init', function() {
if ( ! class_exists( '\GFPDF\Helper\Helper_Abstract_Fields' ) ) {
return;
}

require_once( __DIR__ . '/Field_Colorpicker.php' );
} );

Source Code

This filter is located in the Model_PDF::get_field_class() method of /src/model/Model_PDF.php.