variable.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * @file
  4. * Variable API module. Extended API.
  5. */
  6. /**
  7. * Build generic variable information
  8. */
  9. function variable_build_info($type, $options) {
  10. variable_module_include();
  11. switch ($type) {
  12. case 'variable':
  13. return variable_build_list_info($options);
  14. case 'group':
  15. case 'type':
  16. $info = variable_invoke_all('variable_' . $type . '_info');
  17. drupal_alter('variable_' . $type . '_info', $info);
  18. return $info;
  19. }
  20. }
  21. /**
  22. * Build variable information
  23. */
  24. function variable_build_list_info($options) {
  25. $variables = array();
  26. foreach (module_implements('variable_info') as $module) {
  27. $result = call_user_func($module . '_variable_info', $options);
  28. if (isset($result) && is_array($result)) {
  29. foreach ($result as $name => $variable) {
  30. // Support name => title declarations
  31. $variable = is_array($variable) ? $variable : array('title' => $variable);
  32. $variable += array('name' => $name, 'module' => $module);
  33. // Check variable name for multiple values
  34. $multiple = NULL;
  35. if (preg_match('/\[(\w+)\]/', $name, $matches)) {
  36. $multiple = $matches[1];
  37. $variable += array('type' => 'multiple');
  38. }
  39. $variable += array('group' => 'default', 'type' => 'default');
  40. $variables[$name] = $variable + variable_get_type($variable['type']);
  41. // Add this at the end so it doesn't override type properties
  42. if (!empty($multiple)) {
  43. $variables[$name] += array('multiple' => $multiple);
  44. }
  45. }
  46. }
  47. }
  48. // Last chance for modules to alter variable info.
  49. drupal_alter('variable_info', $variables, $options);
  50. return $variables;
  51. }
  52. /**
  53. * Build multiple variables
  54. */
  55. function variable_build_multiple($variable, $options) {
  56. // Invoke variable callbacks
  57. if (!empty($variable['multiple callback'])) {
  58. $variable['multiple'] = variable_callback($variable['multiple callback'], $variable, $options);
  59. }
  60. if (isset($variable['multiple'])) {
  61. if (!is_array($variable['multiple'])) {
  62. $variable['multiple'] = variable_option_list($variable['multiple'], $variable, $options);
  63. }
  64. $variable += array('children' => array(), 'repeat' => array());
  65. // Build children variables with the name => value array
  66. foreach ($variable['multiple'] as $key => $title) {
  67. $name = preg_replace('/\[\w+\]/', $key, $variable['name']);
  68. // Be careful to respect previously set properties, add but do not override.
  69. $child = isset($variable['children'][$name]) ? $variable['children'][$name] : array();
  70. $child += $variable['repeat'];
  71. $child += array(
  72. 'name' => $name, 'index' => $key, 'title' => $title,
  73. 'type' => 'default', 'parent' => $variable['name'], 'module' => $variable['module'],
  74. );
  75. // Set default value from parent
  76. if (isset($variable['default']) && is_array($variable['default']) && isset($variable['default'][$key])) {
  77. $child += array('default' => $variable['default'][$key]);
  78. }
  79. $child += variable_get_type($child['type']);
  80. $variable['children'][$name] = $child;
  81. }
  82. }
  83. return $variable;
  84. }
  85. /**
  86. * Build variable with options
  87. */
  88. function variable_build_options($variable, $options) {
  89. $variable = variable_build_variable($variable, $options);
  90. if (isset($variable['options callback'])) {
  91. $variable['options'] = variable_callback($variable['options callback'], $variable, $options);
  92. }
  93. if (!empty($variable['options']) && !is_array($variable['options'])) {
  94. $variable['options'] = variable_option_list($variable['options'], $variable, $options);
  95. }
  96. return $variable;
  97. }
  98. /**
  99. * Build single variable
  100. *
  101. * Some variables may spawn into multiple ones
  102. */
  103. function variable_build_variable($variable, $options = array()) {
  104. if (empty($variable['built'])) {
  105. variable_include($variable);
  106. // Mark as built so we don't build it again
  107. $variable['built'] = TRUE;
  108. $options = _variable_options($options);
  109. $variable = _variable_variable($variable, $options);
  110. // If the variable has a build callback, go for it
  111. if (isset($variable['build callback'])) {
  112. $variable = variable_callback($variable['build callback'], $variable, $options);
  113. }
  114. }
  115. return $variable;
  116. }
  117. /**
  118. * Invoke variable callback
  119. *
  120. * @param $callback
  121. * Function name to invoke or array with module and funcion in this order
  122. * @param $variable
  123. * Array of variable information.
  124. * @param $options
  125. * Options to pass to the callback
  126. * @param $module
  127. * Optional module to include its '*.variable.inc' file if the function not found
  128. */
  129. function variable_callback($callback, $variable, $options = array()) {
  130. if (is_array($callback)) {
  131. list($module, $function) = $callback;
  132. }
  133. else {
  134. $function = $callback;
  135. }
  136. if (!function_exists($function)) {
  137. if (isset($module)) {
  138. variable_module_include($module);
  139. }
  140. else {
  141. variable_include($variable);
  142. }
  143. }
  144. return call_user_func($function, $variable, $options);
  145. }
  146. /**
  147. * List variables for a group
  148. */
  149. function variable_list_group($group) {
  150. $list = array();
  151. foreach (variable_get_info() as $name => $variable) {
  152. if ($variable['group'] == $group) {
  153. $list[$name] = $variable;
  154. }
  155. }
  156. return $list;
  157. }
  158. /**
  159. * List variables for a module
  160. */
  161. function variable_list_module($module) {
  162. $list = array();
  163. foreach (variable_get_info() as $name => $variable) {
  164. if ($variable['module'] == $module) {
  165. $list[$name] = $variable;
  166. }
  167. }
  168. return $list;
  169. }
  170. /**
  171. * Fetch options for variable
  172. */
  173. function variable_option_list($type, $variable, $options) {
  174. $cache = &drupal_static(__FUNCTION__);
  175. if (isset($cache[$type])) {
  176. return $cache[$type];
  177. }
  178. elseif ($info = variable_get_type($type)) {
  179. if (isset($info['options callback'])) {
  180. $info['options'] = variable_callback(array($info['module'], $info['options callback']), $variable, $options);
  181. }
  182. if (!empty($info['cache'])) {
  183. $cache[$type] = $info['options'];
  184. }
  185. return $info['options'];
  186. }
  187. else {
  188. return array();
  189. }
  190. }
  191. /**
  192. * General function to include variable definitions for all modules
  193. */
  194. function variable_module_include($modules = NULL) {
  195. static $core_modules = array('locale', 'forum', 'menu', 'node', 'system', 'taxonomy', 'translation', 'user');
  196. static $included = array();
  197. $modules = $modules ? (is_array($modules) ? $modules : array($modules)) : $core_modules;
  198. foreach ($modules as $module) {
  199. if (!isset($included[$module])) {
  200. if (module_exists($module)) {
  201. if (in_array($module, $core_modules)) {
  202. $included[$module] = module_load_include('variable.inc', 'variable', 'includes/' . $module);
  203. }
  204. else {
  205. $included[$module] = module_load_include('variable.inc', $module);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. /**
  212. * Disable variables for module
  213. *
  214. * Store module variables so we can delete them if uninstalled
  215. */
  216. function variable_module_disable($module) {
  217. }
  218. /**
  219. * Disable variables for module
  220. *
  221. * Store module variables so we can delete them if uninstalled
  222. */
  223. function variable_module_enable($module) {
  224. if ($variables = variable_list_module($module)) {
  225. $list = variable_get('variable_module_list', array());
  226. $list[$module] = variable_children($variables);
  227. variable_set('variable_module_list', $list);
  228. }
  229. }
  230. /**
  231. * Uninstall variables for module
  232. *
  233. * This will be called from variable_modules_uninstalled(), no need to implement it directly.
  234. */
  235. function variable_module_uninstall($module) {
  236. $list = variable_get('variable_module_list', array());
  237. if (isset($list[$module])) {
  238. // This is a plain list of variables so we can use raw delete.
  239. array_map('variable_delete', $list[$module]);
  240. unset($list[$module]);
  241. variable_set('variable_module_list', $list);
  242. }
  243. }
  244. /**
  245. * Get value for multiple variable
  246. */
  247. function variable_multiple_get_value($variable, $options = array()) {
  248. $variable = variable_build($variable, $options);
  249. $values = array();
  250. foreach ($variable['children'] as $child) {
  251. $values[$child['index']] = variable_get_value($child, $options);
  252. }
  253. return $values;
  254. }
  255. /**
  256. * Get defaults for multiple variable
  257. */
  258. function variable_multiple_get_default($variable, $options = array()) {
  259. $variable = variable_build($variable, $options);
  260. $values = array();
  261. foreach ($variable['children'] as $child) {
  262. $values[$child['index']] = variable_get_default($child, $options);
  263. }
  264. return $values;
  265. }