PDF for GravityView Hooks – gfpdf_gv_field_middleware
Description
Leave a View field out of the PDF based on who's downloading it, what the entry contains or which PDF is being generated. For a fixed choice, use the field's Exclude from PDF? setting instead (see Field Visibility and Exclusions).
Return true to leave the field out, or return $skip unchanged to leave the decision alone. The plugin's own checks never turn true back to false.
The filter runs for every field in the Single Entry PDF and the Multiple Entries PDF, before gfpdf_gv_view_field. GravityView's action fields, such as the Edit Entry link, are already removed by default. Change that list with gfpdf_gv_internal_fields_to_skip.
Version
This hook was introduced in PDF for GravityView 0.1.
Arguments
The following arguments are passed to functions that use this hook:
$skip | bool
- Return
trueto leave the field out of the PDF. - The default value is
false.
$field | \GV\Field
- The View field being checked.
$field->IDis the form field ID (or a GravityView field name such asentry_link),$field->typeis the field type, and$field->positionis its zone, such assingle_table-columnsordirectory_list-title.
$field_entry | \GV\Entry or null
- For a Single Entry PDF, the entry the field belongs to. If the View joins forms with GravityView's Multiple Forms extension, it's the entry from the field's own form.
- For a Multiple Entries PDF it's
null. The decision is made once for the whole PDF, so a field appears in every row or none. You can't remove a field from a single row based on its value.
$field_form | \GV\GF_Form
- The form the field belongs to.
$view | \GV\View
- The View the PDF is being generated for.
$context | \GV\Template_Context
- GravityView's template context for the field. On a Multiple Entries PDF,
$context->entriesholds the\GV\Entry_Collectionbeing rendered.
Usage
Only include field 12 (internal notes) when the person downloading the PDF can edit entries:
add_filter( 'gfpdf_gv_field_middleware', function( $skip, $field, $field_entry, $field_form, $view, $context ) {
if ( (string) $field->ID === '12' && ! current_user_can( 'gravityview_edit_entries' ) ) {
return true;
}
return $skip;
}, 10, 6 );
Leave the notes field (ID 12) out of a Multiple Entries PDF that holds more than 100 entries:
add_filter( 'gfpdf_gv_field_middleware', function( $skip, $field, $field_entry, $field_form, $view, $context ) {
if ( $field_entry === null && (string) $field->ID === '12' && $context->entries->count() > 100 ) {
return true;
}
return $skip;
}, 10, 6 );
On a Single Entry PDF, leave out the dietary requirements field (ID 8) when the answer was "None":
add_filter( 'gfpdf_gv_field_middleware', function( $skip, $field, $field_entry, $field_form, $view, $context ) {
if ( $field_entry && (string) $field->ID === '8' && rgar( $field_entry->as_entry(), '8' ) === 'None' ) {
return true;
}
return $skip;
}, 10, 6 );
Source Code
This filter is located in src/Pdf/Renderer.php of the PDF for GravityView extension.