1
|
|
2
|
Drupal module: Variable Realms
|
3
|
============================================
|
4
|
This is an API module that works as an arbitrator for multiple modules overriding global variables. It can
|
5
|
handle multiple realms defined by different modules. Examples: 'global', 'language', 'country',
|
6
|
|
7
|
Each realm has a weight and a current status. Realms with higher weights will override realms with lower weight.
|
8
|
|
9
|
There's a special 'global/default' realm that is the one storing default global variables. It has a weight of 0
|
10
|
so realms with weights higher than that (default weight for new realms is 10) will override these.
|
11
|
|
12
|
Any number of realms can be defined by different modules. If two modules use the same realm, the last one's variables
|
13
|
and weight will override the previous one. Every time we switch a realm, the $conf global array will be rebuilt.
|
14
|
|
15
|
At any moment the $conf global array of variables will be a combination of the active realms.
|
16
|
If we've got these two reamls defined:
|
17
|
- global/default, weight 0, which is defined by this module, will hold global default variables
|
18
|
- mymodule/key, weight 10, which may be defined by any contrib module on hook_boot() or hook_init()
|
19
|
The resulting variable set will be a combination of these two, with the second overriding the first one,
|
20
|
because of a higher weight. This is how we calculate the resulting variables when using variable_realm_switch()
|
21
|
|
22
|
$conf = $variables['global/default'] + $variables['mymodule/key']
|
23
|
|
24
|
API Example
|
25
|
-----------
|
26
|
This is an example of how realms work:
|
27
|
|
28
|
// We add a language realm with some variables and immediately switch to it
|
29
|
variable_realm_add('language', 'es', $spanish_variables);
|
30
|
variable_realm_switch('language', 'es');
|
31
|
|
32
|
// We add a country realm on top of it with some more variables but don't switch to it yet.
|
33
|
// Note the first time we add a domain we can set the weight for it.
|
34
|
|
35
|
variable_realm_add('country', 'spain', $spain_variables, 100);
|
36
|
|
37
|
// We add another country realm, but don't switch to it.
|
38
|
// The same weight from previous 'country' realm will be used
|
39
|
|
40
|
variable_realm_add('country', 'mexico', $mexico_variables);
|
41
|
|
42
|
// Now we can switch to the 'spanish/spain' set of variables
|
43
|
|
44
|
variable_realm_switch('country', 'spain');
|
45
|
|
46
|
// Or we can use the 'spanish/mexico' set
|
47
|
|
48
|
variable_realm_switch('country', 'mexico');
|
49
|
|
50
|
// Still we can add one more realm which will override some variables for the current node's content type
|
51
|
// These will override all the others because of its higher weight
|
52
|
variable_realm_add('nodetype', 'story', $story_variables, 200)
|
53
|
variable_realm_switch('nodetype', 'story')
|
54
|
|
55
|
An example of a module using this API is Internationalization's i18n_variable module.
|
56
|
|
57
|
Variable Realm Union.
|
58
|
====================================
|
59
|
|
60
|
This an API that allows combining two existing realms into a new one
|
61
|
whose keys are a combination of the other two.
|
62
|
|
63
|
An example of this module in action is the 'Domain+I18n Variables Integration' module
|
64
|
which is part of 'Domain Variable' module.
|
65
|
|
66
|
How to use it.
|
67
|
=============
|
68
|
To define a new domain that is a combination of two or more existing ones:
|
69
|
|
70
|
1. Implement hook_variable_realm_info() to define the realm name and properties.
|
71
|
|
72
|
function domain_i18n_variable_variable_realm_info() {
|
73
|
$realm['domain_language'] = array(
|
74
|
'title' => t('Domain+Language'),
|
75
|
// Display on settings forms but without form switcher.
|
76
|
'form settings' => TRUE,
|
77
|
'form switcher' => FALSE,
|
78
|
'variable name' => t('multilingual domain'),
|
79
|
);
|
80
|
return $realm;
|
81
|
}
|
82
|
|
83
|
2. Implement hook_variable_realm_controller() to define the Controller class to
|
84
|
be used and which other realms it is a combination of. Example:
|
85
|
|
86
|
function domain_i18n_variable_variable_realm_controller() {
|
87
|
$realm['domain_language'] = array(
|
88
|
'weight' => 200,
|
89
|
'class' => 'VariableStoreRealmController',
|
90
|
'union' => array('domain', 'language'),
|
91
|
);
|
92
|
return $realm;
|
93
|
}
|