Problem/Motivation
Context definitions need a way to provide default values. Example:
/**
* context = {
* "text" = @ContextDefinition("string", label = @Translation("Text"), default_value = "replace me!")
* }
*/
Proposed resolution
Add getDefaultValue() and setDefaultValue() methods to ContextDefinitionInterface. This is an expected change to the interface and has also been documented in code pointing to #2346999: Make ContextDefinitionInterface more expressible.
Remaining tasks
Review the patch.
User interface changes
None.
API changes
ContextDefinitionInterface changes, but all parties are aware of that and this is documented.
Original report by @Xano
\Drupal\Core\TypedData\TypedData::applyDefaultValue() now sets NULL as its own default value, while we can easily let definitions define the default value. I *think* this used to work, but I'm not sure.
I'm adding this as sub-issue to #2346999: Make ContextDefinitionInterface more expressible.
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | interdiff.txt | 3.58 KB | klausi |
| #32 | 2204509.patch | 12.46 KB | klausi |
Comments
Comment #1
xanoComment #3
fagoWe should use getDefaultValue() and associated setter methods as we do for field definitions for other definition objects as well. We want them on ContextDefinitions also though.
Comment #4
fagoComment #5
klausiShould we add getDefaultValue() to DataDefinitionInterface at this point? Or should we stick to getSetting('default_value') as in the patch to make API changes? Should we only support default values for primitive data types such as strings? Or should we also support entities with default values (example: "5" as default value for a node would load node 5)? Where do we put the test cases for this? Should we add to TypedDataTest or try PHPUnit for this?
Comment #6
klausiklausi opened a new pull request for this issue.
Comment #7
klausiSo I followed the approach to use the settings place and added a test case for that. So no interface change for DataDefinitionInterface. I added a test case for that.
Then I also started with ContextDefinitionInterface and implemented the same settings approach there. No test cases for that yet.
Comment #8
fago>Should we add getDefaultValue() to DataDefinitionInterface at this point?
Yes, as said. We need to complete the API here - I don't think the API change is an issue as the base-class will not change. It does not make any sense to cripple the class in order to achieve BC for not existing devs working with this right now.
When adding the default value to the interfaces, it will be there for all data types. However, it has to be compatible with our usage in fields what is problematic because of it's entity parameter :/
Thinking about it, the typed data definition default value is a nice addition, which is not required for d8rules but only aids easing creating field types or other data structures with properties having some default values. Given that I'd suggesting changing the issue scope of this to ContextDefinitions only which should be a simple straight-forward get/set of default values.
I don't think adding settings to context definition makes sense - there are no custom classes which could make use of custom settings. So everything supported, should be in the interface.
Comment #9
klausiOk, started from scratch adding the getDefaultValue() method to DataDefinitionInterface and ContextDefinitionInterface.
TODO: test case for context definitions.
Comment #11
klausiOh, so we have a serious incompatibility with FieldDefinitionInterface now which has a required parameter on the getDefaultValue() method. Not sure how we should solve that.
Comment #12
fagothat I talked about in my last comment: ;)
Thus, as suggested removed typed data definition from the scope. I think we should probably look into doing #2268049: Decouple field definitions from typed data definitions for being able to solve this and #2329937: Allow definition objects to provide options - where we ran into the same issue.
Comment #13
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #14
klausiOk, removed the TypedData changes, let's only do ContextDefinition then.
Comment #15
fagoThanks, patch looks good. However, moreover we should make the default values work, not? :-)
I.e., the context class should return the default value instead of NULL if it's unset.
Comment #16
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #17
klausiRight, implemented that for both context classes and also added test cases.
Comment #21
fagoThanks, here some small remarks:
minor: we usually use !isset() for checking NULL
or NULL to remove it?
To speed up further usages, should we keep a reference on our typed data object and set it in the context?
Any reasons for replicating this test method in core when it is already in component?
Comment #22
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #23
klausiFixed the comment and the isset() check.
3: I don't think we should introduce more complexity here and the optimization of caching the typed data object is necessary.
4: The test case is the same, but the core Context class overrides the method, so we should also make sure that the overridden method works as expected to avoid regressions.
Comment #24
fagoThanks.
ad: 4. k, makes sense
ad 3:
Actually, I figured there is already $this->contextData - so the returned value should be written to the property? I do not see a point in re-creating it every time it's called and the variable to keep it is already there, so we should use it to avoid having an inconsistency inside the class.
Comment #25
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #26
fagoThanks, that looks good now. Also adding tag as this is needed by rules.
Comment #28
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #29
klausiJust a reroll, let's see if this still works.
Comment #30
fagoThanks. Took another look, patch is still good.
Comment #31
alexpott\Drupal\Core\Annotation\ContextDefinition- specifically@defgroup plugin_context Annotation for context definitionHow about structuring this so there aren't two returns? Eg.
$definiton is misspelt.
PHPUnit_Framework_MockObject_MockObject is not properly namespaced
Comment #32
klausiklausi pushed some commits to the pull request.
For an interdiff please see the list of recent commits.
Comment #33
klausiFixed all comments by alexpott, interdiff attached.
Comment #34
fagoGood remarks. They have been all addressed - thus back to RTBC.
Comment #35
MixologicThe testbots are having troubles, Im working on one that was running this last patch, so if it comes back failed, I would try retesting it. (I had to reboot mysql on the bot)
Sorry for the inconvenience.
Comment #40
klausiWas only a testbot fluke, back to RTBC.
Comment #41
alexpottThis issue is a normal bug fix, and its disruption is limited and it unblocks contrib, so it is allowed per https://www.drupal.org/core/beta-changes.Committed c5e5ad4 and pushed to 8.0.x. Thanks!