Create a fillpdf.api.php and document hooks there.

Original report

I have a token from webform [webform:webform-val-number_of_guests] that I want to multiple by ticket price to insert that into a field in the PDF.

Can you provide any help on this?

Thanks.

Comments

wizonesolutions’s picture

Version: 7.x-1.4 » 7.x-1.x-dev
Assigned: Unassigned » wizonesolutions
Category: support » task
Issue tags: +Documentation

We currently don't yet have a fillpdf.api.php file, though we should. That said, you simply implement hook_fillpdf_merge_pdf_fields_alter(&$fields, $context); and change the fields as needed. Your modified values will be placed into the PDF. Note that this happens after field transformations (as configured in the UI) have occurred.

Fields are indexed by PDF key. The value is the array element value. $context contains 'nodes' and 'webforms', which contain loaded nodes and webforms corresponding to the Fill PDF URL. There won't be any webforms in there if you aren't filling a webform, and the nodes will correspond to the webform node if you are.

So here is an example. Untested but gives a starting point:

/**
  * Implements hook_fillpdf_merge_fields_alter().
  */
function mymodule_fillpdf_merge_fields_alter(&$fields, $context) {
  $submission = end($webforms); // Gets the last sid passed.
  $ticket_price = 5.00; // @todo: Replace with code to get this from the webform object.
  $fields['Number of Guests'] *= $ticket_price; // Multiply number of guests by ticket price.
}

Does this help?

Also changing this to a task to document this hook.

bjalford’s picture

perfect - thanks

wizonesolutions’s picture

Assigned: wizonesolutions » Unassigned
Issue tags: +fillpdf2

Need to create fillpdf.api.php and document the other hooks as well.

wizonesolutions’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
kiricou’s picture

I try to do the same thing, I create a function

function form_template_fillpdf_merge_fields_alter(&$fields, $context) {
  $fields['Labo'] = 'test'; 
}

in my own module named form_template which contain other functions which work.

Nothing different when FillPDF generate the PDF.
When I put this line

  $fields['Labo'] = 'test'; 

just under the

drupal_alter('fillpdf_merge_fields_alter', $fields, $context);

in FillPDF module, it works.

I uninstall and reinstall my module but nothing.

Any idea for debugging?
drupal 7.22
FillPDF 7.x-1.9
Thanks

wizonesolutions’s picture

Is your module named form_tempalte? Try clearing the cache. Your function signature looks correct. Doing it in your module should work as well, particularly if it works there.

kiricou’s picture

Yes, the name of the module is correct others hooks work.

But I work with Fill PDF 7.x-1.9, maybe it's better with dev version ?

Thanks

kiricou’s picture

if I rename my function :
function form_template_fillpdf_merge_fields_alter_alter(&$fields, $context) {
two times _alter

it works !!

i don't know why... but it works

kiricou’s picture

I need to add new line in the field I alter, how to do that ?

Like "Line 1\nLine2" ...

Thanks

Simon

wizonesolutions’s picture

Ah. You're right about the function signature. That's not what I intended, but that is currently the right way to do it.

To add a new line, trying adding <br> or <br /> to the output. There is code somewhere in fillpdf_merge_pdf that replaces it with newlines.

kiricou’s picture

Thank you but '<br />' does not work.
To add a new line, simply

fields['labo']="1 line
line2
";

and the form field has to support multi line !

Anonymous’s picture

The row should be drupal_alter('fillpdf_merge_fields', $fields, $context,$fillpdf_info); in the file fillpdf.module.

Alter will be concatenated in the drupal_alter function. The additional fillpdf_info gives the possibility to check which pdf-template you are using.

Can be checked like this:

if(endswith($fillpdf_info->url,'Invoice_Template.pdf'))

wizonesolutions’s picture

filantrop: Yes, I said that in comment #10 :)

Patch welcome in a separate issue to fix the callback name. It can be changed completely in 7.x-2.x, but the correct version can only be added as an alias for the current version in 7.x-1.x (can't break the API in 7.x-1.x). Thanks.

Thanks for the comments and usage examples! If you want to put these in the Fill PDF documentation, you are welcome. Everyone can edit documentation.

oenie’s picture

I encountered the same problem, added a patch in #2140435: Confusing hook name 'hook_fillpdf_merge_fields_alter_alter'.

liam morland’s picture

Version: 7.x-2.x-dev » 8.x-4.x-dev
Issue summary: View changes
liam morland’s picture

Component: Code » Documentation
Issue tags: -Documentation, -fillpdf2
allahnoor turab’s picture

For me it is not working with drupal version 9.5.2 and fillpdf version 5.0
I not see any output.
I want this hook to remove prefix and change date format for some fields
/**
* Implements hook_fillpdf_merge_fields_alter().
*/
function miscellaneous_fillpdf_merge_fields_alter(&$fields, $context) {
dump($fields);
// $submission = end($webforms); // Gets the last sid passed.
// $ticket_price = 5.00; // @todo: Replace with code to get this from the webform object.
// $fields['The rent is'] *= $ticket_price; // Multiply number of guests by ticket price.
}

liam morland’s picture

I thought dump() was a Twig function. It won't work in a hook implementation. If devel module is enabled, you should be able to use dpm().

allahnoor turab’s picture

No success, actually the hook is not triggering.

liam morland’s picture

Title: Example of how to use hook to alter value » Document hooks in fillpdf.api.php
Version: 8.x-4.x-dev » 7.x-1.x-dev
Issue summary: View changes

hook_fillpdf_merge_fields_alter() only exists in Drupal 7.

liam morland’s picture

Status: Active » Closed (outdated)

Drupal 7 is no longer supported. If this applies to a supported version, please re-open.