It would be great that instead of entity label/title we could configure the generated filename using tokens and allow other characters besides letters and numbers.

Something like

[entity:fieldname]-[entity:id]-[entity:title]-[entity:changed:custom:Ymd]

Comments

elpino created an issue. See original summary.

tame4tex’s picture

\Drupal\entity_print\FilenameGenerator is a service, therefore, it can be modified to suit whatever needs to you have. See https://www.drupal.org/docs/8/api/services-and-dependency-injection/alte... for help on this.

tame4tex’s picture

@benjy do you think it is worthwhile creating a filename alter event that enables any module to subscribe and alter the filename based on their own set of rules?

benjy’s picture

I wouldn't be against it if that's easier than replacing the service.

elpino’s picture

I've succesfully overriden the service, and indeed it's quite easy to do.

Having said that, I do believe a filename configuration page would be really useful, specially for those users who aren't developers.

maacl’s picture

Status: Active » Needs review
StatusFileSize
new4.82 KB

I had a use case for something similar and created an event dispatcher to alter the filename in a custom module. It works for me, but it is the first time creating something like this, and I am not fully sure I did it the right way.

maacl’s picture

StatusFileSize
new4.22 KB

Sorry, not used to create patches for drupal.org yet.

Also I am wondering if this should go in a separte issue and keep the scope on using tokens here, which could then use this event.

jayelless’s picture

StatusFileSize
new4.21 KB

Updated patch to correct the spelling of the get/set filename methods.

Status: Needs review » Needs work

The last submitted patch, 8: 3026667-8.patch, failed testing. View results

jayelless’s picture

Status: Needs work » Needs review
StatusFileSize
new4.21 KB

Missed one. This time should work.

lunk rat’s picture

Status: Needs review » Reviewed & tested by the community

I came across a use-case for the patch in #10 and I can report that it works well and I think there is a legitimate case for committing it. For others who want to try it: Apply the patch in #10 and then in your custom module at my_module/src/EventSubscribercreate a file MyModulePrintFilenameSubscriber.php with this as a starting point:


namespace Drupal\my_module\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\entity_print\Event\PrintEvents;
use Drupal\entity_print\Event\FilenameAlterEvent;

/**
 * My Module event subscriber.
 */
class MyModulePrintFilenameSubscriber implements EventSubscriberInterface {
  
  /**
   * {@inheritdoc}
   *
   * @return array
   *   The event names to listen for, and the methods that should be executed.
   */

  public static function getSubscribedEvents() {
    return [
      PrintEvents::FILENAME_ALTER => 'alterFilename',
    ];
  }

  public function alterFilename(FilenameAlterEvent $event) {
    
    // Do any logic you need to construct your preferred filename,
    // then call setFilenames and pass your filename as an array
    $my_filename = 'choice_' . 'is_' . 'yours';
    $event->setFilenames([$my_filename]);
  }
}

Then just add it to my_module.services.yml like this:

 my_module.print_filename_subscriber:
    class: Drupal\my_module\EventSubscriber\MyModulePrintFilenameSubscriber
    tags:
      - { name: event_subscriber }
ab2211’s picture

Does this work for the "PDF Download" at the webform submissions? I want to change the filenamee of the pdf.

hdbonnett’s picture

@ab2211 - yes, this works for me to change the name of the PDF File generated. I applied the patch from #10 and created a new event subscriber as described in #11.

use Drupal\Core\Utility\Token;
public function alterFilename(FilenameAlterEvent $event) {  
    $org = "Report for [custom:organisation]";
    $token_service = \Drupal::token();
    $new_filename  = $token_service->replace($org);
    $event->setFilenames([$new_filename]);
  }
ab2211’s picture

@hdbonnett: I don't have a custom module. I use it with webforms. So, just patching isn't enough?

macdev_drupal’s picture

#10 Works for me with webforms but I needed to load the values used for the filename from the submission.

Anyone got this working with WebformTokens too?

hdbonnett’s picture

Sorry @ab2211, I just saw your comment - you do need a custom module to implement the hook. I do this with a webform in a new EventSubscriber that I attach to my webforms.

namespace Drupal\webform_survey_handler\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use \Drupal;
use Drupal\entity_print\Event\PrintEvents;
use Drupal\entity_print\Event\FilenameAlterEvent;
use Drupal\Core\Utility\Token;
/**
 * SurveyWebformHandlerPrintFilenameSubscriber event subscriber.
 */
class SurveyWebformHandlerPrintFilenameSubscriber implements EventSubscriberInterface {
  
  /**
   * {@inheritdoc}
   *
   * @return array
   *   The event names to listen for, and the methods that should be executed.
   */

  public static function getSubscribedEvents() {
    return [
      PrintEvents::FILENAME_ALTER => 'alterFilename',
    ];
  }

  public function alterFilename(FilenameAlterEvent $event) {
   
    // Call the TokenService to build filename
    // then call setFilenames and pass your filename as an array
    
    $org = "Report for [survey:organisation]";
    $token_service = \Drupal::token();
    // Replace the token for filename.
    $new_filename  = $token_service->replace($org);
    $event->setFilenames([$new_filename]);
  }
}

The file webform_survey_handler.services.yml contains:

services:
  webform_survey_handler.print_filename_subscriber:
    class: Drupal\webform_survey_handler\EventSubscriber\SurveyWebformHandlerPrintFilenameSubscriber
    tags:
      - { name: event_subscriber }
jaydee1818’s picture

This worked for me. Thanks.

vladimiraus’s picture

Status: Reviewed & tested by the community » Fixed

Thank you.

  • VladimirAus committed de47987e on 8.x-2.x authored by jlscott
    Issue #3026667 by maacl, jlscott, elpino, tame4tex, ab2211, hdbonnett,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gngn’s picture

Just because I stumbled upon it: the link in #2 is truncated.
I think it should be
/docs/drupal-apis/services-and-dependency-injection/altering-existing-services-providing-dynamic-services