Callbacks

Data definition containers are driven by callbacks which extends the configuration, provides options and handles actions. There are some useful tools which are provided by Toolkit.

Developing own callbacks

If you want to create a callback class for your data container tl_example Toolkit provides an abstract listener containing the Data container manager.

<?php

 // ExampleCallbacks.php
 class ExampleCallbacks extends Netzmacht\Contao\Toolkit\Dca\Listener\AbstractListener
 {
     /**
      * Name of the data container.
      *
      * @var string
      */
     protected static $name = 'tl_example';

     public function onSubmit()
     {
     }
 }

The base Callbacks class provides even more helpers:

getDefinition()
Gets the definition of the current data container (tl_example) See Definition section for more details.
getDefinition(‘tl_custom’)
Gets the definition of a specific data container. See Definition section for more details.
getFormatter()
Gets the formatter of the current data container (tl_example). See Formatter section for more details.
getFormatter(‘tl_custom’)
Gets the formatter of a specific data container. See Formatter section for more details.

Provided callbacks

Alias generator callback

The alias generator uses the Alias generator to create an alias callback. By default a predefined alias generator is used. You may use the configurations to the toolkit.alias_generator configuration. The fields configuration is required.

<?php

 $GLOBALS['TL_DCA']['tl_example']['fields']['alias']['save_callback'][] = [
     Netzmacht\Contao\Toolkit\Dca\Listener\Save\GenerateAliasListener::class,
     'onSaveCallback'
 ];

 $GLOBALS['TL_DCA']['tl_example']['fields']['alias']['toolkit']['alias_generator'] = [
     'factory' => 'netzmacht.contao_toolkit.data.alias_generator.factory.default_factory',
     'fields' => ['title']
 ];

For more details please have a look at the GenerateAliasListener.

State button callback

The state button callback is used to generate the state toggle button to toggle the active state of an entry. The stateColumn configuration is required.

<?php

 $GLOBALS['TL_DCA']['tl_example']['list']['operations']['toggle']['button_callback'][] = [
     Netzmacht\Contao\Toolkit\Dca\Listener\Button\SaveButtonCallbackListener::class,
     'onButtonCallback'
 ];

 $GLOBALS['TL_DCA']['tl_example']['list']['operations']['toggle']['toolkit']['state_button'] = [
     'disabledIcon' => 'custom-invisible-icon.png,
     'stateColumn'  => 'published',
     'inverse'      => false
 ];

For more details please have a look at the StateButtonCallbackListener.

Color picker wizard

The color picker wizard provides a wizard to choose a rgb color. Every configuration is optional.

<?php

 $GLOBALS['TL_DCA']['tl_example']['fields']['color']['wizard'][] = [
     Netzmacht\Contao\Toolkit\Dca\Listener\Wizard\ColorPickerListener::class,
     'onWizardCallback'
 ];

 $GLOBALS['TL_DCA']['tl_example']['fields']['color']['toolkit']['alias_generator'] = [
     'title'      => null,
     'template'   => null,
     'icon'       => null,
     'replaceHex' => null,
 ];

For more details please have a look at the ColorPickerListener wizard.

File picker wizard

The file picker wizard provides a popup wizard to choose a file.

<?php

 $GLOBALS['TL_DCA']['tl_example']['fields']['file']['wizard'][] = [
     Netzmacht\Contao\Toolkit\Dca\Listener\Wizard\FilePickerListener::class,
     'onWizardCallback'
 ];

For more details please have a look at the FilePickerListener wizard.

Page picker wizard

The page picker wizard provides a popup wizard to choose a page.

<?php

 $GLOBALS['TL_DCA']['tl_example']['fields']['page']['wizard'][] = [
     Netzmacht\Contao\Toolkit\Dca\Listener\Wizard\PagePickerListener::class,
     'onWizardCallback'
 ];

For more details please have a look at the PagePickerListener wizard.

Get templates callback

The get templates callback get all available templates.

<?php

 $GLOBALS['TL_DCA']['tl_example']['fields']['template']['options_callback'] = [
     Netzmacht\Contao\Toolkit\Dca\Listener\Options\TemplateOptionsListener::class,
     'onWizardCallback'
 ];

 $GLOBALS['TL_DCA']['tl_example']['fields']['template']['toolkit']['template_options'] = [
     'prefix' => '',
     'exclude' => null,
 ];

For more details please have a look at the TemplateOptionsListener wizard.

Invoker

If you want to trigger a callback form your code you don’t have to worry about the different supported callback formats. For this case toolkit provides an invoker which is registered as a service.

<?php

 /** @var Netzmacht\Contao\Toolkit\Dca\Callback\Invoker $invoker */
 $invoker = $container->get('netzmacht.contao_toolkit.callback_invoker);

 // Invoke the callback and get the return values.
 $options = $invoker->invoke($GLOBALS['TL_DCA']['tl_example']['fields']['template']['options_callback'], [$dc]);

 // Invoke a list of callbacks and define which value should changed after invoking a callback.
 // The last argument indicates that the first argument of the arguments array ($value) should be changed
 $value = $invoker->invokeAll($GLOBALS['TL_DCA']['tl_example']['fields']['save_callback'], [$value, $dc], 0);