-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add enum support for DCAs and models #6584
Conversation
Any particular reason we need the DcaExtractor to read the enums? Why not just use the listener to convert enum to string options? |
The DcaExtractor maps the individual fields to the configured enum classes and transfers this assignment to the model. To resolve the enums via |
805b54b
to
0cbb6d3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can confirm that the code works. 👍
@aschempp Has your question above been answered? @contao/developers Shall we add two example enums so people can use them as templates for their own enums? namespace Contao\CoreBundle\DataContainer\Enum;
enum PageRobotsEnum: string
{
case indexFollow = 'index,follow';
case indexNofollow = 'index,nofollow';
case noindexFollow = 'noindex,follow';
case noindexNofollow = 'noindex,nofollow';
} namespace Contao\CoreBundle\DataContainer\Enum;
use Contao\CoreBundle\Translation\TranslatableLabelInterface;
use Symfony\Component\Translation\TranslatableMessage;
enum PageRedirectEnum: string implements TranslatableLabelInterface
{
case permanent = 'permanent';
case temporary = 'temporary';
public function label(): TranslatableMessage
{
return new TranslatableMessage('tl_page.'.$this->name, [], 'contao_tl_page');
}
} Do you agree with the namespace? |
@SeverinGloeckle any reason you did not go for |
The two examples are a very good idea. However, I would prefer to add them in a follow-up PR for an independent discussion (namespace, extensibility, possibly resolution within the I may create a follow-up PR as soon as this PR has been merged. |
From my point of view, the explicit tagging of the enum provides the following benefits:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with that
Thank you @SeverinGloeckle. |
Description ----------- **Problem:** The `DcaExtractor` is parsing DCA data and provides information about any enumeration fields within a DCA (see #6584 for the original feature). However, if the extracted data is cached by the `ContaoCacheWarmer`, the enum configuration is missing. This results in an incorrect/incomplete configuration and it is not possible to resolve enumerations within a model if the extract is read from the cache. **Solution:** This PR adds the enumeration configuration to the cached data in `ContaoCacheWarmer`, making the information available in cached extracts. **Additional optimisation & Tests** Manually assembling the extract for the cache does not appear to be the most sustainable solution. However, this is outside the scope of this PR. I have not made any adjustments to the tests in `ContaoCacheWarmerTest`, as so far only the output of `$this->arrFields` is tested in the extract and any other information (e.g. `arrRelations`, `arrKeys`, ...) is not checked explicitly. If an addition to the test is desired, I can extend the test case and test data to include a field with an enum configuration. Commits ------- 407b777 Fix DCA extractor cache for enum fields
Implements #6546
A few notes on the implementation:
1. Usage of TranslatableMessage
The generation of translated references for the generated options is implemented via Symfony’s
TranslatableMessage
.In my opinion, this is the most sane way to handle translations in a complex system with various sources of translation.
Using Symfony’s
TranslatableInterface
however is less restrictive, but does not provide the additional information on parameters and especially on the translation domain.Although it is outside the scope of this PR, a forward-looking decision ought to be made on how Contao wants to deal with translations in the future. Up to now, neither of the two interfaces is being used.
2. Validation of enum values in the model
It’s not straightforward to define a proper handling of invalid enum values in the model. One could:
EnumUtil
).BackedEnum::tryFrom()
fail with the invalid value and let it throw its TypeErrorI implemented the second way in order to give developers a proper hint pointing to the DCA configuration but the other two ways are reasonable as well.
3. Modifications & unit tests for legacy code
For the changes to the legacy code, I stuck to existing styles and structures as much as possible.
For the model, only a simple unit test is added for resolving the enum - if necessary, I can also add tests for the various exceptions.
4. Needs documentation
If this will be merged, I'll also add corresponding notes to the developer documentation.