Information for: DEVELOPERS   PARTNERS

Altering values in settings.php with hooks

When using Acquia Cloud 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.

Important

  • Files executed by settings.php hooks must have a .php extension.
  • Because every web request will run 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 Acquia Cloud 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 Acquia Cloud 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.com/hc/en-us/articles/360004542293
*/

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:

Enabling memcache

To enable memcache on your Acquia Cloud Site Factory websites, complete the following steps:

  • 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';

Acquia Cloud Site Factory customers using Drupal 8 and Acquia BLT must also apply the patch from issue #2766509 on Drupal.org, and use this example from the Acquia BLT issue queue to update memcache to a recommended version.

Importing a configuration directory

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

<?php
/**
*  Set vcs and sync dirs to writable directories above docroot.
*/
$config_directories['vcs'] = '../config/default';
$config_directories['sync'] = '../config/default';

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/

Customize Fast 404 extensions

By default, Drupal 8 core does not include .swf files in Fast 404 settings. If your websites use .swf files, adding the .swf extension into the list of extensions handled by Fast 404 ensures any missing .swf files will not cause performance problems. To add that extension to Fast 404 configuration, use code similar to the following example:

<?php
/**
* Taken from https://docs.acquia.com/site-factory/extend/hooks/settings-php
* Fast 404 paths: see comments in sites/g/settings.php for other settings.
* Drupal 8 Core does not include .swf files, so we add those here.
*/
$config['system.performance']['fast_404']['paths'] = '/\.(?:txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp|swf)$/i';

Drush compatibility with Sumologic

If you forward your website’s logs to Sumologic, this script alters the output from Drush requests in Drupal 7 or Drupal 8 to match Sumologic’s expected logging format:

<?php
/**
*
* Add logging information for drush requests in drupal-requests.log
* on Acquia. By default, these requests show up with no REQUEST_METHOD
* or URI, which can make splitting them up in Sumologic very hard.
* Taken from
* https://gist.github.com/seanhamlin/47d49c59ca50c91c0aec3db5b4ae082d
*/

if (isset($_ENV['AH_SITE_ENVIRONMENT']) && PHP_SAPI === 'cli' && function_exists('drush_main')) {
  // We are in a drush bootstrap, modify $_SERVER variables needed for
  // drupal-request.log.
  $_SERVER['REQUEST_METHOD'] = 'CLI';
  $drush_args = drush_get_arguments();
  $_SERVER['REQUEST_URI'] = 'drush ' . array_shift($drush_args);
}

Drupal 7 cache lifetime per website

To conditionally set the cache lifetime of each website in your Acquia Cloud 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 Acquia Cloud 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:

<?php
if (extension_loaded('newrelic')) {
   $domain_fragments = explode('.', $_SERVER['HTTP_HOST']);
   $site_name = array_shift($domain_fragments);
   newrelic_set_appname("$site_name;CURRENT_APP_NAME", '', '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.