Projet

Général

Profil

Paste
Télécharger (9,87 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / variable / variable.api.php @ 6ae446a4

1
<?php
2

    
3
/**
4
 * @file
5
 * Hooks provided by the Variable module.
6
 */
7

    
8
/**
9
 * @defgroup variable_api_hooks Variable API Hooks
10
 * @{
11
 * Functions to define and modify information about variables.
12
 *
13
 * These hooks and all the related callbacks may be defined in a separate file
14
 * named module.variable.inc
15
 *
16
 * @}
17
 */
18

    
19
/**
20
 * Define variables used by a module.
21
 *
22
 * Provides meta-information for each variable that includes at the very least some human readable title.
23
 * This information may be used by other modules to select variables from a list for translating, exporting, etc.
24
 *
25
 * Though not required we can also provide some more information to be able to handle the variable in an effective
26
 * way, like which type of data and form element it uses, default value, etc.. There are multiple predefined
27
 * variable types ('type' attribute) that will add a predefined set of properties. Some of them are:
28
 *
29
 * - "string": Simple plain string variable. The form element will be a text field and it will be localizable.
30
 * - "number": Simple numeric value. The form element will be a text field.
31
 * - "boolean": Simple TRUE/FALSE value. It will be a checkbox.
32
 * - "enable": Enabled / Disabled selector. It will display as two radio buttons.
33
 * - "select": Selectable list of options. Depending on the number of options, the element will be a list of
34
 *   radios or a drop down.
35
 * - "options": List of options with multiple choices. It will be a list of checkboxes.
36
 * ...
37
 *
38
 * More variable types can be defined by modules using hook_variable_type_info().
39
 *
40
 * For the case of variable names that depend on some other parameter (like variables per content-type),
41
 * there's some special type of variables: Multiple variables. These can be defined like this:
42
 *
43
 * @code
44
 *   $variables['node_options_[node_type]'] = array(
45
 *     'type' => 'multiple',
46
 *     'title' => t('Default options', array(), $options),
47
 *     'repeat' => array(
48
 *       'type' => 'options',
49
 *       'default' => array('status', 'promote'),
50
 *       'options callback' => 'node_variable_option_list',
51
 *     ),
52
 *   );
53
 * @endcode
54
 *
55
 * This multiple variable will spawn into one variable for each node type. Note the variable name that includes
56
 * the property [node_type]. Values for [node_type] will be defined on hook_variable_type_info().
57
 *
58
 * The 'repeat' property defines the properties of children variables. In this case the 'type' property is optional
59
 * and will default to 'multiple'.
60
 *
61
 * @param $options
62
 *   Array of options to build variable properties. Since variable properties are cached per language
63
 *   these options should be used at the very least for string translations, so titles and defaults are
64
 *   localized. Possible options:
65
 *   - "language" => Language object for which strings and defaults must be returned. This one will be always defined.
66
 *
67
 * @return
68
 *   An array of information defining the module's variables. The array
69
 *   contains a sub-array for each node variable, with the variable name
70
 *   as the key. Possible attributes:
71
 *   - "title": The human readable name of the variable, will be used in auto generated forms.
72
 *   - "type": Variable type, should be one of the defined on hook_variable_type_info().
73
 *   - "group": Group key, should be one of the defined on hook_variable_group_info().
74
 *   - "description": Variable description, will be used in auto generated forms.
75
 *   - "options": Array of selectable options, or option name as defined on hook_variable_option_info().
76
 *   - "options callback": Function to invoke to get the list of options.
77
 *   - "default": Default value.
78
 *   - "default callback": Function to invoke to get the default value.
79
 *   - "multiple": Array of multiple children variables to be created from this one.
80
 *   - "multiple callback": Function to invoke to get children variables.
81
 *   - "element": Form element properties to override the default ones for this variable type.
82
 *   - "element callback": Function to invoke to get a form element for this variable.
83
 *   - "module": Module to which this variable belongs. This property will be added automatically.
84
 *   - "repeat": Array of variable properties for children variables.
85
 *   - "localize": Boolean value, TRUE for variables that should be localized. This may be used by other modules.
86
 *   - "validate callback": Callback to validate the variable value, it will be added to form element #validate.
87
 *
88
 * @see hook_variable_info_alter()
89
 */
90
function hook_variable_info($options) {
91
  $variables['site_name'] = array(
92
    'type' => 'string',
93
    'title' => t('Name', array(), $options),
94
    'default' => 'Drupal',
95
    'description' => t('The name of this website.', array(), $options),
96
    'required' => TRUE,
97
  );
98
  $variables['site_403'] = array(
99
    'type' => 'drupal_path',
100
    'title' => t('Default 403 (access denied) page', array(), $options),
101
    'default' => '',
102
    'description' => t('This page is displayed when the requested document is denied to the current user. Leave blank to display a generic "access denied" page.', array(), $options),
103
  );
104
  return $variables;
105
}
106

    
107
/**
108
 * Alter the variable definitions.
109
 *
110
 * @param $info
111
 *   The variable info array, keyed by variable name.
112
 *
113
 * @see hook_variable_info()
114
 */
115
function hook_variable_info_alter(&$info) {
116
}
117

    
118
/**
119
 * Define types of variables or list of values used by a module.
120
 *
121
 * These subtypes can be used to provide defaults for all properties of variables of this type
122
 * or to provide a list of options either for variable options (selectable values) or for children
123
 * variables in the case of multiple variables.
124
 *
125
 * Example, three usages of variable type:
126
 * @code
127
 *   // Use variable type 'weekday' to provide a selector for a day of the week
128
 *   $variables['date_first_day'] = array(
129
 *            'type' => 'weekday',
130
 *            'title' => t('First day of week'),
131
 *            'default' => 0,
132
 *          );
133
 *
134
 *   // Use 'options' with value 'weekday' for any other variable that needs to provide a selectable
135
 *   // list of days of the week. In this example you can select one or more days.
136
 *   $variables['working_days'] = array(
137
 *           'type' => 'options',
138
 *    'options' => 'weekday',
139
 *    'title' => t('Select working days from the list.'),
140
 *   );
141
 *
142
 *   // Use 'multiple' with value 'weekday' to create a subset of variables, one for each day of the week.
143
 *   // In fact, using '[weekday]' in the variable name will set these properties ('type' and 'multiple') automatically.
144
 *          $variables['daily_greeting_[weekday]'] = array(
145
 *      'type' => 'multiple',
146
 *                         'multiple' => 'weekday',
147
 *      'repeat' => array('type' => 'string'),
148
 *      'title' => t('Greeting to display each day of the week'),
149
 *          );
150
 * @endcode
151
 *
152
 * @return
153
 *   An array of information defining variable types. The array contains
154
 *   a sub-array for each variable type, with the variable type as the key.
155
 *
156
 *   The possible attributes are the same as for hook_variable_info(), with the
157
 *   type attributes being added on top of the variable attributes.
158
 *
159
 *   A special attribute:
160
 *   - "type": Variable subtype, the properties for the subtype will be added to these ones.
161
 *
162
 * @see hook_variable_type_info_alter()
163
 */
164
function hook_variable_type_info() {
165
  $type['mail_address'] = array(
166
    'title' => t('E-mail address'),
167
    'element' => array('#type' => 'textfield'),
168
    'token' => TRUE,
169
  );
170
  $type['mail_text'] = array(
171
    'title' => t('Mail text'),
172
    'multiple' => array('subject' => t('Subject'), 'body' => t('Body')),
173
    'build callback' => 'variable_build_mail_text',
174
    'localize' => TRUE,
175
    'type' => 'multiple',
176
  );
177
  return $type;
178
}
179

    
180
/**
181
 * Alter the variable type definitions.
182
 *
183
 * @param $info
184
 *   The variable type info array, keyed by variable type name.
185
 *
186
 * @see hook_variable_type_info()
187
 */
188
function hook_variable_type_info_alter(&$info) {
189
}
190

    
191
/**
192
 * Define groups of variables used by a module.
193
 *
194
 * Variable groups are used for presentation only, to display and edit the variables
195
 * on manageable groups. Groups can define a subset of a module's variables and can
196
 * be reused accross modules to group related variables.
197
 *
198
 * A form to edit all variables in a group can be generated with:
199
 *
200
 *   drupal_get_form('variable_group_form', group_name);
201
 *
202
 * @return
203
 *   An array of information defining variable types. The array contains
204
 *   a sub-array for each variable group, with the group as the key.
205
 *   Possible attributes:
206
 *   - "title": The human readable name of the group. Must be localized.
207
 *   - "description": The human readable description of the group. Must be localized.
208
 *   - "access": Permission required to edit group's variables. Will default to 'administer site configuration'.
209
 *   - "path": Array of administration paths where these variables can be accessed.
210
 *
211
 * @see hook_variable_group_info_alter()
212
 */
213
function hook_variable_group_info() {
214
  $groups['system_site_information'] = array(
215
    'title' => t('Site information'),
216
    'description' => t('Site information and maintenance mode'),
217
    'access' => 'administer site configuration',
218
    'path' => array('admin/config/system/site-information', 'admin/config/development/maintenance'),
219
  );
220
  $groups['system_feed_settings'] = array(
221
    'title' => t('Feed settings'),
222
    'description' => t('Feed settings'),
223
    'access' => 'administer site configuration',
224
  );
225
  return $groups;
226
}
227

    
228
/**
229
 * Alter the variable group definitions.
230
 *
231
 * @param $info
232
 *   The variable type info array, keyed by variable group name.
233
 *
234
 * @see hook_variable_group_info()
235
 */
236
function hook_variable_group_info_alter(&$info) {
237
}
238

    
239
/**
240
 * Alter system settings forms.
241
 *
242
 * This is a special version of hook_form_alter() that is triggered only for
243
 * system settings forms, and only after any other module has added/removed
244
 * variables using hook_form_alter().
245
 *
246
 * It is used to mark / replace special realm variables that are contained in the form.
247
 *
248
 * @see hook_form_alter()
249
 * @see variable_module_implements_alter()
250
 */
251
function hook_variable_settings_form_alter(&$form, &$form_state, $form_id) {
252
}