Last Updated: February 25, 2016
·
2.18K
· timfernihough

Updating domain specific variables in Drupal when using domain module

Those who are somewhat familiar with it know that Drupal uses a database table called "variable" where it stores serialized arrays of values. When you use a:

variable_set('variable', $value);

or a

variable_get('variable', 'default value');

the values are retrieved from the variable table (not withstanding any overrides you might have defined in the $conf array inside the settings.php file). For overriding variables in settings.php, see another post I've written here.

Most modules that have an administrative page utilize the variables table to store the values of any configuration fields. If you are also using the Domain Access module in your work, you'll know that this introduces an additional set of radio buttons at the bottom of any settings page allowing you to pick which domain you want to update the variable for.

One of our clients also uses the Login Toboggan module to allow the login form to show up on pages that the anonymous user is not allowed to see. This is helpful and gives them a way to login if they are indeed authenticated but have just had their session expire.

Recently, the client presented good business logic behind not showing this form anymore. Because we are also using domain access, I can't do a simple variable_set() and expect this to be enough to address the request.

So, I wrote an update hook that iterates through the available domains that have been configured (and does nothing for the primary domain as it doesn't have a value in the domain_conf table). It is agnostic to the point that you could add this as an update hook to any relevant module to solve the same problem if you have it yourself.

//  Disable the login form on 403/access denied page for primary domain.
variable_set('site_403', "1");

//  Iterate through all available domains and disable the login form on 403/access denied pages.
$domains = domain_domains();
foreach($domains as $domain) {
    //  There is no entry for the primary domain in this table.
    if($domain['domain_id'] != 10) {
        domain_conf_variable_set($domain['domain_id'], 'site_403', '1');
    }
}

Disclaimer: Domain ID 10 represents the primary domain but only in version 3 and higher of Domain Access. It was domain ID 1 in previous versions.