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

Add an option to show a wildcard in the backend #110

Closed qzminski closed 5 years ago

qzminski commented 5 years ago

Simply by setting the correct flag:

// rsce_foobar_config.php

return [
    // ...
    'showBackendWildcard' => true,
    // ...
];

/cc @aschempp

ausi commented 5 years ago

Couldn’t this be achieved by setting 'beTemplate' => 'be_wildcard' and add a line that sets $this->wildcard in https://github.com/madeyourday/contao-rocksolid-custom-elements/blob/2ffd50ecb4d3f900f75552f2554a66b0a5c14ad5/src/Element/CustomElement.php#L95-L98

qzminski commented 5 years ago

Likely yes, but this way it's more simpler IMO ☺️ note that you also have to get the translated label, then Utf8::strtoupper it etc.

ausi commented 5 years ago

How about updating the code to this:

if (!empty($config['beTemplate'])) {
    if (!isset($this->arrData['wildcard'])) {
        $label = CustomElements::getLabelTranslated($config['label'])
        $this->arrData['wildcard'] = '### ' . Utf8::strtoupper(is_array($label) ? $label[0] : $label) . ' ###';
    }
    $this->strTemplate = $config['beTemplate'];
    return null;
}

This way you could also use a custom be_wildcard template and still use the wildcard label in your template.

qzminski commented 5 years ago

Looks good to me apart from the fact that parent::generate() is called unnecessary. @aschempp what do you think?

aschempp commented 5 years ago

I don't think it makes sense to generate the whole element in the back end?

ausi commented 5 years ago

But that’s how content elements work in contao?

I don’t like to add too many config flags, especially if they contradict each other. What if beTemplate and showBackendWildcard is set?

I think 'beTemplate' => 'be_wildcard' is a better way to solve this. And if we add the code from above it would result in the exact same output.

qzminski commented 5 years ago

But that’s how content elements work in contao?

If a frontend module or a content element displays a wildcard immediately without calling the parent method, e.g. https://github.com/contao/contao/blob/master/news-bundle/src/Resources/contao/modules/ModuleNewsList.php#L51

That's why I think it's better to do the same instead of calling parent::generate() which we don't need at all.

I think 'beTemplate' => 'be_wildcard' is a better way to solve this. And if we add the code from above it would result in the exact same output.

Then maybe we can automatically generate and return a wildcard if beTemplate === be_wildcard?

ausi commented 5 years ago

But parent::generate() does basically do the same, it creates a template, sets the data on it and calls parse() on it. That shouldn’t be too much overhead I think.

Additionally it has the benefit of having access to the whole data of the element inside the backend template.

Then maybe we can automatically generate and return a wildcard if beTemplate === be_wildcard?

That would make 'beTemplate' => 'be_wildcard_custom' behave differently then 'beTemplate' => 'be_wildcard' which is probably not a good idea.

qzminski commented 5 years ago

Okay you convinced me. Shall I update the code or you'll take care?

ausi commented 5 years ago

I’ll take care. Thanks!

qzminski commented 5 years ago

Thank YOU 🙂

ausi commented 5 years ago

I’ve added the changes to this PR, this should make 'beTemplate' => 'be_wildcard' work as expected.

ausi commented 5 years ago

Can you confirm that my implementation works for your use case?

qzminski commented 5 years ago

'beTemplate' => 'be_wildcard', does the job, thank you @ausi!

aschempp commented 5 years ago

❤️