Module Development: Settings Variables in settings.php Using $conf

Published May 21, 2009

Sometimes you need to set some global variables in Drupal that aren't used by just a single module, but rather a set of modules. You can take advantage of the $conf array in your site's settings.php file to set these variables that your various modules can then access them using the "variable_get()" method.

For example, I have a client that pulls in some data from an external SQL Server database to their Drupal site. Data is pulled in by a variety of custom modules and they wanted to have a single, secure place to set the database connection information.

If you check out your site's settings.php file, you'll see that it has a "Variable overrides" section:

/**
* Variable overrides:
*
* To override specific entries in the 'variable' table for this site,
* set them here. You usually don't need to use this feature. This is
* useful in a configuration file for a vhost or directory, rather than
* the default settings.php. Any configuration setting from the 'variable'
* table can be given a new value. Note that any values you provide in
* these variable overrides will not be modifiable from the Drupal
* administration interface.
*
* Remove the leading hash signs to enable.
*/

In my case, I went ahead and set the SQL Server database connection information here as follows:

$conf = array(
'sqlserver_dsn' => 'client_dsn',
'sqlserver_username' => 'admin',
'sqlserver_password' => 'super_secret_so_you_better_not_tell_anybody',
);

Then, in the various custom modules, I can easily access the values using:

$dsn = variable_get('sqlserver_dsn', '');

The advantage to my client is that all the DB login information is in a single, secure place.

One warning: the devel module's Variable Editor indicates that it will display all variables including those from the $conf array. This is not the case - it is still a "TO DO" item in the latest -dev version (2009-Apr-23).

You can set lots more in the $conf array. See this related DrupalEasy Quicktip.

Comments

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name
CAPTCHA