PDF for GravityView Hooks – gfpdf_gv_cache_key_extra
Description
Use this filter to add values to a PDF's cache key when your code changes the PDF based on something the cache doesn't track, such as a cookie, a URL parameter or the current month.
A cached PDF is matched to the entry, View, form, PDF settings, user and site language (see Caching). Anything else your code depends on is invisible to the cache, so the old copy keeps being served. Each value you add gets its own cached PDF, and the PDF is regenerated when the value changes.
Your values are added to the key. They can't replace or remove the built-in values, so this filter can't make one user's PDF show for another user.
Only add a value the PDF depends on, and keep the number of possible values small, since each one is cached separately. Visitors control values from the URL or a cookie, so check them against a list of allowed values (as in the example below).
If the PDF depends on a visitor's search, use gfpdf_gv_cache_key_request_params instead. To stop caching a View's PDFs, use gfpdf_gv_enable_pdf_cache.
Version
This hook was introduced in PDF for GravityView 2.0.
Arguments
The following arguments are passed to functions that use this hook:
$extra | array
- The values added to the cache key, keyed by name. Use a unique name and return the array with your value added.
- The default value is an empty array. The plugin adds its own values, such as the search values and the map settings.
$view | \GV\View
- The View the PDF is being generated for.
$entry | \GV\Entry or \GV\Entry_Collection
- The entry for a Single Entry PDF, or the entries in a Multiple Entries PDF.
$context | string
- The PDF type:
single(Single Entry PDF) ordirectory(Multiple Entries PDF).
$view_fields | array
- The fields included in the PDF.
Usage
Cache a separate PDF for each currency, when a custom template displays prices in the currency set in a currency cookie:
add_filter( 'gfpdf_gv_cache_key_extra', function( $extra, $view, $entry, $context, $view_fields ) {
if ( (int) $view->ID !== 12 ) {
return $extra;
}
$currency = $_COOKIE['currency'] ?? 'USD';
$extra['my_currency'] = in_array( $currency, [ 'USD', 'EUR', 'GBP' ], true ) ? $currency : 'USD';
return $extra;
}, 10, 5 );
Source Code
This filter is located in src/Pdf/Caching/Cache.php of the PDF for GravityView extension.