Site Factory

Altering values in settings.php with hooks

When using Site Factory, you cannot directly modify your website’s settings.php file, but you can make changes to values in settings.php using hooks at the beginning and end of the settings.php file. These hooks enable you to run custom code during your website’s bootstrap process.

Note

  • Files executed by settings.php hooks must have a .php extension.
  • Due to every web request running the commands in settings.php hooks, commands included in settings.php hooks must be lightweight (for example, setting variables) and must not read or write to databases or files. Accessing databases or files using the hook file can greatly impair your website’s performance. If you must access databases or files, you must use modules instead.

Using the settings.php hooks

Like several of the other hooks in use by Site Factory, you must create a script file and then place it in a particular directory. The scripts run in alphabetical order at the appropriate time when doing database updates for your websites.

For detailed instructions, see Hooks in Site Factory.

Pre-settings.php example script

Modify the following script to suit your needs in pre-settings-php hooks:

Conditionally increasing memory limits

Certain pages on your websites may require increased memory limits to function properly. To increase the memory limit on your websites, use the following script:

<?php
/**
* Sets a higher memory limit for admin/structure/menu which frequently
* needs an unusually high amount of memory to load, due to complexity.
* For additional examples of changing memory limits for pages on
* your websites, see
* https://support-acquia.force.com/s/article/360004542293-Conditionally-increasing-memory-limits
*/

if (strpos($_GET['q'], 'admin/structure/menu/') === 0) {
   ini_set('memory_limit', '700M');
}

For additional examples, see Examples of conditional memory limit changes.

Post-settings.php example scripts

Modify the following scripts to suit your needs in post-settings-php hooks:

Setting temp directories for file uploads

When Drupal temporarily uploads a file to a path that is inaccessible by the web server that needs to access it, an unexpected behavior might occur.

To prevent such unexpected behavior, add the following code script to your post-settings.php hooks:

$settings['file_temp_path'] = "/mnt/gfs/{$_ENV['AH_SITE_GROUP']}.{$_ENV['AH_SITE_ENVIRONMENT']}/tmp";

This sets the temporary directory path for your Site Factory sites in each environment to be used as a distributed file system folder.

Enabling memcache

To enable memcache on your Site Factory websites:

  • Contact your Technical Account Manager or Account Manager to discuss your subscription’s hardware needs.
  • Install the Memcache API and Integration module to your codebase.
  • Modify the following script to provide the necessary configuration details:
$conf['cache_backends'][] = '<path to the memcache.inc file>';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

Importing a configuration directory

To import configuration values into an Site Factory website, edit the following script to point to the directory (or directories) where your configuration values are stored:

Important

In the current Drupal version, the sync directory path and the vcs directory path are defined as values in the $settings array instead of the $config_directories array:

$settings['config_sync_directory'] = 'your/config/sync/directory/path'
$settings['config_vcs_directory'] = 'your/config/vcs/directory/path'

If the config directory does not already exist, you must create it in the same folder containing your docroot directory, as in this example:

/docroot/
config/default/

Drupal 7 cache lifetime per website

To conditionally set the cache lifetime of each website in your Site Factory subscription to individual values, and disallowing any value of fewer than 5 minutes, download this example acsf-cache-lifetime.php script.

New Relic monitoring

To enable New Relic monitoring in a multisite environment, like Site Factory, create a post-settings.php script named new_relic.php containing the following code, replacing current_app_name with the name of your application:

if (extension_loaded('newrelic')) {
$env = 'local';

if (isset($_ENV['AH_SITE_ENVIRONMENT'])) {
 $env = $_ENV['AH_SITE_ENVIRONMENT'];
}

global $_acsf_site_name;
newrelic_set_appname("<current_app_name>.$env.$_acsf_site_name; <current_app_name>.$env", '', 'true');
}

Storing credentials outside of version control

If you must store sensitive credentials outside of your codebase, you can create a secrets.settings.php file and then make its contents available to Drupal with a post-settings.php hook (as described in Storing sensitive information outside of your codebase). By doing so, sensitive credentials are not kept in your database backups or your version control repository, allowing you to distribute database backups or grant repository access to team members who must not possess sensitive credentials.

Avoiding database deadlocks

You can minimize database deadlocks by changing the transaction isolation (tx_isolation) variable from the MySQL default of REPEATABLE-READ to READ-COMMITTED by creating both a pre-settings.php hook script and a post-settings.php hook script.

First, create a pre-settings.php hook script containing the following line:

$conf['acquia_hosting_settings_autoconnect'] = FALSE;

Next, download this example acsf-hook-tx-isolation.php script to create a post-settings.php hook script.

For more information, see Fixing database deadlocks in the Acquia Support Knowledge Base.