PDF for GravityView Hooks – gfpdf_gv_template_arguments
Description
Changes the variables passed to a PDF for GravityView template, or adds your own. Use it to get extra data into a template override without querying for it inside the template.
Each key in the $args array becomes a variable in the template. For example, $args['view'] is also available as $view.
The filter runs once per PDF, for both the Single Entry PDF and the Multiple Entries PDF, just before the template is loaded.
When the render cache is on, a cached PDF is served without running the template or this filter. If the data you add changes over time, independent of the entry, the View or its settings, the cached copy is served until it expires. See gfpdf_gv_cache_ttl.
Version
This hook was introduced in PDF for GravityView 0.1.
Arguments
The following arguments are passed to functions that use this hook:
$args | array
- The variables passed to the PDF template. PDF for GravityView adds these four:
view: the\GV\Viewbeing rendered.view_entry: a\GV\Entryfor a Single Entry PDF, or a\GV\Entry_Collectionfor a Multiple Entries PDF.view_template: the GravityView template class the layout uses on the website. A\GV\Entry_Templatefor a Single Entry PDF, or a\GV\View_Templatefor a Multiple Entries PDF.view_fields: the fields to be rendered, grouped by their zone in the View editor (such assingle_table-columnsordirectory_list-title). Fields removed by gfpdf_gv_field_middleware or the Exclude from PDF? setting are already gone, and gfpdf_gv_view_field has already run.
- The rest come from Gravity PDF:
form_id,lead_id,form,entry,form_data,fields,config,settingsandgfpdf. See Gravity PDF'sgfpdf_template_argsdocumentation. settingsholds the resolved PDF settings from the View's Single Entry PDF or Multiple Entries PDF tab, such asmargin,show_title,show_labelsandadditional_css.- A Multiple Entries PDF has no single entry.
entryonly holdsid(every entry ID in the PDF, comma-separated) andform_id,form_datais built from the first entry, andmpdf_writeris added after this filter runs.
$view | \GV\View
- The View the PDF is being generated for.
$entry | \GV\Entry or \GV\Entry_Collection
- A
\GV\Entryfor a Single Entry PDF. - A
\GV\Entry_Collectionfor a Multiple Entries PDF.
$pdf_settings | array
- The resolved PDF settings, after gfpdf_gv_pdf_settings or gfpdf_gv_pdf_settings_multiple has run. The same array as
$args['settings'].
Usage
Pass the current user's display name to your templates as $prepared_for:
add_filter( 'gfpdf_gv_template_arguments', function( $args, $view, $entry, $pdf_settings ) {
$user = wp_get_current_user();
$args['prepared_for'] = $user->exists() ? $user->display_name : '';
return $args;
}, 10, 4 );
Pass the number of entries to Multiple Entries PDF templates only:
add_filter( 'gfpdf_gv_template_arguments', function( $args, $view, $entry, $pdf_settings ) {
if ( $entry instanceof \GV\Entry_Collection ) {
$args['entry_count'] = $entry->count();
}
return $args;
}, 10, 4 );
Source Code
This filter is located in src/Pdf/Renderer.php of the PDF for GravityView extension.