Input Mask: Adding More Values to Single Select

By default, the value column for the single select attribute is restricted to selecting one attribute from any Contao table. If you want to display one or more attributes/values from the referenced table in the input mask’s single select attribute, this can be done in several ways:

1. “Combined values” attribute

Create an additional attribute in the referenced model that combines the values for display.

2. Event “GetPropertyOptionsEvent” (recommended)

 1<?php
 2// src/EventListener/GetPropertyOptionsListener.php
 3
 4namespace App\EventListener;
 5
 6use Contao\MemberModel;
 7use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetPropertyOptionsEvent;
 8use MetaModels\AttributeSelectBundle\Attribute\AbstractSelect;
 9use MetaModels\DcGeneral\Data\Model;
10use Terminal42\ServiceAnnotationBundle\Annotation\ServiceTag;
11
12/**
13 * @ServiceTag("kernel.event_listener", event="dc-general.view.contao2backend.get-property-options", priority="100")
14 */
15class GetPropertyOptionsListener
16{
17    public function __invoke(GetPropertyOptionsEvent $event)
18    {
19        // Check if options set.
20        if ($event->getOptions() !== null) {
21            return;
22        }
23
24        // Check if right model table and type.
25        if ('mm_my_model' !== $event->getEnvironment()->getDataDefinition()->getName()) {
26            return;
27        }
28
29        $model = $event->getModel();
30        if (!($model instanceof Model)) {
31            return;
32        }
33
34        // Check if right attribute and type.
35        if ('member' !== $event->getPropertyName()) {
36            return;
37        }
38
39        $attribute = $model->getItem()->getAttribute($event->getPropertyName());
40        if (!($attribute instanceof AbstractSelect)) {
41            return;
42        }
43
44        // Generate own options list.
45        $members     = MemberModel::findAll(['order' => 'lastname ASC']); // add e.g. filter for not disabled...
46        $aliasColumn = $attribute->get('select_alias');
47
48        $options = [];
49
50        foreach ($members as $member) {
51            $options[$member->{$aliasColumn}] =
52                \sprintf('%s, %s [%s]', $member->lastname, $member->firstname, $member->email);
53        }
54
55        $event->setOptions($options);
56    }
57}

Result:
img_manipulate-select-values_01

Reference:
GetPropertyOptionsListener

3. DCA callback “options_callback”

 1<?php
 2// contao/dca/<MM-Table-Name>.php
 3$GLOBALS['TL_DCA']['<MM-Table-Name>']['fields']['<MM-Column-Name-Select>'] = [
 4 'options_callback' => function () {
 5     $modelName = '<MM-Table-Name-Select>';
 6     $factory   = $this->getContainer()->get('metamodels.factory');
 7     $model     = $factory->getMetaModel($modelName);
 8     $filter    = $model->getEmptyFilter();
 9     $items     = $model->findByFilter($filter);
10     $arrItems  = $items->parseAll('text');
11
12     $options = [];
13     foreach ($arrItems as $arrItem) {
14         $options[$arrItem['text']['<MM-Select-Column-Name-Alias>']] = \sprintf(
15         '%s [%s]',
16         $arrItem['text']['<MM-Select-Column-Name-1>'],
17         $arrItem['text']['<MM-Select-Column-Name-2>']
18         );
19     }
20
21     return $options;
22    },
23];

The keys of the $options array must match the “Alias” setting from the attribute configuration.

Filters configured in the “Select” attribute for the backend are bypassed with this approach.