Formbuilder - DynamicChoice - getPreferredChoices (dynamic)

I have a form “registerForm” so that users are abel to register for an event. Because Events changing all the time, this list is dynamicly.

There are also pages with links to this registerForm and which send an GET-param with eventId.

If param “eventId” is empty - the user should see the item “please select …”, but if “eventId” is an event, user should see selected event of dynamicChoise/regevent.

I have set the “getPreferredChoices” method, but it seems that it does not work or formBuilder does not use the “getPreferredChoices” method.

What I have done:

The Listing of Events works perfect.
It works also perfect if I set dynamicChoiceField/DefaultOption to the name of an Event.
But because EventList is dynamic, I want set it dynamicly.

So I set php-service “EventChoices.php” like this:

public function setFormBuilder(FormBuilderInterface $builder)
{
    $this->builder = $builder;

    $builder->addModelTransformer(new CallbackTransformer(
        function ($entries) {
            return $entries;
        },
        function ($entries) {
            if (empty($entries)) {
                return $entries;
            }

            $eventEntities = $this->getEventListArray();
            if (is_array($entries)) {
                $data = [];
                foreach ($entries as $id) {
                    $data[] = $eventEntities[$id];
                }
                return implode(', ', $data);
            } else {
                return $eventEntities[$entries];
            }
        }
    ));
}

public function getPreferredChoices($element, $key, $index)
{
    if( $this->param('registerEvent') ) {
        return urldecode($this->param('registerEvent'));
    } else {
        return 'Please select ...';
    }
}

public function getList()
{
    $data = [];
    foreach ($this->getEventListArray() as $entityId => $entityName) {
        $data[$entityName] = $entityId;
    }
    return $data;
}
private function getEventListArray() {
    return arrax('Alfa' => 1,'Beta' => 2);
}

However, it appears that the Form Builder does not use this getPreferredChoices method.

Andreas

OK, I am blind. Didn’t see that the simple example has another class than advanced class.
Now it works perfect.

Andreas