Skip to content

Commit efa990c

Browse files
davidjamesstonealexluckett
authored andcommitted
Update README
1 parent c794206 commit efa990c

1 file changed

Lines changed: 82 additions & 11 deletions

File tree

README.md

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,74 @@ Optional dependencies
3636

3737
### Templates and views
3838

39-
Vision and nunjucks both need to be configured to search in the forms plugin module directory when looking for views.
40-
41-
The path is `node_modules/@defra/forms-engine/src/server/plugins/engine/views`.
39+
Vision and nunjucks both need to be configured to search in the `forms-engine-plugin` views directory when looking for template files.
4240

4341
For vision this is done through the `path` [plugin option](https://github.com/hapijs/vision/blob/master/API.md#options)
44-
For nunjucks it is configured through the environment [confgure options](https://mozilla.github.io/nunjucks/api.html#configure).
42+
For nunjucks it is configured through the environment [configure options](https://mozilla.github.io/nunjucks/api.html#configure).
43+
44+
The `forms-engine-plugin` path to add can be imported from:
45+
46+
`import { VIEW_PATH } from '@defra/forms-engine-plugin'`
47+
48+
Which can then be appended to the `node_modules` path `node_modules/@defra/forms-engine`.
49+
50+
The main template layout is `govuk-frontend`'s `template.njk` file, this also needs to be added to the `path`s that nunjucks can look in.
51+
52+
See example below for more detail.
53+
54+
### Form config
55+
56+
The `form-engine-plugin` uses JSON configuration files to serve form journeys.
57+
These files are called `Form definitions` and are built up of:
58+
59+
- `pages` - includes a `path`, `title`
60+
- `components` - one or more questions on a page
61+
- `conditions` - used to conditionally show and hide pages and
62+
- `lists` - data used to in selection fields like [Select](https://design-system.service.gov.uk/components/select/), [Checkboxes](https://design-system.service.gov.uk/components/checkboxes/) and [Radios](https://design-system.service.gov.uk/components/radios/)
63+
64+
The [types](https://github.com/DEFRA/forms-designer/blob/main/model/src/form/form-definition/types.ts), `joi` [schema](https://github.com/DEFRA/forms-designer/blob/main/model/src/form/form-definition/index.ts) and the [examples](test/form/definitions) folder are a good place to learn about the structure of these files.
65+
66+
TODO - Link to wiki for `Form metadata`
67+
TODO - Link to wiki for `Form definition`
68+
69+
#### Providing form config to the engine
70+
71+
The engine plugin registers several [routes](https://hapi.dev/tutorials/routing/?lang=en_US) on the hapi server.
72+
73+
They look like this:
74+
75+
```
76+
GET /{slug}/{path}
77+
POST /{slug}/{path}
78+
```
79+
80+
A unique `slug` is used to route the user to the correct form, and the `path` used to identify the correct page within the form to show.
81+
The [plugin registration options](#options) have a `services` setting to provide a `formsService` that is responsible for returning `form definition` data.
82+
4583

46-
The main template layout you use will likely be the govuk-frontend `template.njk` file, so this also needs to be added to the `path`s that nunjucks can look in.
84+
WARNING: This below is subject to change
85+
86+
A `formsService` has two methods, one for returning `formMetadata` and another to return `formDefinition`s.
87+
88+
```
89+
const formsService = {
90+
getFormMetadata: async function (slug) {
91+
// Returns the metadata for the slug
92+
},
93+
getFormDefinition: async function (id, state) {
94+
// Returns the form definition for the given id
95+
}
96+
}
97+
```
98+
99+
The reason for the two separate methods is caching.
100+
`formMetadata` is a lightweight record designed to give top level information about a form.
101+
This method is invoked for every page request.
102+
103+
Only when the `formMetadata` indicates that the definition has changed is a call to `getFormDefinition` is made.
104+
The response from this can be quite big as it contains the entire form definition.
105+
106+
See [example](#example) below for more detail
47107

48108
### Static assets and styles
49109

@@ -78,6 +138,8 @@ await server.register({
78138
}
79139
})
80140
141+
// Paths array to tell `vision` and `nunjucks` where template files are stored.
142+
// This can include `server/views`, or change it to where your local template are stored.
81143
const path = [
82144
`node_modules/@defra/forms-engine-plugin/${VIEW_PATH}`,
83145
'server/views'
@@ -96,13 +158,17 @@ await server.register({
96158
}
97159
},
98160
prepare: (options, next) => {
161+
// Nunjucks also needs an additional path configuration
162+
// to use the templates and macros from `govuk-frontend`
99163
const environment = nunjucks.configure(
100164
[
101165
...path,
102166
'node_modules/govuk-frontend/dist'
103167
]
104168
)
105169
170+
// Applies custom filters and globals for nunjucks
171+
// that are required by the `forms-engine-plugin`
106172
prepareNunjucksEnvironment(environment)
107173
108174
options.compileOptions.environment = environment
@@ -116,6 +182,7 @@ await server.register({
116182
}
117183
})
118184
185+
// Registers the `forms-engine-plugin`
119186
await server.register({
120187
plugin
121188
})
@@ -129,13 +196,17 @@ await server.start()
129196

130197
The forms plugin is configured with [registration options](https://hapi.dev/api/?v=21.4.0#plugins)
131198

132-
- `model` (optional) -
133-
- `services` (optional) -
134-
- `controllers` (optional) -
135-
- `cacheName` (optional) -
199+
- `services` (optional) - object containing `formsService`, `formSubmissionService` and `outputService`
200+
- `formsService` - used to load `formMetadata` and `formDefinition`
201+
- `formSubmissionService` - used prepare the form during submission (ignore - subject to change)
202+
- `outputService` - used to save the submission
203+
- `controllers` (optional) - Object map of custom page controllers used to override the default. See [custom controllers](#custom-controllers)
204+
- `cacheName` (optional) - The cache name to use. Defaults to hapi's [default server cache]. Recommended for production. See [here](#custom-cache) for more details
136205

137206
### Services
138207

208+
TODO
209+
139210
### Custom controllers
140211

141212
TODO
@@ -167,14 +238,14 @@ const server = new Hapi.Server({
167238

168239
## Exemplar
169240

170-
TODO: Link to exemplar
241+
TODO: Link to CDP exemplar
171242

172243
## Templates
173244

174245
The following elements support [LiquidJS templates](https://liquidjs.com/):
175246

176247
- Page **title**
177-
- Form component **titles**
248+
- Form component **title**
178249
- Support for fieldset legend text or label text
179250
- This includes when the title is used in **error messages**
180251
- Html (guidance) component **content**

0 commit comments

Comments
 (0)