Ibexa DXP Discussions

Community discussion forum for developers working with Ibexa DXP

Render template to a variable

eZ Platform 3-alpha

How can I render a content view full template into a variable from inside a publish version event listener?

If there is a better way to generate a PDF on publish, please let me know.

I have an onPublishVersionEvent listener as a service and I would like to have it create a PDF using KNP/Snappy/PDF when content objects of specific content types are published.

App\EventListener\PublishListener:
    arguments:
        $knpSnappyPdf: '@knp_snappy.pdf'
        $pdfOnPublish: [ 13, 17 ]
    tags:
        - { name: kernel.event_listener, event: content.edit.publish, method: onPublishVersionEvent }

The connection works fine, it gets into the listener okay

class PublishListener
{
    private $locationService;
    private $contentService;
    private $pdfOnPublish;
    private $knpSnappyPdf;

    public function __construct(LocationService $locationService, ContentService $contentService, Array $pdfOnPublish = [], Pdf $knpSnappyPdf) {
        $this->locationService = $locationService;
        $this->contentService = $contentService;
        $this->pdfOnPublish = $pdfOnPublish;
        $this->knpSnappyPdf = $knpSnappyPdf;
    }

    public function onPublishVersionEvent(FormActionEvent $event)
    {
        $data = $event->getData();
        $contentInfo = $data->contentDraft->versionInfo->contentInfo;
        $locationId = $contentInfo->mainLocationId;
        $contentObjectId = $contentInfo->id;

        // How do I create the HTML for the template here?

This is the configuration for the content view full template, it’s working as I want it to

                syllabus:
                    controller: App\Controller\DefaultController::syllabusViewEnhancedAction
                    template: '@ezdesign/full/syllabus.html.twig'
                    match:
                        Identifier\ContentType: [syllabus]
                    params:
                        availableSupportServicesId: 70
                        collegePoliciesId: 52
                        gradingSchemeId: 85

I can generate a PDF using curl and wkhtmltopdf on the command line, but I would like to use the listener.

Solution

  • I extended the ViewController to check if a PDF existed for the location. If not, it created a PDF. Advantage - it only stores PDFs of content that people request, disadvantage - it checks every time
  • The publish listener deletes the PDF if it exists when new content is published. Advantage - PDF is current, disadvantage - PDF must be created at view time
1 Like

Nice! Sounds very useful.