Input Mask: Automatic Default Values

The input fields of input masks can be pre-filled with default values automatically. This can make filling in input masks easier when a new record is created.

The default value can be set via the BuildDataDefinitionEvent of the DCG — the following is an example event listener to pre-fill the name attribute of the model mm_employees with “Moin”.

 1<?php
 2// src/EventListener/SetDefaultValueListener.php
 3namespace App\EventListener;
 4
 5use ContaoCommunityAlliance\DcGeneral\DataDefinition\Palette\PaletteInterface;
 6use ContaoCommunityAlliance\DcGeneral\Factory\Event\BuildDataDefinitionEvent;
 7
 8class SetDefaultValueListener
 9{
10    public function __invoke(BuildDataDefinitionEvent $event): void
11    {
12        // Get container.
13        $container = $event->getContainer();
14        // Check right table present.
15        if ('mm_employees' !== $container->getName()) {
16            return;
17        }
18        // Set default value.
19        $container->getPropertiesDefinition()->getProperty('name')->setDefaultValue('Moin');
20    }
21}
1services:
2# src/Resources/config/services.yml
3  App\EventListener\SetDefaultValueListener:
4    public: true
5    tags:
6      - { name: kernel.event_listener, event: dc-general.factory.build-data-definition }

Defaults with Legacy Code

Note

Defaults using legacy code should no longer be used. From MM 2.3, for correct label output for the field, an additional entry with an empty string must be created — e.g.
$GLOBALS['TL_DCA']['<MM-Table-Name>']['fields']['<Field-Column-Name>']['label'] = '';
otherwise “LABEL NOT SET: <column>” will be shown instead of the label.

MetaModels input fields are (almost) identical to fields from Contao core or standard extensions created with a DCA array. Differences arise in part from the dynamic generation of fields in MetaModels via DC-General.

Defaults for fields can be achieved by extending the DCA array with the “default” key — see the Contao documentation.

To add a default, the internal name of the MetaModel and the column name of the attribute must be known. These can be added as an array entry using the general form:

1<?php
2// contao/dca/<MM-Table-Name>.php
3$GLOBALS['TL_DCA']['<MM-Table-Name>']['fields']['<Field-Column-Name>']['default'] = <Value>;

For the e-mail field ([text]) from The First MetaModel, the default could look like this:

1<?php
2// contao/dca/mm_employeelist.php
3$GLOBALS['TL_DCA']['mm_employeelist']['fields']['email']['default'] = '@mmtest.com';

For the individual attribute types there are specific expectations about the format of the values:

  • Text: text in quotes, e.g. @mmtest.com
    ...['default'] = '@mmtest.com';

  • Timestamp: integer for the timestamp, e.g. 1463657005 or PHP function time()
    ...['default'] = 1463657005; or
    ...['default'] = time();

  • Single select [Select]: integer of the value ID in quotes
    ...['default'] = '2';

  • Multi-select [Tags]: array with the alias values from the configured alias column
    ...['default'] = ['purchasing', 'marketing'];

  • Checkbox: true
    ...['default'] = true;

As can be seen with the “Timestamp” attribute, dynamic defaults are also possible. It would also be possible to access existing values from MetaModels and output them — optionally with a calculation — as the default. The API methods (MetaModels Interfaces) are available for accessing MetaModels.