Hi Briohny
I ended up hacking something together for another client if you are interested. I gave up on doing this using Sapphire directly and went with Javascript instead. Note that this is a severly limited hack, but we only need it for one contact form. Here's what I did:
1. Add a function to the form controller to get the var from the URL (or post, or cookie)
/userforms/code/UserDefinedForms - Inside the UserDefinedForm_Controller class
/**
* MODADC - Get and decode the selected product for the form autopopulation
*/
function getProductDetails() {
if(isset($_GET['product_item'])) {
return urldecode($_GET['product_item']);
} else {
return '';
}
}
2. Use the UserDefinedForm_Controller's init() method to get the var, and inject some JS code onto the page.
/userforms/code/UserDefinedForms - around line 163 - under the line "Requirements::javascript(THIRDPARTY_DIR . 'jsparty/behaviour.js');"
// Call the function that gets the GET var
$product_item = $this->getProductDetails();
// Only inject the javascript if we have the required var
if($product_item != '') {
Requirements::customScript(<<<JS
// This function adds functions to the onload event, so all the orignal javascript still works.
// No changes needed in this function at all
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
function setDefaults() {
// I used firebug to get the id of the field on the form I want to target
// Use firebug to do the same in your case
if(document.getElementById('Form_Form_EditableTextField3')) {
document.getElementById('Form_Form_EditableTextField3').value = '$product_item';
}
}
// Add the setDefaults function to the onload event, so it is only called after the page loads
addLoadEvent(setDefaults);
JS
);
}
IMORTANT: The above uses heredoc syntax, so that very last 'JS' must be on it's own line with nothing else, and have no indentation. Doesn't look the prettiest, but it allows us to type custom javascript without escaping. If you wanted to tidy it up, put your javascript in a seperate file and include it the same as the two requirements lines already there.
Hope that helps. Works for me and my client.
Cheers
Aaron