Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save travislopes/e2e7d8642e2fd38b3d671b996dd9798d to your computer and use it in GitHub Desktop.
Save travislopes/e2e7d8642e2fd38b3d671b996dd9798d to your computer and use it in GitHub Desktop.
<?php
add_filter( 'fg_fillable_pdf_args', 'add_list_field_to_pdf', 10, 4 );
function add_list_field_to_pdf( $pdf_meta, $feed, $entry, $form ) {
// Prepare column names for each day of the week.
$column_names = array(
'Sunday %d',
'Monday %d',
'Tuesday %d',
'Wednesday %d',
'Thursday %d',
'Friday %d',
'Saturday %d',
);
// Set maximum number of rows.
$max_rows = 4;
// Get List items.
$list_items = rgar( $entry, 4 ); // Replace 4 with your List field ID.
$list_items = maybe_unserialize( $list_items );
// If there are no List items, return.
if ( empty( $list_items ) ) {
return $pdf_meta;
}
// Loop through List rows up to the maximum row count.
for ( $i = 0; $i < ( $max_rows + 1 ); $i++ ) {
// Get List row.
$list_row = rgar( $list_items, $i );
// If List row is not found, skip.
if ( ! $list_row ) {
continue;
}
// Remove array keys from List row contents.
$list_row = array_values( $list_row );
// Loop through each column in the row.
foreach ( $list_row as $col => $value ) {
// Get the PDF key for this column.
$pdf_key = sprintf( $column_names[ $col ], ( $i + 1 ) );
// Add value if it is not empty.
if ( ! rgblank( $value ) ) {
$pdf_meta['field_values'][ $pdf_key ] = $value;
}
}
}
return $pdf_meta;
}
@dhitimedin
Copy link

Hi Travis,

I have a fillable-pdf installed but I am unable to find the hook 'fg_fillable_pdf_args' in my install. Can you share details on where and when I would get this hook.

My intention is to change the name of the generated pdf before it is stored. Let me know if my assumption is right that this filter would help me do that.

@travislopes
Copy link
Author

@dhitimedin You can learn all about the filter on its documentation page: https://cosmicgiant.com/documentation/fillable-pdfs/fg_fillablepdfs_pdf_args/

@dhitimedin
Copy link

Hi Travis,

I had gone through it. My question was that add_filter( 'fg_fillable_pdf_args', 'add_list_field_to_pdf', 10, 4 ); is not getting called. I could not find any reference of fg_fillable_pdf_args hook in my code repository.

Is it that I missed any code? is it that I need to install any other plugin add on? I would like to understand that.

Regards,
Nishit

@travislopes
Copy link
Author

The filter name is fg_fillablepdfs_pdf_args.

@dhitimedin
Copy link

Hi Travis,

Thank you for your response. I am able to attach a method to the hook 'fg_fillablepdfs_pdf_args', however, I am unable to meet my objective.

I want to change the file name and attach a suffix based on a field entry. The file_name is changing but the physical_file_name remains the same.

Is there a way to customize the name of the pdf file ?

Regards,
Nishit

@travislopes
Copy link
Author

The physical_file_name is unable to modified using the filter. If you're regenerating an existing PDF, the physical file name will never change even if the publicly visible file name changes through feed settings updates or using the filter.

Any additional questions about use of this filter should be directed to our support form: https://cosmicgiant.com/account/request-support/

@dhitimedin
Copy link

Hi Travis, Thank you. I used rename() for the same.

Regards,
Nishit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment