Skip to main content

9. For programmers and developers

This section is aimed at programmers who want to influence this extension in their own modules.

Internal field variables for PDF

New in version 2.3 are internal field variables that can be entered via the pdf_formsBeforePdf hook. These variables can then be used in exactly the same way as form fields from the submitted form. To be able to use these variables in the conditions, an entry must be made in config/config.yaml so that these fields are added to the conditions SELECT:

# config/config.yaml

softleister_pdfforms:
    add_condition_options: [foo, bar]

In the example, the field variables foo and bar are made available for conditions. After entering them in the config.yaml file, the Symfony cache must be cleared/refreshed.

The variables and their values are then created in the pdf_formsBeforePdf hook (see below).

Hooks

In the program flow, you can link in at various points using a HOOK registration. The extension contao-pdfforms-bundle calls the registered hooks, if any are registered.

The following hooks are available:

pdf_formsBeforePdf

Is called after data preparation before the PDF is created. Here, further entries can be added to the $arrPDF array or existing entries can be modified. The hook must return the $arrPDF as the return value.

Example:

// src/EventListener/Pdf_formsBeforePdfListener.php
<?php

namespace App\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;

#[AsHook('pdf_formsBeforePdf')]
class Pdf_formsBeforePdfListener
{
    public function __invoke(array $arrPDF)
    {
        // any code

        // Example: Adding internal field variables
        //          The variables can be used in the PDF in the same way
        //          as form data. To use the field in conditions, an entry
        //          in config/config.yaml is necessary (see above).

        $arrPDF['arrFields']['foo'] = ['type'=>'text', 'value'=>1, 'orig'=>'foo', 'options'=>''];
        $arrPDF['arrFields']['bar'] = ['type'=>'text', 'value'=>'', 'orig'=>'bar', 'options'=>''];


        // Example: The text output of the upload field with the field name
        //          ‘singleupload’ should enter the file name and 1 last
        //          directory before it in the PDF. The default is only the
        //          file name without the directory.
        //
        //          Option: basename:Number of directories before

        if( isset( $arrPDF['arrFields']['singleupload'] ) ) {
          $arrPDF['arrFields']['singleupload']['options'] = 'basename:1';
        }

        return $arrPDF;
    }
}

pdf_formsPositions

Is called up for each item in the form during PDF creation. It is still possible to mani­pulate data here. The page number cannot be changed, otherwise the output will not work correctly. The passed and possibly modified array $arrItem is expected as the return value.

Example:

// src/EventListener/Pdf_formsPositionsListener.php
<?php

namespace App\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;

#[AsHook('pdf_formsPositions')]
class Pdf_formsPositionsListener
{
    public function __invoke(array $arrItem, array $arrPDF)
    {
        // any code
 
        return $arrItem;
    }
}

pdf_formsAfterPdf

Is called up after creation, saving and transfer to form processing. Further final activities can be inserted here using a hook. The hook has no return value.

Example:

// src/EventListener/Pdf_formsAfterPdfListener.php
<?php

namespace App\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;

#[AsHook('pdf_formsAfterPdf')]
class Pdf_formsAfterPdfListener
{
    public function __invoke(string $pdfdatei, array $arrPDF): void
    {
        // any code
    }
}