0

I am having different environments development and production for a website and each of its database varies.

I have created a module which will connect to an external database. But now I want to make it dynamic like if the environment is dev then automatic the fields must be entered according to dev and if its prod then details must be according to prod.

Following is my code:

mod_test.xml

<fieldset name="basic">
    <field name="db_hostname" type="text" default="" label="Host Name" description="Enter the Database Host Name" />
    <field name="db_port" type="text" default="" label="Port" description="Enter the Database Port Number" />
    <field name="db_name" type="text" default="" label="Database Name" description="Enter the Database Name" />
    <field name="db_user" type="text" default="" label="Database Username" description="Enter the Database Username" />
    <field name="db_password" type="password" default="" label="Database Password" description="Enter the Database Password" />
</fieldset>

Currently, each time I have to change the db details in the administrator according the environments. Therefore I want to make it dynamic and to pre-fill the data according to different environments. Kindly, let me know how to proceed on this.

Thanks for any help

1 Answer 1

1

I have got the solution to my problem :) custom_fields

Following is my code:

<fieldset name="basic" addfieldpath="/modules/mod_test/models/fields">
    <field name="db_hostname" type="Dbhost" default="" label="Host Name" description="Enter the Database Host Name" />
</fieldset>

in models/fields/dbhost.php

<?php 
defined('_JEXEC') or die('Restricted access');
//define environment
define('ENV', getenv('ENV'));
jimport('joomla.form.formfield');
class JFormFieldDbhost extends JFormField {
protected $type = 'Dbhost';
public function getInput() {
 if(defined('ENV')){
  switch(ENV){
   case 'dev':
   $host= 'devhost';
   break;
   case 'prod':
   $host = 'prodhost';
   break;
   default:
   $host = 'localhost';
   break;
   }
 }
  return '<input type="text" id="'.$this->id.'" name="'.$this->name.'"    value="'.$host.'">';             
}
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.