variable.api.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Variable module.
  5. */
  6. /**
  7. * @defgroup variable_api_hooks Variable API Hooks
  8. * @{
  9. * Functions to define and modify information about variables.
  10. *
  11. * These hooks and all the related callbacks may be defined in a separate file
  12. * named module.variable.inc
  13. *
  14. * @}
  15. */
  16. /**
  17. * Define variables used by a module.
  18. *
  19. * Provides meta-information for each variable that includes at the very least some human readable title.
  20. * This information may be used by other modules to select variables from a list for translating, exporting, etc.
  21. *
  22. * Though not required we can also provide some more information to be able to handle the variable in an effective
  23. * way, like which type of data and form element it uses, default value, etc.. There are multiple predefined
  24. * variable types ('type' attribute) that will add a predefined set of properties. Some of them are:
  25. *
  26. * - "string": Simple plain string variable. The form element will be a text field and it will be localizable.
  27. * - "number": Simple numeric value. The form element will be a text field.
  28. * - "boolean": Simple TRUE/FALSE value. It will be a checkbox.
  29. * - "enable": Enabled / Disabled selector. It will display as two radio buttons.
  30. * - "select": Selectable list of options. Depending on the number of options, the element will be a list of
  31. * radios or a drop down.
  32. * - "options": List of options with multiple choices. It will be a list of checkboxes.
  33. * ...
  34. *
  35. * More variable types can be defined by modules using hook_variable_type_info().
  36. *
  37. * For the case of variable names that depend on some other parameter (like variables per content-type),
  38. * there's some special type of variables: Multiple variables. These can be defined like this:
  39. *
  40. * @code
  41. * $variables['node_options_[node_type]'] = array(
  42. * 'type' => 'multiple',
  43. * 'title' => t('Default options', array(), $options),
  44. * 'repeat' => array(
  45. * 'type' => 'options',
  46. * 'default' => array('status', 'promote'),
  47. * 'options callback' => 'node_variable_option_list',
  48. * ),
  49. * );
  50. * @endcode
  51. *
  52. * This multiple variable will spawn into one variable for each node type. Note the variable name that includes
  53. * the property [node_type]. Values for [node_type] will be defined on hook_variable_type_info().
  54. *
  55. * The 'repeat' property defines the properties of children variables. In this case the 'type' property is optional
  56. * and will default to 'multiple'.
  57. *
  58. * @param $options
  59. * Array of options to build variable properties. Since variable properties are cached per language
  60. * these options should be used at the very least for string translations, so titles and defaults are
  61. * localized. Possible options:
  62. * - "language" => Language object for which strings and defaults must be returned. This one will be always defined.
  63. *
  64. * @return
  65. * An array of information defining the module's variables. The array
  66. * contains a sub-array for each node variable, with the variable name
  67. * as the key. Possible attributes:
  68. * - "title": The human readable name of the variable, will be used in auto generated forms.
  69. * - "type": Variable type, should be one of the defined on hook_variable_type_info().
  70. * - "group": Group key, should be one of the defined on hook_variable_group_info().
  71. * - "description": Variable description, will be used in auto generated forms.
  72. * - "options": Array of selectable options, or option name as defined on hook_variable_option_info().
  73. * - "options callback": Function to invoke to get the list of options.
  74. * - "default": Default value.
  75. * - "default callback": Function to invoke to get the default value.
  76. * - "multiple": Array of multiple children variables to be created from this one.
  77. * - "multiple callback": Function to invoke to get children variables.
  78. * - "element": Form element properties to override the default ones for this variable type.
  79. * - "element callback": Function to invoke to get a form element for this variable.
  80. * - "module": Module to which this variable belongs. This property will be added automatically.
  81. * - "repeat": Array of variable properties for children variables.
  82. * - "localize": Boolean value, TRUE for variables that should be localized. This may be used by other modules.
  83. * - "validate callback": Callback to validate the variable value, it will be added to form element #validate.
  84. *
  85. * @see hook_variable_info_alter()
  86. */
  87. function hook_variable_info($options) {
  88. $variables['site_name'] = array(
  89. 'type' => 'string',
  90. 'title' => t('Name', array(), $options),
  91. 'default' => 'Drupal',
  92. 'description' => t('The name of this website.', array(), $options),
  93. 'required' => TRUE,
  94. );
  95. $variables['site_403'] = array(
  96. 'type' => 'drupal_path',
  97. 'title' => t('Default 403 (access denied) page', array(), $options),
  98. 'default' => '',
  99. '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),
  100. );
  101. return $variables;
  102. }
  103. /**
  104. * Alter the variable definitions.
  105. *
  106. * @param $info
  107. * The variable info array, keyed by variable name.
  108. *
  109. * @see hook_variable_info()
  110. */
  111. function hook_variable_info_alter(&$info) {
  112. }
  113. /**
  114. * Define types of variables or list of values used by a module.
  115. *
  116. * These subtypes can be used to provide defaults for all properties of variables of this type
  117. * or to provide a list of options either for variable options (selectable values) or for children
  118. * variables in the case of multiple variables.
  119. *
  120. * Example, three usages of variable type:
  121. * @code
  122. * // Use variable type 'weekday' to provide a selector for a day of the week
  123. * $variables['date_first_day'] = array(
  124. * 'type' => 'weekday',
  125. * 'title' => t('First day of week'),
  126. * 'default' => 0,
  127. * );
  128. *
  129. * // Use 'options' with value 'weekday' for any other variable that needs to provide a selectable
  130. * // list of days of the week. In this example you can select one or more days.
  131. * $variables['working_days'] = array(
  132. * 'type' => 'options',
  133. * 'options' => 'weekday',
  134. * 'title' => t('Select working days from the list.'),
  135. * );
  136. *
  137. * // Use 'multiple' with value 'weekday' to create a subset of variables, one for each day of the week.
  138. * // In fact, using '[weekday]' in the variable name will set these properties ('type' and 'multiple') automatically.
  139. * $variables['daily_greeting_[weekday]'] = array(
  140. * 'type' => 'multiple',
  141. * 'multiple' => 'weekday',
  142. * 'repeat' => array('type' => 'string'),
  143. * 'title' => t('Greeting to display each day of the week'),
  144. * );
  145. * @endcode
  146. *
  147. * @return
  148. * An array of information defining variable types. The array contains
  149. * a sub-array for each variable type, with the variable type as the key.
  150. *
  151. * The possible attributes are the same as for hook_variable_info(), with the
  152. * type attributes being added on top of the variable attributes.
  153. *
  154. * A special attribute:
  155. * - "type": Variable subtype, the properties for the subtype will be added to these ones.
  156. *
  157. * @see hook_variable_type_info_alter()
  158. */
  159. function hook_variable_type_info() {
  160. $type['mail_address'] = array(
  161. 'title' => t('E-mail address'),
  162. 'element' => array('#type' => 'textfield'),
  163. 'token' => TRUE,
  164. );
  165. $type['mail_text'] = array(
  166. 'title' => t('Mail text'),
  167. 'multiple' => array('subject' => t('Subject'), 'body' => t('Body')),
  168. 'build callback' => 'variable_build_mail_text',
  169. 'localize' => TRUE,
  170. 'type' => 'multiple',
  171. );
  172. return $type;
  173. }
  174. /**
  175. * Alter the variable type definitions.
  176. *
  177. * @param $info
  178. * The variable type info array, keyed by variable type name.
  179. *
  180. * @see hook_variable_type_info()
  181. */
  182. function hook_variable_type_info_alter(&$info) {
  183. }
  184. /**
  185. * Define groups of variables used by a module.
  186. *
  187. * Variable groups are used for presentation only, to display and edit the variables
  188. * on manageable groups. Groups can define a subset of a module's variables and can
  189. * be reused accross modules to group related variables.
  190. *
  191. * A form to edit all variables in a group can be generated with:
  192. *
  193. * drupal_get_form('variable_group_form', group_name);
  194. *
  195. * @return
  196. * An array of information defining variable types. The array contains
  197. * a sub-array for each variable group, with the group as the key.
  198. * Possible attributes:
  199. * - "title": The human readable name of the group. Must be localized.
  200. * - "description": The human readable description of the group. Must be localized.
  201. * - "access": Permission required to edit group's variables. Will default to 'administer site configuration'.
  202. * - "path": Array of administration paths where these variables can be accessed.
  203. *
  204. * @see hook_variable_group_info_alter()
  205. */
  206. function hook_variable_group_info() {
  207. $groups['system_site_information'] = array(
  208. 'title' => t('Site information'),
  209. 'description' => t('Site information and maintenance mode'),
  210. 'access' => 'administer site configuration',
  211. 'path' => array('admin/config/system/site-information', 'admin/config/development/maintenance'),
  212. );
  213. $groups['system_feed_settings'] = array(
  214. 'title' => t('Feed settings'),
  215. 'description' => t('Feed settings'),
  216. 'access' => 'administer site configuration',
  217. );
  218. return $groups;
  219. }
  220. /**
  221. * Alter the variable group definitions.
  222. *
  223. * @param $info
  224. * The variable type info array, keyed by variable group name.
  225. *
  226. * @see hook_variable_group_info()
  227. */
  228. function hook_variable_group_info_alter(&$info) {
  229. }
  230. /**
  231. * Alter system settings forms.
  232. *
  233. * This is a special version of hook_form_alter() that is triggered only for
  234. * system settings forms, and only after any other module has added/removed
  235. * variables using hook_form_alter().
  236. *
  237. * It is used to mark / replace special realm variables that are contained in the form.
  238. *
  239. * @see hook_form_alter()
  240. * @see variable_module_implements_alter()
  241. */
  242. function hook_variable_settings_form_alter(&$form, &$form_state, $form_id) {
  243. }