madeyourday / contao-rocksolid-custom-elements

RockSolid Custom Elements Contao Extension
http://rocksolidthemes.com/de/contao/plugins/custom-content-elements
MIT License
48 stars 12 forks source link

Option Callbacks via ServiceAnnotation possible? #121

Closed rabauss closed 4 years ago

rabauss commented 4 years ago

Are Option Callbacks for Fields of CustomElements via ServiceAnnotation possible? I guess that the Callback gets not registered for the dummy field.

It just worked for me with Definition in the config:

...
'background' => [
    'label' => ['Background'],
    'inputType' => 'select',
    'options_callback' => [\App\EventListener\DataContainer\ContentListener::class,  'getBackgroundOptions'],
],
...

I tested with the following Listener:

namespace App\EventListener\DataContainer;

use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\DataContainer;
use Terminal42\ServiceAnnotationBundle\ServiceAnnotationInterface;

class ContentListener implements ServiceAnnotationInterface
{
    /**
     * @Callback(table="tl_content", target="fields.rsce_field_cards__rsce_dummy__background.options")
     */
    public function getBackgroundOptions(DataContainer $dataContainer): array
    {
        $options = [];
        // do something....
        return $options;
    }
}
ausi commented 4 years ago

The dummy field is only used when you create a new list entry client side in the backend.

You would have to register the callback for rsce_field_cards__0__background, rsce_field_cards__1__background, rsce_field_cards__2__background and so on.

rabauss commented 4 years ago

... to infinity :-D Thanks for the advice - I will stay with the DCA definition!