Override entity_print filename on Drupal 8/9.

Barbara Bombachini
Code Snippets
Published in
2 min readSep 29, 2022

Often times clients want to change the PDF document filename created by entity_print module.

One of the ways to make this happens is to create a service extending ServiceProviderBase and alter the class that will process this filename.

For that, create a new file at the /src folder on your custom module and extend the ServiceProviderBase. Ex: /cms_module/src/CmsServiceProvider.php

<?phpnamespace Drupal\cms_module;use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;
/**
* Modifies the entity_print FilenameGenerator service.
*/
class CmsServiceProvider extends ServiceProviderBase {
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
$definition = $container
->getDefinition('entity_print.filename_generator');
$definition->setClass('Drupal\cms_module\GenerateFilename')
->addArgument(new Reference('transliteration'));
}
}

In my case, I’m calling the class GenerateFilename to process the filename for me. So on /cms_module/src/GenerateFilename.php we have:

<?phpnamespace Drupal\cms_module;use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\entity_print\FilenameGeneratorInterface;
/**
* A service override for generating filenames for printed documents.
*/
class GenerateFilename implements FilenameGeneratorInterface { /**
* The transliteration service.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
protected $transliteration;
/**
* FilenameGenerator constructor.
*
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* Transliteration service.
*/
public function __construct(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
/**
* {@inheritdoc}
*/
public function generateFilename(array $entities, callable $entity_label_callback = NULL) {
$filenames = [];

/** @var \Drupal\Core\Entity\EntityInterface $entity */
foreach ($entities as $entity) {
if ($label = trim($this->sanitizeFilename($entity_label_callback ? $entity_label_callback($entity) : $entity->label(), $entity->language()->getId()))) {
$filenames[] = $label; }
}
return $filenames ? implode('-', $filenames) : static::DEFAULT_FILENAME;}/**
* Gets a safe filename.
*
* @param string $filename
* The un-processed filename.
* @param string $langcode
* The language of the filename.
*
* @return string
* The filename stripped to only safe characters.
*/
protected function sanitizeFilename($filename, $langcode) {
$transformed = $this->transliteration->transliterate($filename, $langcode); $clean = preg_replace("/[^A-Za-z0-9 ]/", '', $transformed);
$clean = strtolower(str_replace(" ", "_", $clean));
return $clean; }
}

Medium needs to improve a lot to be bad, but I hope this can give you some idea on how to implement these methods to override the PDF filename.

--

--