Problem/Motivation
Elements endpoint webform_rest/{webform_id}/elements?_format=json takes forever to load on a very complex webforms.
Proposed resolution
Instead of returning renderable array maybe return simpler JSON ?
The following code load much faster and provides everything needed to build the form on front end.
<?php public function get($webform_id) {
if (empty($webform_id)) {
throw new HttpException(t("Webform ID wasn't provided"));
}
// Load the webform.
$webform = Webform::load($webform_id);
// Basic check to see if something's returned.
if ($webform) {
// Return only the form elements.
return new ModifiedResourceResponse($webform->getElementsInitialized());
}
throw new HttpException(t("Can't load webform."));
}
Also it is probably a good idea to cache response and use Cache tags to invalidate cache and provide the most recent data.
Comments
Comment #2
minnur commentedComment #3
imclean commentedSounds good. I did look at
getElementsInitialized()but can't remember why I discounted it. Is the output the same as the render array?Caching would also be useful.
Comment #4
minnur commentedHere is what I created for the project that I am working on https://github.com/minnur/webform_restapi
Comment #5
imclean commentedThanks. I don't have time to test much in the next few days and was wondering if I changed the code as you suggested would the output also be changed?
Does adding cache metadata work OK with
ResourceResponsewhen an email handler is set for the webform? Is that part of the webform object?Comment #6
imclean commentedI do support the idea of caching and using Webform's built in methods for obtaining form elements, such as
getElementsInitialized(). This would require an additional endpoint as it's changing the output.Comment #8
imclean commentedThanks. I've added
getElementsInitialized()as a new endpoint/webform_rest/{webform_id}fields.Caching will require more testing I think.
Comment #9
imclean commented