Problem/Motivation
Recipes can accept input, but there is only one way to for a user to provide it: at the command line, either interactively or via drupal recipe FOO --input=FOO.var_name=value.
We need some way to collect recipe inputs in the UI, both to allow Project Browser to apply recipes that require input, and to enable this use case in other situations (for example, during Drupal CMS's installer, if there's a recipe that needs an API key or similar).
Proposed resolution
Create a trait for forms that want to collect input on behalf of recipes. This will have two methods:
getRecipeInputForm(): This will go through a recipe's inputs (and those of its dependencies), and for any that define aformsetting, it will expose that setting as an element in a tree.collectRecipeInputValues(): This is meant to run on form submission, and will go through a recipe's inputs (and those of its dependencies), setting whatever input values are found in the form state.
This also means that recipe inputs will be able to define how they should be viewable in a form. For example:
input:
recipient:
data_type: email
description: 'The email address that should receive submissions from the feedback form.'
constraints:
NotBlank: []
form:
'#type': textfield
prompt:
method: ask
arguments:
question: 'What email address should receive website feedback?'
default:
source: config
config: ['system.site', 'mail']
Everything in the form array will be used to construct the form element - this syntax is similar to what you see in Webform. The element cannot contain sub-elements (this will be validated by the recipe system). If an input doesn't have a form array, it won't be visible in a form at all.
Issue fork drupal-3483435
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:
Comments
Comment #2
phenaproximaComment #4
phenaproximaComment #5
phenaproximaI discussed this with @alexpott in Zoom today.
Long story short: this issue can proceed as it currently exists, but there needs to be a longer-term plan to unify the messy solar system that consists of Typed Data, config schema, the form system, and CLI input. Drupal doesn't have a single generalized "data input, validation, and translation system" that we could leverage, really. The recipe system uses Typed Data in a reasonably pure fashion, but as the current patch demonstrates, that necessitates encoding form API elements into recipes if we want them to be presented in a particular way in a particular context. And that is sad. It would be so much better if I could just say "this input is a string with these validation constraints", and trust that Drupal would be able to present that input appropriately in the CLI, in a form, etc.
But we're a long way off from that.
So we decided to be pragmatic as ever, and implement the proposed approach. Hopefully we can deprecate it as core's ability to handle input in a unified way becomes more robust. But for now, this is the clearest way for us to deliver this essential feature.
Comment #6
alexpottWe also need to deal with recipe translation. We should see if there is an issue and if not open one. It's going to be complex.
Comment #7
phenaproximaFiled #3488972: [Meta] Make recipes translatable to deal with recipe translation.
Comment #8
tim.plunkettGiven that #3489023: [PP-1] Give recipes a way to group and organize their inputs, when they are presented in a form exists, I think this is a pretty nice way of handling this. Great work!
Comment #9
phenaproximaMade an attempt at a change record: https://www.drupal.org/node/3489030
Comment #10
phenaproximaWhoops. Nope, we're still RTBC.
Comment #11
thejimbirch commented+1 for RTBC
Best we've got on the Forms API elements
https://www.drupal.org/docs/drupal-apis/form-api/form-render-elements#s-...
Comment #12
alexpottCommitted and pushed 8b67272ac33 to 11.x and cc885723370 to 11.1.x and 0e4ae7c20fe to 10.5.x and 45843c37585 to 10.4.x. Thanks!
Comment #19
alexpottWe need to change some of the constraints for the forms definition in the recipe for this to work on 10.x - reverted from there.
Comment #20
thejimbirch commented