1
|
|
2
|
Drupal module: Variable API
|
3
|
===========================
|
4
|
|
5
|
Variable module will provide a registry for meta-data about Drupal variables.
|
6
|
|
7
|
Module Developers: Please declare your variables.
|
8
|
|
9
|
Why?
|
10
|
====
|
11
|
- So other modules can know about your module's variables and they can be translated, exported, etc.
|
12
|
- You'll get automatic variable edit forms, tokens, access control and uninstall for free.
|
13
|
|
14
|
How?
|
15
|
====
|
16
|
Easy: Implement hook_variable_info();
|
17
|
|
18
|
/**
|
19
|
* Implements hook_variable_info().
|
20
|
*/
|
21
|
function mymodule_variable_info($options) {
|
22
|
|
23
|
$variable['mymodule_number'] = array(
|
24
|
'title' => t('Magic number', array(), $options),
|
25
|
'description' => t('Magic number, array(), $options),
|
26
|
'type' => 'number',
|
27
|
'access' => 'administer menus',
|
28
|
);
|
29
|
|
30
|
$variable['mymodule_name'] = array(
|
31
|
'title' => t('Name', array(), $options),
|
32
|
'description' => t('Enter your name, please.', array(), $options),
|
33
|
'type' => 'string',
|
34
|
'default' => t('Drupal user', array(), $options),
|
35
|
);
|
36
|
|
37
|
$variable['mymodule_mail'] = array(
|
38
|
'title' => t('Mail'),
|
39
|
'type' => 'mail_text',
|
40
|
// This type will spawn into two real variables: mymodule_mail_subject, mymodule_mail_body
|
41
|
// Everything, included the form elements, will be handled automatically
|
42
|
);
|
43
|
|
44
|
return $variable;
|
45
|
}
|
46
|
|
47
|
Note: You can have your variables declared in a separate file that just will be loaded when needed.
|
48
|
|
49
|
yourmodule.variable.inc
|
50
|
|
51
|
FAQ
|
52
|
===
|
53
|
|
54
|
- Will I need to add a dependency on the variable.module?
|
55
|
|
56
|
Not neccessarily. Just if you want to enjoy some of the module's features advanced features like:
|
57
|
- Getting variable values or defaults in different languages. Use variable_get_value().
|
58
|
- Let other modules alter my variable defaults. Implement hook_variable_info_alter().
|
59
|
- Let other modules know when variables are changed. Use variable_set_value(). Implement hook_variable_update().
|
60
|
- Getting automatic forms for all the module's variables, a group of variables, etc..
|
61
|
- Having variables with multiple values handled automatically like mail body and subject or variables for node types.
|
62
|
|
63
|
Otherwise you can just provide the meta-data for other modules to use. You still get:
|
64
|
- Tokens for your variables like [variable:myvariable_name]
|
65
|
- Variables deleted automatically when the module is uninstalled
|
66
|
- Localizable texts for your variables when using the Internationalization module.
|
67
|
|
68
|
- How do I get a form with all of my module's variables?
|
69
|
|
70
|
drupal_get_form('variable_module_form', 'mymodule');
|
71
|
|
72
|
- Once I have declared a default for my variable, how can I benefit from it?
|
73
|
|
74
|
variable_get_value('variable_name');
|
75
|
|
76
|
- What if I don't want to provide any administration form for my variables?
|
77
|
|
78
|
That's ok, people will still be able to see and edit them by enabling the 'Variable Admin' module included.
|
79
|
|
80
|
|
81
|
|