Skip to content

Commit 96de843

Browse files
committed
allow plugin consumer to provide viewContext props
1 parent 86ab510 commit 96de843

10 files changed

Lines changed: 23 additions & 20 deletions

File tree

src/server/plugins/engine/configureEnginePlugin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ export const configureEnginePlugin = async ({
2727

2828
return {
2929
plugin,
30-
options: { model, services, controllers, baseLayoutPath: 'layout.html' }
30+
options: {
31+
model,
32+
services,
33+
controllers,
34+
viewContext: {
35+
baseLayoutPath: 'layout.html'
36+
}
37+
}
3138
}
3239
}
3340

src/server/plugins/engine/plugin.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export interface PluginOptions {
7373
viewPaths?: string[]
7474
filters?: Record<string, FilterFunction>
7575
pluginPath?: string
76-
baseLayoutPath: string
76+
viewContext: {
77+
baseLayoutPath: string
78+
}
7779
}
7880

7981
export const plugin = {
@@ -89,7 +91,7 @@ export const plugin = {
8991
viewPaths,
9092
filters,
9193
pluginPath = PLUGIN_PATH,
92-
baseLayoutPath
94+
viewContext
9395
} = options
9496
const { formsService } = services
9597
const cacheService = new CacheService(server, cacheName)
@@ -150,7 +152,7 @@ export const plugin = {
150152
}
151153
})
152154

153-
server.expose('baseLayoutPath', baseLayoutPath)
155+
server.expose('viewContext', viewContext)
154156
server.expose('cacheService', cacheService)
155157

156158
server.app.model = model

src/server/plugins/engine/views/confirmation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends pluginOptions.baseLayoutPath %}
1+
{% extends baseLayoutPath %}
22

33
{% from "govuk/components/panel/macro.njk" import govukPanel %}
44

src/server/plugins/engine/views/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends pluginOptions.baseLayoutPath %}
1+
{% extends baseLayoutPath %}
22

33
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}
44
{% from "partials/components.html" import componentList with context %}

src/server/plugins/engine/views/item-delete.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends pluginOptions.baseLayoutPath %}
1+
{% extends baseLayoutPath %}
22

33
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}
44
{% from "govuk/components/button/macro.njk" import govukButton %}

src/server/plugins/engine/views/repeat-list-summary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends pluginOptions.baseLayoutPath %}
1+
{% extends baseLayoutPath %}
22

33
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}
44
{% from "govuk/components/button/macro.njk" import govukButton %}

src/server/plugins/engine/views/summary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends pluginOptions.baseLayoutPath %}
1+
{% extends baseLayoutPath %}
22

33
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}
44
{% from "govuk/components/summary-list/macro.njk" import govukSummaryList %}

src/server/plugins/nunjucks/context.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function context(request) {
4646

4747
/** @type {ViewContext} */
4848
const ctx = {
49+
...request?.server.plugins['forms-engine-plugin'].viewContext, // take consumers props first so we can override it
4950
appVersion: pkg.version,
5051
assetPath: '/assets',
5152
config: {
@@ -57,10 +58,6 @@ export function context(request) {
5758
serviceName: config.get('serviceName'),
5859
serviceVersion: config.get('serviceVersion')
5960
},
60-
pluginOptions: {
61-
baseLayoutPath:
62-
request?.server.plugins['forms-engine-plugin'].baseLayoutPath
63-
},
6461
crumb: safeGenerateCrumb(request),
6562
cspNonce: request?.plugins.blankie?.nonces?.script,
6663
currentPath: request ? `${request.path}${request.url.search}` : undefined,

src/server/plugins/nunjucks/types.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
* @property {object} [context] - Nunjucks render context
1010
*/
1111

12-
/**
13-
* @typedef {object} PluginOptions
14-
* @property {string} [baseLayoutPath] - Page layout to extend
15-
*/
16-
1712
/**
1813
* @typedef {object} ViewContext - Nunjucks view context
1914
* @property {string} appVersion - Application version
@@ -27,7 +22,7 @@
2722
* @property {string} [slug] - Form slug
2823
* @property {(asset?: string) => string} getAssetPath - Asset path resolver
2924
* @property {FormContext} [context] - the current form context
30-
* @property {PluginOptions} [pluginOptions] - the current form context
25+
* @property {PluginOptions['viewContext']} [injectedViewContext] - the current form context
3126
*/
3227

3328
/**
@@ -43,4 +38,5 @@
4338
* @import { CookieConsent } from '~/src/common/types.js'
4439
* @import { config } from '~/src/config/index.js'
4540
* @import { FormContext } from '~/src/server/plugins/engine/types.js'
41+
* @import { PluginOptions } from '~/src/server/plugins/engine/plugin.js'
4642
*/

src/typings/hapi/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { type ServerYar, type Yar } from '@hapi/yar'
55
import { type Logger } from 'pino'
66

77
import { type FormModel } from '~/src/server/plugins/engine/models/index.js'
8+
import { type ViewContext } from '~/src/server/plugins/nunjucks/types.js'
89
import {
910
type FormRequest,
1011
type FormRequestPayload
@@ -20,7 +21,7 @@ declare module '@hapi/hapi' {
2021
}
2122
'forms-engine-plugin': {
2223
cacheService: CacheService
23-
baseLayoutPath: string
24+
viewContext: ViewContext
2425
}
2526
}
2627

0 commit comments

Comments
 (0)