variable.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. /**
  3. * @file
  4. * Variable API module
  5. */
  6. /**
  7. * Implementation of hook_boot()
  8. *
  9. * Though we don't do anything here, we ensure the module is loaded at boot stage
  10. * for other modules (like variable_realm) to be able to use the API
  11. */
  12. function variable_boot() {
  13. }
  14. /**
  15. * @defgroup variable_api Variable API
  16. * @{
  17. * Get meta-information about variables and extended get/set methods
  18. *
  19. * Use these functions if you want to take full advantage of Variable API
  20. */
  21. /**
  22. * Check access to variable
  23. *
  24. * All variables are restricted for editing so unless we've got some explicit access
  25. * variables cannot be edited as default.
  26. *
  27. * @param $variable
  28. * Variable name or info array
  29. */
  30. function variable_access($variable, $account = NULL) {
  31. $account = $account ? $account : $GLOBALS['user'];
  32. if (user_access('administer site configuration', $account)) {
  33. return TRUE;
  34. }
  35. elseif ($variable = _variable_variable($variable)) {
  36. // Check parent variable if this is a child variable and doesn't have explicit access.
  37. if (!isset($variable['access']) && !empty($variable['parent'])) {
  38. return variable_access($variable['parent']);
  39. }
  40. // We need either a variable or group explicit access.
  41. $group = isset($variable['group']) ? variable_get_group($variable['group']) : array();
  42. if (!isset($group['access']) && !isset($variable['access']) ||
  43. isset($group['access']) && !user_access($group['access'], $account) ||
  44. isset($variable['access']) && !user_access($variable['access'], $account) )
  45. {
  46. return FALSE;
  47. }
  48. else {
  49. return TRUE;
  50. }
  51. }
  52. else {
  53. // We don't have information for such variable
  54. return FALSE;
  55. }
  56. }
  57. /**
  58. * Get list of variables expanding multiple ones
  59. *
  60. * @param $names
  61. * List of variable names or full variable arrays
  62. *
  63. * @return array()
  64. * List variable names with spawned multiple variables
  65. */
  66. function variable_children($names) {
  67. $names = is_array($names) ? $names : array($names);
  68. $list = array();
  69. foreach ($names as $name) {
  70. // We need to build the variable, it may be multiple
  71. $variable = variable_build($name);
  72. if (!empty($variable['children'])) {
  73. $list = array_merge($list, array_keys($variable['children']));
  74. }
  75. else {
  76. $list[] = $variable['name'];
  77. }
  78. }
  79. return $list;
  80. }
  81. /**
  82. * Map children variables to parent variables
  83. */
  84. function variable_parent($name) {
  85. $map = &drupal_static(__FUNCTION__);
  86. if (!isset($map)) {
  87. foreach (array_keys(variable_get_info()) as $key) {
  88. if ($children = variable_children($key)) {
  89. foreach ($children as $child) {
  90. $map[$child] = $key;
  91. }
  92. }
  93. }
  94. }
  95. return isset($map[$name]) ? $map[$name] : NULL;
  96. }
  97. /**
  98. * Format printable value
  99. *
  100. * @param $variable
  101. */
  102. function variable_format_value($variable, $options = array()) {
  103. $variable = variable_build($variable, $options);
  104. $variable['value'] = variable_get_value($variable, $options);
  105. if (isset($variable['value'])) {
  106. return !empty($variable['format callback']) ? variable_callback($variable['format callback'], $variable, $options) : variable_format_unknown($variable, $options);
  107. }
  108. else {
  109. return isset($variable['empty']) ? $variable['empty'] : t('Empty');
  110. }
  111. }
  112. /**
  113. * Format unknown variable
  114. */
  115. function variable_format_unknown($variable, $options = array()) {
  116. return '<pre>' . check_plain(print_r($variable['value'], TRUE)) . '</pre>';
  117. }
  118. /**
  119. * Get variable child element from multiple variable
  120. *
  121. * @param $parent
  122. * Parent variable
  123. * @param $key
  124. * Key of the children variable (not the full name, just the piece of string that makes the difference)
  125. */
  126. function variable_get_child($parent, $key, $options = array()) {
  127. $variable = variable_build($parent, $options);
  128. $name = preg_replace('/\[\w+\]/', $key, $variable['name']);
  129. $child = $variable['children'][$name];
  130. // Replace title and description
  131. foreach (array('title', 'description') as $property) {
  132. if (isset($variable[$property])) {
  133. $child[$property] = $variable[$property];
  134. }
  135. }
  136. return $child;
  137. }
  138. /**
  139. * Get variable information
  140. *
  141. * Variable information is collected from modules and cached by language
  142. *
  143. * @param $name
  144. * Optional variable name. Will return all if no name.
  145. * @param $options array
  146. * Options for variable values
  147. * - 'langcode', Language code
  148. */
  149. function variable_get_info($name = NULL, $options = array()) {
  150. $options = _variable_options($options);
  151. if (!$name) {
  152. return _variable_info('variable', NULL, $options);
  153. }
  154. elseif ($info = _variable_info('variable', $name, $options)) {
  155. return $info;
  156. }
  157. elseif ($parent = variable_parent($name)) {
  158. $info = variable_build(variable_get_info($parent));
  159. $child = $info['children'][$name];
  160. // Copy over some values from parent to child to add some context to it.
  161. $child['title'] = $info['title'] . ' [' . $child['title'] . ']';
  162. if (isset($info['description'])) {
  163. $child['description'] = $info['description'];
  164. }
  165. return $child;
  166. }
  167. else {
  168. return NULL;
  169. }
  170. }
  171. /**
  172. * Get variable group information.
  173. *
  174. * @param $group
  175. * Group name. Will return all if not name.
  176. */
  177. function variable_get_group($group = NULL) {
  178. return _variable_info('group', $group);
  179. }
  180. /**
  181. * Get variable type information.
  182. *
  183. * @param $type
  184. * Type name. Will return all if no name.
  185. */
  186. function variable_get_type($type = NULL) {
  187. $info = _variable_info('type', $type);
  188. if ($type && !empty($info['type'])) {
  189. // Add subtipe properties, recursive
  190. $info += variable_get_type($info['type']);
  191. }
  192. return $info;
  193. }
  194. /**
  195. * Get variable option information.
  196. */
  197. function variable_get_option($type = NULL) {
  198. return _variable_info('option', $type);
  199. }
  200. /**
  201. * Get value for simple scalar variable
  202. *
  203. * @param $variable
  204. * Variable name or array data
  205. * @param $options
  206. * Options array, it may have the following elements
  207. * - language => Language object
  208. * - default => Default value if not set
  209. * - realm => Realm object if working inside a variable realm
  210. */
  211. function variable_get_value($variable, $options = array()) {
  212. $variable = _variable_variable($variable, $options);
  213. if (isset($variable['value'])) {
  214. return $variable['value'];
  215. }
  216. elseif (!empty($variable['value callback'])) {
  217. variable_include($variable);
  218. return variable_callback($variable['value callback'], $variable, _variable_options($options));
  219. }
  220. else {
  221. if (!empty($options['realm'])) {
  222. $value = $options['realm']->variable_get($variable['name'], NULL);
  223. }
  224. else {
  225. $value = variable_get($variable['name'], NULL);
  226. }
  227. if (isset($value)) {
  228. return $value;
  229. }
  230. else {
  231. return isset($options['default']) ? $options['default'] : variable_get_default($variable, $options);
  232. }
  233. }
  234. }
  235. /**
  236. * Set variable value
  237. *
  238. * This basically sets the variable but also invokes hook_variable_update
  239. */
  240. function variable_set_value($name, $value, $options = array()) {
  241. $old_value = variable_get_value($name, NULL, $options);
  242. if (!empty($options['realm'])) {
  243. $options['realm']->variable_set($name, $value);
  244. }
  245. else {
  246. variable_set($name, $value);
  247. }
  248. module_invoke_all('variable_update', $name, $value, $old_value, $options);
  249. }
  250. /**
  251. * Get variable default
  252. *
  253. * @param $variable
  254. * Variable name or variable information
  255. */
  256. function variable_get_default($variable, $options = array()) {
  257. $variable = _variable_variable($variable, $options);
  258. if (isset($variable['default callback'])) {
  259. variable_include($variable);
  260. return call_user_func($variable['default callback'], $variable, _variable_options($options));
  261. }
  262. elseif (isset($variable['default'])) {
  263. return $variable['default'];
  264. }
  265. else {
  266. return NULL;
  267. }
  268. }
  269. /**
  270. * Delete variable (included children variables)
  271. */
  272. function variable_delete($variable, $options = array()) {
  273. $variable = _variable_variable($variable, $options);
  274. variable_include();
  275. // We need to build the variable, it may be multiple
  276. $variable = variable_build($variable, $options);
  277. if (!empty($variable['children'])) {
  278. $callback = !empty($options['realm']) ? array($options['realm'], 'variable_del') : 'variable_del';
  279. array_map($callback, array_keys($variable['children']));
  280. }
  281. elseif (!empty($options['realm'])) {
  282. $options['realm']->variable_del($variable['name']);
  283. }
  284. else {
  285. variable_del($variable['name']);
  286. }
  287. // Invoke the hook only once even if its multiple
  288. module_invoke_all('variable_delete', $variable, $options);
  289. }
  290. /**
  291. * Provide list of variable titles
  292. *
  293. * @param $names
  294. * List of variable names or full variable arrays
  295. * @return array()
  296. * List of name => title
  297. */
  298. function variable_list($names) {
  299. $list = array();
  300. foreach ($names as $name) {
  301. if ($variable = _variable_variable($name)) {
  302. $list[$variable['name']] = $variable['title'];
  303. }
  304. }
  305. return $list;
  306. }
  307. /**
  308. * Get human readable name for variable.
  309. */
  310. function variable_name($variable) {
  311. $variable = _variable_variable($variable);
  312. return $variable['title'];
  313. }
  314. /**
  315. * Get human readable names for variable list.
  316. */
  317. function variable_list_name($list) {
  318. $names = array_map('variable_name', $list);
  319. return implode(', ', $names);
  320. }
  321. /**
  322. * @} End of "defgroup variable_api".
  323. */
  324. /**
  325. * Build full variable data
  326. */
  327. function variable_build($variable, $options = array()) {
  328. variable_include();
  329. $variable = _variable_variable($variable, $options);
  330. return variable_build_variable($variable, $options);
  331. }
  332. /**
  333. * Clear cache
  334. */
  335. function variable_cache_clear() {
  336. cache_clear_all('*', 'cache_variable', TRUE);
  337. }
  338. /**
  339. * Implementation of hook_flush_caches()
  340. */
  341. function variable_flush_caches() {
  342. return array('cache_variable');
  343. }
  344. /**
  345. * Implements hook_element_info()
  346. */
  347. function variable_element_info() {
  348. // A fieldset with variable list
  349. $types['variable_fieldset'] = array(
  350. '#collapsible' => FALSE,
  351. '#collapsed' => FALSE,
  352. '#value' => NULL,
  353. '#process' => array('variable_element_process_fieldset', 'form_process_fieldset', 'ajax_process_form'),
  354. '#pre_render' => array('form_pre_render_fieldset'),
  355. '#theme_wrappers' => array('fieldset'),
  356. '#variable_list' => array(),
  357. );
  358. return $types;
  359. }
  360. /**
  361. * Process variable fieldset
  362. */
  363. function variable_element_process_fieldset($element) {
  364. $element += variable_edit_subform($element['#variable_list']);
  365. return $element;
  366. }
  367. /**
  368. * Implements hook_hook_info().
  369. */
  370. function variable_hook_info() {
  371. $hooks['variable_info'] = array(
  372. 'group' => 'variable',
  373. );
  374. $hooks['variable_group_info'] = array(
  375. 'group' => 'variable',
  376. );
  377. $hooks['variable_type_info'] = array(
  378. 'group' => 'variable',
  379. );
  380. $hooks['variable_settings_form_alter'] = array(
  381. 'group' => 'variable',
  382. );
  383. return $hooks;
  384. }
  385. /**
  386. * Form for variable list
  387. *
  388. * @param $list
  389. * Variable name or list of variable names.
  390. * @param $options
  391. * Optional array with variable options.
  392. */
  393. function variable_edit_form($form, &$form_state, $list, $options = array()) {
  394. $list = is_array($list) ? $list : array($list);
  395. $form = variable_base_form($form, $form_state, $list, $options);
  396. $form += variable_edit_subform($list, $options);
  397. return variable_settings_form($form, $options);
  398. }
  399. /**
  400. * Build base form for variable list without fields.
  401. *
  402. * @param $list
  403. * List of variable names.
  404. * @param $options
  405. * Optional array with variable options.
  406. */
  407. function variable_base_form($form, &$form_state, $list, $options = array()) {
  408. form_load_include($form_state, 'form.inc', 'variable');
  409. // Pass on the values on the form for further reference.
  410. $form['#variable_edit_form'] = $list;
  411. $form['#variable_options'] = $options;
  412. // Run submit callback for variables in form.
  413. $form['#submit'][] = 'variable_form_submit_callback';
  414. return $form;
  415. }
  416. /**
  417. * Form elements for variable list.
  418. *
  419. * @param $list
  420. * Variable name or array of variable names..
  421. * @param $options
  422. * Regular variable options (language, realm, etc) and optional 'form parents' array.
  423. */
  424. function variable_edit_subform($list, $options = array()) {
  425. module_load_include('form.inc', 'variable');
  426. $list = is_array($list) ? $list : array($list);
  427. $form = array();
  428. foreach ($list as $name) {
  429. if ($variable = variable_get_info($name, $options)) {
  430. $form[$name] = variable_form_element($variable, $options);
  431. }
  432. }
  433. return $form;
  434. }
  435. /**
  436. * Include extended API and files related to specific variable
  437. *
  438. * @param $variable
  439. * Variable array
  440. */
  441. function variable_include($variable = NULL) {
  442. static $included;
  443. if (!isset($included)) {
  444. // As a first step, include main variable API
  445. module_load_include('inc', 'variable');
  446. $included = array();
  447. }
  448. if ($variable && !isset($included[$variable['name']])) {
  449. // Include module.variable.inc files related to the variable and the variable type.
  450. variable_module_include($variable['module']);
  451. variable_type_include($variable['type']);
  452. $included[$variable['name']] = TRUE;
  453. }
  454. }
  455. /**
  456. * Include variable type files
  457. */
  458. function variable_type_include($type) {
  459. variable_include();
  460. $info = variable_get_type($type);
  461. variable_module_include($info['module']);
  462. // Include subtype if any
  463. if (!empty($info['type'])) {
  464. variable_type_include($info['type']);
  465. }
  466. }
  467. /**
  468. * Form for module variables
  469. */
  470. function variable_module_form($form, $form_state, $module) {
  471. variable_include();
  472. // Pass on the values on the form for further reference.
  473. $form['#variable_module_form'] = $module;
  474. return variable_edit_form($form, $form_state, array_keys(variable_list_module($module)));
  475. }
  476. /**
  477. * Form for group variables
  478. */
  479. function variable_group_form($form, $form_state, $group) {
  480. variable_include();
  481. // Pass on the values on the form for further reference.
  482. $form['#variable_group_form'] = $group;
  483. return variable_edit_form($form, $form_state, array_keys(variable_list_group($group)));
  484. }
  485. /**
  486. * Implements hook_modules_disabled().
  487. */
  488. function variable_modules_disabled($modules) {
  489. variable_include();
  490. array_map('variable_module_disable', $modules);
  491. variable_cache_clear();
  492. }
  493. /**
  494. * Implements hook_modules_enabled().
  495. */
  496. function variable_modules_enabled($modules) {
  497. variable_include();
  498. array_map('variable_module_enable', $modules);
  499. variable_cache_clear();
  500. }
  501. /**
  502. * Implements hook_modules_uninstalled().
  503. */
  504. function variable_modules_uninstalled($modules) {
  505. variable_include();
  506. array_map('variable_module_uninstall', $modules);
  507. variable_cache_clear();
  508. }
  509. /**
  510. * Implements hook_theme()
  511. */
  512. function variable_theme($existing, $type, $theme, $path) {
  513. return array(
  514. 'variable_table_select' => array(
  515. 'render element' => 'element',
  516. 'file' => 'variable.form.inc',
  517. ),
  518. );
  519. }
  520. /**
  521. * Get variable info static data, try the cache, or invoke the hook to collect it.
  522. *
  523. * @param $type
  524. * Name of the info to collect
  525. * - 'variable', Variable information, hook_variable_info()
  526. * - 'group', Group information, hook_variable_group_info()
  527. * - 'type', Type information, hook_variable_type_info()
  528. * @param $options
  529. * Options to retrieve or build the data.
  530. * The only used option to collect the data is 'langcode', though a full array of options may be used for the hooks
  531. */
  532. function &variable_static($type, $options = array()) {
  533. static $data;
  534. $name = 'variable_' . $type;
  535. $langcode = isset($options['langcode']) ? $options['langcode'] : 'default';
  536. if (!isset($data[$type])) {
  537. $data[$type] = &drupal_static($name);
  538. }
  539. if (!isset($data[$type][$langcode])) {
  540. $cache_id = $type . ':' . $langcode;
  541. if ($cache = cache_get($cache_id, 'cache_variable')) {
  542. $data[$type][$langcode] = $cache->data;
  543. }
  544. else {
  545. variable_include();
  546. $data[$type][$langcode] = variable_build_info($type, $options);
  547. cache_set($cache_id, $data[$type][$langcode], 'cache_variable');
  548. }
  549. }
  550. // Return a reference inside the big array
  551. return $data[$type][$langcode];
  552. }
  553. /**
  554. * Get data from a variable module info array.
  555. */
  556. function _variable_info($type, $name = NULL, $options = array()) {
  557. $info = &variable_static($type, $options);
  558. if ($name) {
  559. return isset($info[$name]) ? $info[$name] : array();
  560. }
  561. else {
  562. return $info;
  563. }
  564. }
  565. /**
  566. * Get global language object.
  567. *
  568. * Initialize the language system if needed as we absolutely need a language here
  569. */
  570. function _variable_language() {
  571. global $language;
  572. if (!isset($language)) {
  573. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  574. }
  575. return $language;
  576. }
  577. /**
  578. * Normalize variable options
  579. *
  580. * Will fill the following values if not present in the parameters
  581. * - langcode, Language code
  582. * - language, Language object
  583. */
  584. function _variable_options($options = array()) {
  585. if (!empty($options['language'])) {
  586. $options['langcode'] = $options['language']->language;
  587. }
  588. elseif (!empty($options['langcode']) && ($list = language_list()) && isset($list[$options['langcode']])) {
  589. $options['language'] = $list[$options['langcode']];
  590. }
  591. else {
  592. $language = _variable_language();
  593. $options['language'] = $language;
  594. $options['langcode'] = $language->language;
  595. }
  596. return $options;
  597. }
  598. /**
  599. * Normalize variable data
  600. *
  601. * @param $variable
  602. * Variable name or variable info array
  603. * @return array
  604. * Variable information
  605. */
  606. function _variable_variable($variable, $options = array()) {
  607. if (is_array($variable)) {
  608. return $variable;
  609. }
  610. elseif ($info = variable_get_info($variable, $options)) {
  611. return $info;
  612. }
  613. else {
  614. // We don't have meta-information about this variable.
  615. return _variable_unknown($variable);
  616. }
  617. }
  618. /**
  619. * Unknown variable
  620. */
  621. function _variable_unknown($name) {
  622. return array(
  623. 'name' => $name,
  624. 'title' => t('Unknown variable @name', array('@name' => $name)),
  625. 'type' => 'unknown',
  626. 'group' => 'default',
  627. 'module' => 'variable',
  628. // Space for public service advertisement :-)
  629. 'description' => t('We have no meta information for this variable. Please, ask module developers to declare their variables. See <a href="http://drupal.org/project/variable">Variable module</a>.'),
  630. );
  631. }
  632. /**
  633. * Implements hook_form_alter().
  634. *
  635. * Triggers hook_variable_realm_settings_form_alter() giving other modules a chance
  636. * to act on settings forms after other contrib modules have added their variables.
  637. */
  638. function variable_form_alter(&$form, &$form_state, $form_id) {
  639. if (isset($form['#submit']) && is_array($form['#submit']) && in_array('system_settings_form_submit', $form['#submit'])) {
  640. // Replace submit callback and use our own function.
  641. $form['#submit'] = str_replace('system_settings_form_submit', 'variable_settings_form_submit', $form['#submit']);
  642. $alter = TRUE;
  643. }
  644. elseif (isset($form['#variable_edit_form'])) {
  645. // This is a variable form, just invoke the hook but don't change submit callback.
  646. $alter = TRUE;
  647. }
  648. if (!empty($alter)) {
  649. foreach (module_implements('variable_settings_form_alter') as $module) {
  650. $function = $module . '_variable_settings_form_alter';
  651. $function($form, $form_state, $form_id);
  652. }
  653. }
  654. }
  655. /**
  656. * Implement validate callback.
  657. *
  658. * This needs to be in the module as it may be needed by form ajax callbacks.
  659. */
  660. function variable_form_element_validate($element, &$form_state, $form) {
  661. $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
  662. $variable = $element['#variable'];
  663. variable_include($variable);
  664. $variable['value'] = isset($element['#value']) ? $element['#value'] : NULL;
  665. $error = $variable['validate callback']($variable, $options, $element, $form, $form_state);
  666. if ($error) {
  667. form_error($element, $error);
  668. }
  669. }
  670. /**
  671. * Implements hook_module_implements_alter().
  672. *
  673. * Move variable_form_alter() to the end of the list to give modules manipulating
  674. * variables/realms a chance to act on settings forms.
  675. *
  676. * @param $implementations
  677. * All implementations of the given hook.
  678. * @param $hook
  679. * Name of the hook.
  680. */
  681. function variable_module_implements_alter(&$implementations, $hook) {
  682. if ($hook == 'form_alter') {
  683. $group = $implementations['variable'];
  684. unset($implementations['variable']);
  685. $implementations['variable'] = $group;
  686. }
  687. }
  688. /**
  689. * Group variable list by variable info property.
  690. *
  691. * @param $list
  692. * Array of variable names or full built variables.
  693. * @param $field
  694. * Field to group by. If empty, they will be added to the '<none>' array.
  695. *
  696. * @result
  697. * Array of arrays indexed by that field value.
  698. */
  699. function variable_group_variables($list, $field = 'group') {
  700. $groups = array();
  701. foreach ($list as $variable) {
  702. $build = _variable_variable($variable);
  703. $value = isset($build[$field]) ? $build[$field] : '<none>';
  704. $groups[$value][$build['name']] = $variable;
  705. }
  706. return $groups;
  707. }
  708. /**
  709. * Add default buttons to a form and set its prefix.
  710. *
  711. * @param $form
  712. * An associative array containing the structure of the form.
  713. * @param $options
  714. * Aditional options to pass on, like variable realm.
  715. *
  716. * @return
  717. * The form structure.
  718. *
  719. * @see system_settings_form_submit()
  720. * @ingroup forms
  721. */
  722. function variable_settings_form($form, $options = array()) {
  723. $form['#variable_options'] = $options;
  724. $form['actions']['#type'] = 'actions';
  725. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  726. if (!empty($_POST) && form_get_errors()) {
  727. drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  728. }
  729. $form['#submit'][] = 'variable_settings_form_submit';
  730. // By default, render the form using theme_system_settings_form().
  731. if (!isset($form['#theme'])) {
  732. $form['#theme'] = 'system_settings_form';
  733. }
  734. return $form;
  735. }
  736. /**
  737. * Execute the system_settings_form.
  738. *
  739. * If you want node type configure style handling of your checkboxes,
  740. * add an array_filter value to your form.
  741. */
  742. function variable_settings_form_submit($form, &$form_state) {
  743. // Exclude unnecessary elements.
  744. form_state_values_clean($form_state);
  745. // This may contain some realm options.
  746. $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
  747. // Now run regular settings submission but using variable_set_value()
  748. foreach ($form_state['values'] as $key => $value) {
  749. if (is_array($value) && isset($form_state['values']['array_filter'])) {
  750. $value = array_keys(array_filter($value));
  751. }
  752. // Using this function will invoke variable_update hook.
  753. variable_set_value($key, $value, $options);
  754. }
  755. drupal_set_message(t('The configuration options have been saved.'));
  756. }
  757. /**
  758. * Invoke hook on all modules, adding 'module' property
  759. */
  760. function variable_invoke_all() {
  761. $args = func_get_args();
  762. $hook = $args[0];
  763. unset($args[0]);
  764. $return = array();
  765. foreach (module_implements($hook) as $module) {
  766. $function = $module . '_' . $hook;
  767. if (function_exists($function)) {
  768. $result = call_user_func_array($function, $args);
  769. if (isset($result) && is_array($result)) {
  770. foreach ($result as $key => $value) {
  771. $return[$key] = $value + array('module' => $module);
  772. }
  773. }
  774. }
  775. }
  776. return $return;
  777. }