Problem/Motivation
There are currently 3 sources for default values when it comes to input collection for recipes: value, config, and env. My original idea on how their usage could be improved to make recipes more generically applicable was a chain of fallbacks like this:
input:
name:
default:
- source: env
env: NAME
- source: config
config: ['some.settings', 'name']
- source: value
value: My name is something
This could be used in automatic scripts where env variables would be available, but it could also be used interactively, and to re-apply a recipe, without having to maintain a recipe in different flavours for those different use cases. It would use the first source that provides a value in the order env/config/value.
But in a Slack thread I learned that this is too complex but that a fallback value for at least the config source would make sense, e.g. like this:
input_name:
default:
source: config
config: ['foo.settings', 'baz']
fallback: bar
So, if a config object foo.settings doesn't exist (yet), instead of failing in InputConfigurator.php line 191, it could fall back to the given value.
Introduced terminology
The term "fallback" is introduced to describe a default value for something.
Issue fork drupal-3537627
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3537627-if-a-recipe
changes, plain diff MR !12818
Comments
Comment #2
thejimbirch commentedComment #3
phenaproximaRe-titling for clarity.
Comment #4
phenaproximaWe could implement this by replacing the unconditional
\RuntimeExceptionwith this:Nice one-line fix and easy to test. I'd be okay with making this allowance for the
configvalue source specifically.Comment #6
thejimbirch commentedI created a MR with the change. Added Needs tests tag is someone wants to update/add a test for this.
Comment #7
jurgenhaasReady for a review, the nightwatch failure doesn't seem to be related.
Comment #8
phenaproximaThis looks good to me but I don't think we need to bring the console-specific input collection stuff into it.
Comment #9
phenaproximaComment #10
thejimbirch commentedUpdated issue summary.
Comment #11
thejimbirch commentedWe're going to park this for @alexpott to review.
Comment #12
alexpottI think we also should support the fallback value for env too. The differences between the env and the config implementations are interesting. Env with return an empty string when the env var does not exist but config will throw an exception. I wonder what the right the thing to do here is...
Comment #13
alexpottFWIW the concept of a fallback value seems go to me.
Comment #14
phenaproximaI like the idea of making it consistent.
Comment #15
thejimbirch commentedLooks great, Love the additional test and the comment updates.
I added a draft change record and am moving to RTBC.
Comment #16
thejimbirch commentedComment #17
alexpottI think we should allow NULL as a fallback value. See review on MR.
Comment #18
phenaproximaComment #19
thejimbirch commentedSuggestions added, all threads resolved.
I added the null possibility to the change record, and am moving this back to RTBC.
Comment #20
alexpottCommitted c961ea3 and pushed to 11.x. Thanks!
Comment #22
damienmckennaOut of interest why was the attribute name "fallback" used rather than e.g. "default_value" or just "default", a naming convention used in other subsystems?
Comment #23
damienmckennaI opened #3541371: Rename "fallback" recipe attribute to "default_value" to fix the naming convention.