# 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:

```yaml
# 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:

```php
// 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.

<span style="font-size: medium;"><span style="color: #000000;"><span style="font-family: Arial, sans-serif;"><span lang="de-DE">Example</span></span></span></span>:

```php
// 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:

```php
// 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
    }
}
```