Is it possible to prefill Input/hidden fields on the standard magnolia form (page with Form component created in Magnolia Author UI) from url parameters or it could/should be done only in FreeMarker template?
Asked
Active
Viewed 111 times
2 Answers
2
We did this in magnolia 5.7 by overriding the hiddenField like this:
<!-- set the value to the queryparam (ctx[..]) or the form field value if not empty -->
[#assign value = content.value!ctx[content.controlName]!model.value!""]
<div ${model.style!} >
<input type="hidden" name="${content.controlName}" id="${content.controlName}" value="${value}"/>
</div><!-- end ${model.style!} -->
<!-- remove the queryParam and value from URL without redirect/reload so that it isn't sent twice with the form -->
<script>
var newUrl = RemoveParameterFromUrl(window.location.href, "${content.controlName}")
window.history.pushState("", "", newUrl);
// credits to Jared for this regex https://stackoverflow.com/a/25214672/15591150
function RemoveParameterFromUrl(url, parameter) {
return url
.replace(new RegExp('[?&]' + parameter + '=[^&#]*(#.*)?$'), '$1')
.replace(new RegExp('([?&])' + parameter + '=[^&]*&'), '$1');
}
</script>

btk
- 71
- 5
0
There isn't an out-of-the-box feature. As you told you could customize the components in order to retrieve the value inside Freemarker template (e.g. ${ctx.keyParamName} ).

G. Scimeca
- 36
- 2