digitalist.global // February 07 2016

Handle non-activated services on site install

In our developer workflow we install the site continuously during development, as a health check of the code base. One of the problems we have in this workflow is when we declare a service that belongs to a module that is not activated yet during the install, like Memcache module.

In our developer workflow we install the site continuously during development, as a health check of the code base. One of the problems we have in this workflow is when we declare a service that belongs to a module that is not activated yet during the install, like Memcache module.
This is what a site needs normally in settings.php,

$settings['memcache']['servers'] = ['localhost:11211' => 'default']; 
$settings['memcache']['bins'] = ['default' => 'default']; 
$settings['memcache']['key_prefix'] = 'foo_bar'; 
$settings['cache']['default'] = 'cache.backend.memcache';

 

This works perfect to add on a runnings site when the module is activated and the service cache.backend.memcache is available.

But when drupal loads the file during the install – which happens during bootstrap, it does that before the module is activated, so we get an error: 'You have requested a non-existent [error] service "cache.backend.memcache" Error'.

A good solution for this should be to use the function drupal_installation_attempted that returns TRUE if we are installing the site, even during the bootstrap, so we do like this:

if (!drupal_installation_attempted()) {
  $settings['cache']['default'] = 'cache.backend.memcache';
} 

 

When site is installed and drupal loads settings.php, the memcache cache service is available.

This would off course also work for other services that you declare in settings.php.

Happy caching!

Photo by Matthias Buehler License: GPL 2.0

 

More from digitalist.global