feat: check if template fields should be extracted

The `BeforeGetTemplatesEvent` now has a property that allows us
to check if the templates should be enhanced with fields or not.
If so, then we extract the fields, if not, simply do not.

Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
This commit is contained in:
Elizabeth Danzberger 2025-05-12 16:06:16 -04:00
parent 1f483fcd35
commit 770287fa60
No known key found for this signature in database
GPG key ID: D64CE07FD0188C79
2 changed files with 32 additions and 13 deletions

View file

@ -192,6 +192,23 @@ describe('User templates', function() {
{ index: 'ContentControls.ByIndex.4', type: 'checkbox', alias: '', checked: false },
]
// Intercept the initial templates request to ensure it does not get the fields yet
cy.intercept(
'GET',
'**/apps/files/api/v1/templates',
(req) => req.continue(),
).as('templatesRequest')
// Intercept the POST request to verify the correct fields are submitted
cy.intercept('POST', '**/templates/create', (req) => {
const templateFields = Object.values(req.body.templateFields)
expect(templateFields[0].content).to.equal(fields[0].content)
expect(templateFields[1].content).to.equal(fields[1].content)
req.continue()
}).as('reqFillFields')
cy.visit('/apps/files')
// Create a new document
@ -205,21 +222,19 @@ describe('User templates', function() {
cy.get('input[data-cy-files-new-node-dialog-input=""]').type('FileFromTemplateWithFields')
cy.get('button[data-cy-files-new-node-dialog-submit=""]').click()
// Ensure the template fields array is of length 0, meaning no fields were fetched
cy.wait('@templatesRequest').then(({ response }) => {
const templates = response.body.ocs.data
const template = templates[1].templates[0]
expect(template.fields.length).to.equal(0)
})
// Choose the document template
cy.get('form.templates-picker__form').as('templatePicker')
cy.get('@templatePicker').contains('document').click()
cy.get('@templatePicker').find('input[type="submit"]').click()
// Intercept the POST request to verify the correct fields are submitted
cy.intercept('POST', '**/templates/create', (req) => {
const templateFields = Object.values(req.body.templateFields)
expect(templateFields[0].content).to.equal(fields[0].content)
expect(templateFields[1].content).to.equal(fields[1].content)
req.continue()
}).as('reqFillFields')
cy.submitTemplateFields(fields)
// Wait for the response and collect the file ID of the created file

View file

@ -24,10 +24,14 @@ class BeforeGetTemplatesListener implements IEventListener {
return;
}
if (method_exists($event, 'shouldGetFields') && !$event->shouldGetFields()) {
return;
}
foreach ($event->getTemplates() as $template) {
$templateFileId = $template->jsonSerialize()['fileid'];
$fields = $this->templateFieldService->extractFields($templateFileId);
$templateId = $template->jsonSerialize()['fileid'];
$fields = $this->templateFieldService->extractFields($templateId);
$template->setFields($fields);
}
}