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

Support RSCE definition in fragment controller #162

Open aschempp opened 1 year ago

aschempp commented 1 year ago

Here's an idea for maybe a version 3: instead of having a definition file, I'd love to define the fields in a method of my custom controller. Something like this:

class FoobarController extends AbstractContentElementController implements CustomElementInterface
{
    public function getResponse(ContentModel $model): Response
    {
        $config = StringUtil::deserialize($model->rsce_data);
    }

    public function getDefinnition(): array
    {
        return [
            'fields' => [
                'image' => [
                    'label' => ['Bild', 'Wählen Sie das Bild der Person.'],
                    'inputType' => 'fileTree',
                    'eval' => ['filesOnly' => true, 'mandatory' => true],
                ],
                'name' => [
                    'label' => ['Name', 'Geben Sie den Namen der Person ein.'],
                    'inputType' => 'text',
                    'eval' => ['mandatory' => true, 'tl_class' => 'w50'],
                ],
                'position' => [
                    'label' => ['Position/Abteilung/Untertitel', 'Geben Sie eine Position, Abteilung oder Untertitel für die Person ein.'],
                    'inputType' => 'text',
                    'eval' => ['mandatory' => true, 'tl_class' => 'w50'],
                ],
            ],
        ];
    }
}

we could also have a custom abstract controller or a trait that would deserialize the data and/or pass it to the template. The advantage is that I could use any template name, including Twig namespaces and overrides, and I would still have a real controller class if I need to do more stuff with the fields config 😎

ausi commented 8 months ago

we could also have a custom abstract controller

That would make sense I think.

Additionally I’d like to get rid of the _config.php files completely when switching to fragment controllers. Maybe by using something similar to single file components:

{% extends "@Contao/content_element/_base.html.twig" %}

{% block content %}
    {{ text }}
{%  endblock %}

{% config %}
    return [
        'label' => ['Custom Element', ''],
        'types' => ['content'],
        'contentCategory' => 'texts',
        'standardFields' => ['cssID', 'headline'],
        'fields' => [
            'text' => [
                'inputType' => 'standardField',
            ],
        ],
    ];
{% endconfig %}
aschempp commented 8 months ago

Also reminds me a bit on how Twig components work, maybe we can copy some stuff from there 🤔