Reputation: 55
I added custom field into config.xml. I want to save value of my custom field into other table, not the #__extensions table. How can I do it for extension options page.
Upvotes: 1
Views: 1309
Reputation: 146
The "short" answer is that you'd have to create your own custom JFormField class to handle this. I then have some Javascript making an AJAX call when the value changes.
Here's my example (edited from a similar exercise I did a while back):
Step 1: Create a JFormField class somewhere (I'm putting mine in /libraries/test/mycontrol.php for now):
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.html.html');
jimport('joomla.form.formfield');//import the necessary class definition for formfield
class JFormFieldMyControl extends JFormField
{
protected function getInput()
{
$document = JFactory::getDocument();
$document->addScript(JURI::base() . '../libraries/test/config_test.js');
# This is a hidden control, because Joomla needs to save a value against any field in a config's xml
$control = '<input type="hidden" id="' . $this->id . '" name="' . $this->name . '">';
# Now let's create a textbox which will be the thing that actually saves to the DB via Javascript
$control .= '<input type="text" id="your_test_input" name="your_test_input" value="' . $this->value . '" />';
return $control;
}
}
Step 2: Create the JS file referenced in the above code snippet (so, in /libraries/test/config_test.js). This will trigger an AJAX call to another PHP file, savefieldvalue.php, when your textbox's value changes.
window.addEvent('domready', function() {
// Filter the validator dropdown
var jcategory = document.id('your_test_input');
jcategory.addEvent('change',function(event) {
saveFieldValue(this.get('value'));
});
});
function saveFieldValue(value)
{
var req = new Request({
url:'libraries/test/savefieldvalue.php',
method:'post',
autoCancel:true,
data:'save_value=' + value,
onRequest: function() {
},
onSuccess: function(s) {
alert('saved ok!');
}
}).send();
}
Step 3: The savefieldvalue.php file, which handles the AJAX request and saves the thing.
<?php
# Bootstrap
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__) . '/../..'); # note, you may need to fiddle with these by adding or removing "../"'s until you get the depth right
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
$app->initialise();
# ======================
$db = JFactory::getDbo();
$value = $_POST['save_value'];
# and then put your normal Joomla save code here
Step 4: Now introduce your new JFormField to your config XML, like so:
<fieldset name="test" label="Test Control" addfieldpath="/libraries/test/">
<field name="test-field" id="test-field" type="MyControl" label="My Custom Control" />
</fieldset>
Note how you use addfieldpath to indicate to Joomla where your custom JFormField lies, and then set the type of your field name to your control.
It's a bit convoluted, and you can adjust it to do all this on postback rather than on change, but I've found it works well for me. I'm using 2.5, so the bootstrap bit in savefieldvalue.php may need to reflect your normal /index.php.
Good luck!
Upvotes: 2