I would like the ability to rewrite the name of the PDF that is generated using the tokenized replacement patterns that are available. Each time a pdf is generated from an existing template, the only difference in the file names is an appended numerical value like exampleFILLPDFpdf_0.pdf, exampleFILLPDFpdf_1.pdf, exampleFILLPDFpdf_2.pdf, etc...

The problem with this is that we need to be able to link to the pdf file that is stored on the server, and without knowing what the name of the file is, we can't do that.

Comments

landing’s picture

Okay - I figured out how to rename files by using token values in the Title field found at admin/content/fillpdf/fid ... here's the code I added to the fillpdf.module file:

function _fillpdf_process_new_filename($filename_new, $token_objects) {
  
  // Replace any applicable tokens
  $types = array();
  if (isset($token_objects['node'])) {
    $types[] = 'node';
  }
  elseif (isset($token_objects['webform'])) {
    $types[] = 'webform';
  }
  
  foreach ($types as $type) {
    $filename_new = token_replace($filename_new, $type, $token_objects[$type]);
  }
  return $filename_new;
}

Then on line 308 I changed

  $output_name = preg_replace('/[^a-zA-Z0-9_]/', '', $fillpdf_info->title) .'.pdf';

to


 $output_name = _fillpdf_process_new_filename($fillpdf_info->title, $token_objects) .'.pdf'; 

wizonesolutions’s picture

Status: Active » Needs work

Thanks for your contribution. Can you roll this as a patch? It'll be much easier to deal with - and more likely to get in if you want this feature in the module in the future. I have no objections to it - I think I was just waiting for someone to ask.

Patch information is at http://drupal.org/patch.

landing’s picture

Title: Rewriting file names using replacement patterns » Rewritting file names using replacement patterns
Version: 7.x-1.x-dev » 6.x-1.15
Component: Code » Miscellaneous
Status: Needs review » Needs work

Can I send you my modified module file so you can create the patch?

wizonesolutions’s picture

Version: 6.x-1.15 » 7.x-1.1

New features are developed in 7.x first and then backported currently. I'll look into this the next time I'm adding new features. Patches of course welcome. @landing - you don't need to use Git to make a patch. You can also just copy the file, then make your changes, then use diff -uP oldfile newfile > fillpdf_1264872_title_tokens.patch (for example).

wizonesolutions’s picture

Version: 7.x-1.1 » 7.x-1.x-dev

Oops.

wizonesolutions’s picture

Title: Rewritting file names using replacement patterns » Rewriting file names using replacement patterns
wizonesolutions’s picture

Component: Miscellaneous » Code
wizonesolutions’s picture

StatusFileSize
new2.58 KB

Finally - here's a patch to add this functionality to Drupal 7. It uses the existing Title field of the PDF (at the top of the form, in case it's become invisible to you like it has me).

wizonesolutions’s picture

StatusFileSize
new2.58 KB

Oops - that had a nasty bug in it that prevented sanitization of the filename (returned too early). Use this instead.

wizonesolutions’s picture

Status: Needs work » Needs review
wizonesolutions’s picture

StatusFileSize
new3.54 KB

Discovered a bug where forms without any replacements set up wouldn't replace the tokens in the title. I don't know why anyone would do this, but now if they want to, they can!

Full patch.

wizonesolutions’s picture

Title: Rewritting file names using replacement patterns » Rewriting file names using replacement patterns
Version: 6.x-1.15 » 7.x-1.x-dev
Component: Miscellaneous » Code
Status: Needs work » Needs review

Oh yeah, and the latest one was rolled against the latest -dev, a.k.a. 7.x-1.5 (needless to say).

wizonesolutions’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
Status: Needs review » Patch (to be ported)
StatusFileSize
new5.08 KB

OK, this is the patch I committed in the end - realized I needed to indicate that the Title field supports tokens! So added a description and a TOKENS fieldset under it.

wizonesolutions’s picture

landing wrote some code for this, so I can probably base the D6 backport off that (plus what D7 now has).

alexborsody’s picture

wizonesolutions’s picture

Status: Patch (to be ported) » Reviewed & tested by the community

If this is working, I approve. Commit 'er!

wizonesolutions’s picture

Status: Reviewed & tested by the community » Closed (fixed)

I saw that this got committed a while ago.

wizonesolutions’s picture

De-tag also.

  • Commit 1fea002 on 7.x-1.x, 7.x-2.x, 7.x-2.x-tests1, 7.x-1.x-ubercartbackporttest by wizonesolutions:
    Issue #1264872: Replace tokens in Title field.
    
    
  • Commit 837a4ad on 7.x-1.x, 7.x-2.x, 7.x-2.x-tests1, 7.x-1.x-ubercartbackporttest by wizonesolutions:
    Issue #1264872: Add token fieldset under Title.