variable.module 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. */
  391. function variable_edit_form($form, $form_state, $list, $options = array()) {
  392. // Pass on the values on the form for further reference.
  393. $form['#variable_edit_form'] = $list;
  394. module_load_include('form.inc', 'variable');
  395. $form += variable_edit_subform($list, $options);
  396. return variable_settings_form($form, $options);
  397. }
  398. /**
  399. * Form elements for variable list.
  400. *
  401. * @param $list
  402. * Variable name or array of variable names..
  403. * @param $options
  404. * Regular variable options (language, realm, etc) and optional 'form parents' array.
  405. */
  406. function variable_edit_subform($list, $options = array()) {
  407. module_load_include('form.inc', 'variable');
  408. $list = is_array($list) ? $list : array($list);
  409. $form = array();
  410. foreach ($list as $name) {
  411. if ($variable = variable_get_info($name, $options)) {
  412. $form[$name] = variable_form_element($variable, $options);
  413. }
  414. }
  415. return $form;
  416. }
  417. /**
  418. * Include extended API and files related to specific variable
  419. *
  420. * @param $variable
  421. * Variable array
  422. */
  423. function variable_include($variable = NULL) {
  424. static $included;
  425. if (!isset($included)) {
  426. // As a first step, include main variable API
  427. module_load_include('inc', 'variable');
  428. $included = array();
  429. }
  430. if ($variable && !isset($included[$variable['name']])) {
  431. // Include module.variable.inc files related to the variable and the variable type.
  432. variable_module_include($variable['module']);
  433. variable_type_include($variable['type']);
  434. $included[$variable['name']] = TRUE;
  435. }
  436. }
  437. /**
  438. * Include variable type files
  439. */
  440. function variable_type_include($type) {
  441. variable_include();
  442. $info = variable_get_type($type);
  443. variable_module_include($info['module']);
  444. // Include subtype if any
  445. if (!empty($info['type'])) {
  446. variable_type_include($info['type']);
  447. }
  448. }
  449. /**
  450. * Form for module variables
  451. */
  452. function variable_module_form($form, $form_state, $module) {
  453. variable_include();
  454. // Pass on the values on the form for further reference.
  455. $form['#variable_module_form'] = $module;
  456. return variable_edit_form($form, $form_state, array_keys(variable_list_module($module)));
  457. }
  458. /**
  459. * Form for group variables
  460. */
  461. function variable_group_form($form, $form_state, $group) {
  462. variable_include();
  463. // Pass on the values on the form for further reference.
  464. $form['#variable_group_form'] = $group;
  465. return variable_edit_form($form, $form_state, array_keys(variable_list_group($group)));
  466. }
  467. /**
  468. * Implements hook_modules_disabled().
  469. */
  470. function variable_modules_disabled($modules) {
  471. variable_include();
  472. array_map('variable_module_disable', $modules);
  473. variable_cache_clear();
  474. }
  475. /**
  476. * Implements hook_modules_enabled().
  477. */
  478. function variable_modules_enabled($modules) {
  479. variable_include();
  480. array_map('variable_module_enable', $modules);
  481. variable_cache_clear();
  482. }
  483. /**
  484. * Implements hook_modules_uninstalled().
  485. */
  486. function variable_modules_uninstalled($modules) {
  487. variable_include();
  488. array_map('variable_module_uninstall', $modules);
  489. variable_cache_clear();
  490. }
  491. /**
  492. * Implements hook_theme()
  493. */
  494. function variable_theme($existing, $type, $theme, $path) {
  495. return array(
  496. 'variable_table_select' => array(
  497. 'render element' => 'element',
  498. 'file' => 'variable.form.inc',
  499. ),
  500. );
  501. }
  502. /**
  503. * Get variable info static data, try the cache, or invoke the hook to collect it.
  504. *
  505. * @param $type
  506. * Name of the info to collect
  507. * - 'variable', Variable information, hook_variable_info()
  508. * - 'group', Group information, hook_variable_group_info()
  509. * - 'type', Type information, hook_variable_type_info()
  510. * @param $options
  511. * Options to retrieve or build the data.
  512. * The only used option to collect the data is 'langcode', though a full array of options may be used for the hooks
  513. */
  514. function &variable_static($type, $options = array()) {
  515. static $data;
  516. $name = 'variable_' . $type;
  517. $langcode = isset($options['langcode']) ? $options['langcode'] : 'default';
  518. if (!isset($data[$type])) {
  519. $data[$type] = &drupal_static($name);
  520. }
  521. if (!isset($data[$type][$langcode])) {
  522. $cache_id = $type . ':' . $langcode;
  523. if ($cache = cache_get($cache_id, 'cache_variable')) {
  524. $data[$type][$langcode] = $cache->data;
  525. }
  526. else {
  527. variable_include();
  528. $data[$type][$langcode] = variable_build_info($type, $options);
  529. cache_set($cache_id, $data[$type][$langcode], 'cache_variable');
  530. }
  531. }
  532. // Return a reference inside the big array
  533. return $data[$type][$langcode];
  534. }
  535. /**
  536. * Get data from a variable module info array.
  537. */
  538. function _variable_info($type, $name = NULL, $options = array()) {
  539. $info = &variable_static($type, $options);
  540. if ($name) {
  541. return isset($info[$name]) ? $info[$name] : array();
  542. }
  543. else {
  544. return $info;
  545. }
  546. }
  547. /**
  548. * Get global language object.
  549. *
  550. * Initialize the language system if needed as we absolutely need a language here
  551. */
  552. function _variable_language() {
  553. global $language;
  554. if (!isset($language)) {
  555. drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  556. }
  557. return $language;
  558. }
  559. /**
  560. * Normalize variable options
  561. *
  562. * Will fill the following values if not present in the parameters
  563. * - langcode, Language code
  564. * - language, Language object
  565. */
  566. function _variable_options($options = array()) {
  567. if (!empty($options['language'])) {
  568. $options['langcode'] = $options['language']->language;
  569. }
  570. elseif (!empty($options['langcode']) && ($list = language_list()) && isset($list[$options['langcode']])) {
  571. $options['language'] = $list[$options['langcode']];
  572. }
  573. else {
  574. $language = _variable_language();
  575. $options['language'] = $language;
  576. $options['langcode'] = $language->language;
  577. }
  578. return $options;
  579. }
  580. /**
  581. * Normalize variable data
  582. *
  583. * @param $variable
  584. * Variable name or variable info array
  585. * @return array
  586. * Variable information
  587. */
  588. function _variable_variable($variable, $options = array()) {
  589. if (is_array($variable)) {
  590. return $variable;
  591. }
  592. elseif ($info = variable_get_info($variable, $options)) {
  593. return $info;
  594. }
  595. else {
  596. // We don't have meta-information about this variable.
  597. return _variable_unknown($variable);
  598. }
  599. }
  600. /**
  601. * Unknown variable
  602. */
  603. function _variable_unknown($name) {
  604. return array(
  605. 'name' => $name,
  606. 'title' => t('Unknown variable @name', array('@name' => $name)),
  607. 'type' => 'unknown',
  608. 'group' => 'default',
  609. 'module' => 'variable',
  610. // Space for public service advertisement :-)
  611. '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>.'),
  612. );
  613. }
  614. /**
  615. * Implements hook_form_alter().
  616. *
  617. * Triggers hook_variable_realm_settings_form_alter() giving other modules a chance
  618. * to act on settings forms after other contrib modules have added their variables.
  619. */
  620. function variable_form_alter(&$form, &$form_state, $form_id) {
  621. if (isset($form['#submit']) && is_array($form['#submit']) && in_array('system_settings_form_submit', $form['#submit'])) {
  622. // Replace submit callback and use our own function.
  623. $form['#submit'] = str_replace('system_settings_form_submit', 'variable_settings_form_submit', $form['#submit']);
  624. $alter = TRUE;
  625. }
  626. elseif (isset($form['#variable_edit_form'])) {
  627. // This is a variable form, just invoke the hook but don't change submit callback.
  628. $alter = TRUE;
  629. }
  630. if (!empty($alter)) {
  631. foreach (module_implements('variable_settings_form_alter') as $module) {
  632. $function = $module . '_variable_settings_form_alter';
  633. $function($form, $form_state, $form_id);
  634. }
  635. }
  636. }
  637. /**
  638. * Implements hook_module_implements_alter().
  639. *
  640. * Move variable_form_alter() to the end of the list to give modules manipulating
  641. * variables/realms a chance to act on settings forms.
  642. *
  643. * @param $implementations
  644. * All implementations of the given hook.
  645. * @param $hook
  646. * Name of the hook.
  647. */
  648. function variable_module_implements_alter(&$implementations, $hook) {
  649. if ($hook == 'form_alter') {
  650. $group = $implementations['variable'];
  651. unset($implementations['variable']);
  652. $implementations['variable'] = $group;
  653. }
  654. }
  655. /**
  656. * Group variable list by variable info property.
  657. *
  658. * @param $list
  659. * Array of variable names or full built variables.
  660. * @param $field
  661. * Field to group by. If empty, they will be added to the '<none>' array.
  662. *
  663. * @result
  664. * Array of arrays indexed by that field value.
  665. */
  666. function variable_group_variables($list, $field = 'group') {
  667. $groups = array();
  668. foreach ($list as $variable) {
  669. $build = _variable_variable($variable);
  670. $value = isset($build[$field]) ? $build[$field] : '<none>';
  671. $groups[$value][$build['name']] = $variable;
  672. }
  673. return $groups;
  674. }
  675. /**
  676. * Add default buttons to a form and set its prefix.
  677. *
  678. * @param $form
  679. * An associative array containing the structure of the form.
  680. * @param $options
  681. * Aditional options to pass on, like variable realm.
  682. *
  683. * @return
  684. * The form structure.
  685. *
  686. * @see system_settings_form_submit()
  687. * @ingroup forms
  688. */
  689. function variable_settings_form($form, $options = array()) {
  690. $form['#variable_options'] = $options;
  691. $form['actions']['#type'] = 'actions';
  692. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  693. if (!empty($_POST) && form_get_errors()) {
  694. drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  695. }
  696. $form['#submit'][] = 'variable_settings_form_submit';
  697. // By default, render the form using theme_system_settings_form().
  698. if (!isset($form['#theme'])) {
  699. $form['#theme'] = 'system_settings_form';
  700. }
  701. return $form;
  702. }
  703. /**
  704. * Execute the system_settings_form.
  705. *
  706. * If you want node type configure style handling of your checkboxes,
  707. * add an array_filter value to your form.
  708. */
  709. function variable_settings_form_submit($form, &$form_state) {
  710. // Exclude unnecessary elements.
  711. form_state_values_clean($form_state);
  712. // This may contain some realm options.
  713. $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
  714. // Now run regular settings submission but using variable_set_value()
  715. foreach ($form_state['values'] as $key => $value) {
  716. if (is_array($value) && isset($form_state['values']['array_filter'])) {
  717. $value = array_keys(array_filter($value));
  718. }
  719. // Using this function will invoke variable_update hook.
  720. variable_set_value($key, $value, $options);
  721. }
  722. drupal_set_message(t('The configuration options have been saved.'));
  723. }
  724. /**
  725. * Invoke hook on all modules, adding 'module' property
  726. */
  727. function variable_invoke_all() {
  728. $args = func_get_args();
  729. $hook = $args[0];
  730. unset($args[0]);
  731. $return = array();
  732. foreach (module_implements($hook) as $module) {
  733. $function = $module . '_' . $hook;
  734. if (function_exists($function)) {
  735. $result = call_user_func_array($function, $args);
  736. if (isset($result) && is_array($result)) {
  737. foreach ($result as $key => $value) {
  738. $return[$key] = $value + array('module' => $module);
  739. }
  740. }
  741. }
  742. }
  743. return $return;
  744. }