Problem: We maintain separate dev, test and production instances of our sites:

samplesite.dev - a localhost development site
samplesite.test.ourcompany.com - an internet-facing test site
samplesite.com - the client's production site

Because each has a separate domain name, the Google map API key is different for each instance. Since the API key is saved in variables table of the database by default, in the past we would manually update the API key at http://whicheverURL/admin/settings/gmap after pushing the database from dev to test to production.

There are a number of ways to handle this. You could write a script that runs as part of your deployment process that updates the API key in the variables table, or use the Keys module.

Another solution that we've found was to override the API key value in our settings.php file.

At the bottom of the settings.php file, you will find the following section to override variable settings:

/**
 * 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.
 *
 * Remove the leading hash signs to enable.
 */
$conf = array(
#   'site_name' => 'My Drupal site',
#   'theme_default' => 'minnelli',
#   'anonymous' => 'Visitor',
);

In the conf array above, you can add the line:

'googlemap_api_key' => the_appropriate_googlemap_api_key_for your domain

Knowing this, there are two ways to use this override to account for dev, test, and production instances of your site. First, you could add separate directories under "sites" that contain separate settings.php files for each domain - with each settings.php file including the appropriate API key override.

However, IMHO, this creates a lot of overhead. A more elegant solution is to add a snippet of php code to your settings.php file that checks the domain at which the site is being visited, and overrides the API with the appropriate, domain-specific key. Because of the way that our company handles version control and deployment, use the following code to handle this process:

1) We add the following include statement at the bottom of our settings.php file (we have a separate setting.php file for dev, test and production - which only contain the db credentials for each particular hosting instance):

/**
 * domain-specific variable overrides
 */
include 'settings.extend.php';

#NOTE: settings.php file does not end with closing php brackets.

2) We create the following file: sites/default/settings.extend.php:

<?php
/**
 *------------------------------------------------------------------------------
 * System        : SampleSite
 * Author        : Grant Kruger, OpenSourcery.com
 * Date Written  : October 2007
 * Description   : This file serves as an extension to the Drupal settings.php
 *                 file.  To include these settings simply add this statement
 *                 to settings.php: 
 *                 include 'settings.extend.php';
 *------------------------------------------------------------------------------
 */

// Set URLs
$main_site_name = 'SampleSite';
$dev_url        = 'samplesite.dev';
$test_url       = 'samplesite.test.ourcompany.com';

// Control whether to link to API Keys for the development site, test site or
// live site.

// Set API Keys.
$dev_googlemap_api_key  = 'XXXXXXXXXXXXXXXXXX';
$test_googlemap_api_key = 'XXXXXXXXXXXXXXXXXX';
$live_googlemap_api_key = 'XXXXXXXXXXXXXXXXXX';

// Set API keys based on the environment.

$conf = array(); // Define Drupal array that replaces entries on table "variables".

if ($_SERVER['HTTP_HOST'] == $dev_url) { // If this is a development URL
    $conf['site_name']         = 'Development-' . $main_site_name;
    $conf['googlemap_api_key'] = $dev_googlemap_api_key;
} elseif ($_SERVER['HTTP_HOST'] == $test_url) { // If this is a test site URL
    $conf['site_name']         = 'Test-' . $main_site_name;
    $conf['googlemap_api_key'] = $test_googlemap_api_key;
} else { // Production URL
    $conf['googlemap_api_key'] = $live_googlemap_api_key;
}

?>

The cool thing about this approach is that you can include other domain-specific URL overrides in this settings.extend.php file - such as other third-party API keys. For example, you'll notice in the code above, we also change the $site_name variable to include the prefixes "dev-" and "test-" for additional clarity when visiting the three different instances of the site.

Comments

nightlife2008’s picture

I advise you to have a look at: http://drupal.org/project/keys

It's a module dedicated to managing multiple API keys based on config or domainname... AND Gmap supports it ;-)

Greets,
Kim

Anonymous’s picture

I forgot to restart apache at first.
Once I did that, it worked great for me.

Thanks!!

GaryWong’s picture

Hi, FYI the variable to set in settings.php is no longer "googlemap_api_key", but instead "gmap_api_key".
HTH