variable.variable.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * @file
  4. * Variable module hook implementations
  5. */
  6. /**
  7. * Implements hook_variable_group_info().
  8. */
  9. function variable_variable_group_info() {
  10. // Group for variable that have no group
  11. $groups['default'] = array(
  12. 'title' => t('Other'),
  13. 'description' => t("Variables that don't belong to any other group."),
  14. );
  15. $groups['debug'] = array(
  16. 'title' => t('Debug'),
  17. 'description' => t('Debug and development options.'),
  18. );
  19. $groups['variable'] = array(
  20. 'title' => t('Variable'),
  21. 'description' => t('Variables that contain metadata about the variable system.'),
  22. );
  23. return $groups;
  24. }
  25. /**
  26. * Implements hook_variable_type_info().
  27. */
  28. function variable_variable_type_info() {
  29. // Array of values
  30. $type['array'] = array(
  31. 'title' => t('Array'),
  32. 'element' => array('#type' => 'fieldset', '#tree' => TRUE),
  33. // Properties for each array item
  34. 'repeat' => array(
  35. 'element' => array('#type' => 'textfield'),
  36. ),
  37. 'format callback' => 'variable_format_array',
  38. 'element callback' => 'variable_form_element_array',
  39. 'default' => array(),
  40. );
  41. // Array whose keys are named properties.
  42. $type['properties'] = array(
  43. 'title' => t('Properties'),
  44. 'format callback' => 'variable_format_properties',
  45. 'type' => 'array',
  46. );
  47. // TRUE / FALSE value, checkbox
  48. $type['boolean'] = array(
  49. 'title' => t('Boolean'),
  50. 'element' => array('#type' => 'checkbox'),
  51. 'format callback' => 'variable_format_boolean',
  52. );
  53. // Default type for variables with no other type
  54. $type['default'] = array(
  55. 'title' => t('Default'),
  56. 'element' => array('#type' => 'textfield'),
  57. 'access' => 'administer site configuration',
  58. );
  59. // Enable/Disable
  60. $type['enable'] = array(
  61. 'title' => t('Enable'),
  62. 'options' => array(t('Disabled'), t('Enabled')),
  63. 'default' => 0,
  64. 'element' => array('#type' => 'radios'),
  65. 'format callback' => 'variable_format_selection',
  66. );
  67. // Multiple variable that will spawn into multiple elements
  68. $type['multiple'] = array(
  69. 'title' => t('Multiple'),
  70. 'element' => array('#type' => 'fieldset'),
  71. 'build callback' => 'variable_build_multiple',
  72. 'format callback' => 'variable_format_multiple',
  73. 'element callback' => 'variable_form_element_multiple',
  74. 'value callback' => 'variable_multiple_get_value',
  75. 'default callback' => 'variable_multiple_get_default',
  76. );
  77. $type['mail_address'] = array(
  78. 'title' => t('E-mail address'),
  79. 'element' => array('#type' => 'textfield'),
  80. 'token' => TRUE,
  81. );
  82. $type['mail_text'] = array(
  83. 'title' => t('Mail text'),
  84. 'multiple' => array('subject' => t('Subject'), 'body' => t('Body')),
  85. 'build callback' => 'variable_build_mail_text',
  86. 'localize' => TRUE,
  87. 'type' => 'multiple',
  88. );
  89. $type['number'] = array(
  90. 'title' => t('Number'),
  91. 'element' => array('#type' => 'textfield', '#size' => 15, '#maxlength' => 10),
  92. 'token' => TRUE,
  93. 'validate callback' => 'variable_validate_number',
  94. );
  95. // Select multiple options from multiple choices
  96. $type['options'] = array(
  97. 'title' => t('Options'),
  98. 'options' => TRUE,
  99. 'element' => array('#type' => 'checkboxes'),
  100. 'element callback' => 'variable_form_element_options',
  101. 'format callback' => 'variable_format_options',
  102. );
  103. // Select single option from multiple choices
  104. $type['select'] = array(
  105. 'title' => t('Select'),
  106. 'options' => TRUE,
  107. // This will become radios or drop-down depending on the number of options
  108. 'element callback' => 'variable_form_element_options',
  109. 'format callback' => 'variable_format_selection',
  110. );
  111. // Select number from array of values. Options array that can be list of numbers will be converted to a value => value
  112. $type['select_number'] = array(
  113. 'title' => t('Select'),
  114. 'options' => TRUE,
  115. 'element callback' => 'variable_form_element_options',
  116. 'options callback' => 'variable_options_select_number',
  117. );
  118. $type['string'] = array(
  119. 'title' => t('String'),
  120. 'element' => array('#type' => 'textfield'),
  121. 'localize' => TRUE,
  122. 'format callback' => 'variable_format_string',
  123. 'token' => TRUE,
  124. // This type may have an 'allowed tags' attribute.
  125. // If empty it will be formatted as plain text
  126. 'allowed tags' => array(),
  127. );
  128. $type['text'] = array(
  129. 'title' => t('Text'),
  130. 'element' => array('#type' => 'textarea'),
  131. 'localize' => TRUE,
  132. 'format callback' => 'variable_format_text',
  133. 'token' => TRUE,
  134. // This type may have an 'allowed tags' attribute.
  135. // If empty it will be formatted with filter_xss_admin.
  136. 'allowed tags' => array(),
  137. );
  138. // Default type for variables with no other type
  139. $type['unknown'] = array(
  140. 'title' => t('Unknown'),
  141. 'access' => 'administer site configuration',
  142. 'format' => 'variable_format_unknown',
  143. 'element callback' => 'variable_form_element_unknown',
  144. 'element' => array('#type' => 'item'),
  145. );
  146. $type['url'] = array(
  147. 'title' => t('URL'),
  148. 'element' => array('#type' => 'textfield', '#size' => 80, '#maxlength' => 255),
  149. 'token' => TRUE,
  150. );
  151. $type['mail_part'] = array(
  152. 'title' => t('Mail parts'),
  153. 'options' => array('subject' => t('Subject'), 'body' => t('Body')),
  154. );
  155. $type['text_format'] = array(
  156. 'title' => t('Formatted text'),
  157. 'element' => array('#type' => 'text_format'),
  158. 'element callback' => 'variable_form_element_text_format',
  159. 'format callback' => 'variable_format_text_format',
  160. 'default callback' => 'variable_text_format_default',
  161. 'localize' => TRUE,
  162. );
  163. return $type;
  164. }
  165. /**
  166. * Build multiple mail variable
  167. */
  168. function variable_build_mail_text($variable, $options = array()) {
  169. $name = str_replace('[mail_part]', '', $variable['name']);
  170. // For mail text, children have different types
  171. $variable['children'][$name . 'subject']['type'] = 'string';
  172. $variable['children'][$name . 'body']['type'] = 'text';
  173. $variable = variable_build_multiple($variable, $options);
  174. return $variable;
  175. }
  176. /**
  177. * Format select variable
  178. */
  179. function variable_format_selection($variable, $options = array()) {
  180. $variable = variable_build_options($variable, $options);
  181. if (isset($variable['value'])) {
  182. return isset($variable['options'][$variable['value']]) ? $variable['options'][$variable['value']] : '<' . t('Invalid option') . '>';
  183. }
  184. else {
  185. return variable_format_empty($variable);
  186. }
  187. }
  188. /**
  189. * Format options variable. Value is an array of options.
  190. */
  191. function variable_format_options($variable, $options = array()) {
  192. $variable = variable_build_options($variable, $options);
  193. $names = array();
  194. if (isset($variable['value']) && $variable['value']) {
  195. if (is_array($variable['value'])) {
  196. foreach ($variable['value'] as $index => $value) {
  197. $names[$index] = isset($variable['options'][$value]) ? $variable['options'][$value] : '<' . t('Invalid option') . '>';
  198. }
  199. return implode(', ', $names);
  200. }
  201. else {
  202. return '<' . t('Invalid value') . '>';
  203. }
  204. }
  205. else {
  206. return variable_format_empty($variable);
  207. }
  208. }
  209. /**
  210. * Format array variable, handling nested arrays
  211. */
  212. function variable_format_array($variable = NULL, $options = array()) {
  213. if (empty($variable['value'])) {
  214. return variable_format_empty($variable);
  215. }
  216. else {
  217. $list = array();
  218. foreach ($variable['value'] as $index => $item) {
  219. if (is_array($item) || is_object($item)) {
  220. $list[$index] = variable_format_array(array('value' => (array)$item), $options);
  221. }
  222. else {
  223. $list[$index] = check_plain((string)$item);
  224. }
  225. }
  226. return theme('item_list', array('items' => $list));
  227. }
  228. }
  229. /**
  230. * Format array variable with known keys, handling nested arrays
  231. */
  232. function variable_format_properties($variable = NULL, $options = array()) {
  233. if (empty($variable['value'])) {
  234. return variable_format_empty($variable);
  235. }
  236. else {
  237. $rows = array();
  238. foreach ($variable['value'] as $name => $item) {
  239. $title = check_plain((string)$name);
  240. if (is_array($item) || is_object($item)) {
  241. $value = variable_format_array(array('value' => (array)$item), $options);
  242. }
  243. else {
  244. $value = check_plain((string)$item);
  245. }
  246. $rows[] = array('<em>' . $title . '</em>', $value);
  247. }
  248. return theme('table', array('rows' => $rows));
  249. }
  250. }
  251. /**
  252. * Format boolean variable
  253. */
  254. function variable_format_boolean($variable, $options = array()) {
  255. if (isset($variable['value'])) {
  256. return $variable['value'] ? t('True') : t('False');
  257. }
  258. else {
  259. return t('Undefined');
  260. }
  261. }
  262. /**
  263. * Format variable empty value
  264. */
  265. function variable_format_empty($variable) {
  266. return isset($variable['empty']) ? $variable['empty'] : t('Empty');
  267. }
  268. /**
  269. * Format variable as string. Either check plain for filter_xss.
  270. */
  271. function variable_format_string($variable, $options = array()) {
  272. if (empty($variable['value'])) {
  273. return '';
  274. }
  275. elseif (!empty($variable['allowed tags'])) {
  276. return filter_xss($variable['value'], $variable['allowed tags']);
  277. }
  278. else {
  279. return check_plain($variable['value']);
  280. }
  281. }
  282. /**
  283. * Format text variable
  284. */
  285. function variable_format_text($variable, $options = array()) {
  286. if (empty($variable['value'])) {
  287. return '';
  288. }
  289. elseif (!empty($variable['allowed tags'])) {
  290. return filter_xss($variable['value'], $variable['allowed tags']);
  291. }
  292. else {
  293. return filter_xss_admin($variable['value']);
  294. }
  295. }
  296. /**
  297. * Options callback for numeric select
  298. */
  299. function variable_options_select_number($variable, $options = array()) {
  300. return drupal_map_assoc($variable['options']);
  301. }
  302. /**
  303. * Default callback for text_format.
  304. */
  305. function variable_text_format_default($variable, $options = array()) {
  306. $out = array(
  307. 'value' => '',
  308. 'format' => filter_default_format(),
  309. );
  310. if (!empty($variable['default'])) {
  311. if (is_string($variable['default'])) {
  312. $out['value'] = $variable['default'];
  313. }
  314. elseif (is_array($variable['default'])) {
  315. if (isset($variable['default']['value'])) {
  316. $out['value'] = $variable['default']['value'];
  317. }
  318. if (isset($variable['default']['format'])) {
  319. $out['format'] = $variable['default']['format'];
  320. }
  321. }
  322. }
  323. return $out;
  324. }
  325. /**
  326. * Format text_format.
  327. */
  328. function variable_format_text_format($variable, $options = array()) {
  329. return check_markup($variable['value']['value'], $variable['value']['format']);
  330. }
  331. /**
  332. * Format multiple variable.
  333. */
  334. function variable_format_multiple($variable, $options = array()) {
  335. $rows = array();
  336. foreach ($variable['children'] as $name => $child) {
  337. $rows[$name] = array(
  338. array('data' => check_plain($child['title']), 'header' => TRUE),
  339. variable_format_value($child)
  340. );
  341. }
  342. return theme('table', array('rows' => $rows));
  343. }
  344. /**
  345. * Validate numeric variable.
  346. */
  347. function variable_validate_number($variable) {
  348. if (!is_numeric($variable['value'])) {
  349. return t('The value is not a number.');
  350. }
  351. }