variable_example.module 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Variable example.
  4. */
  5. /**
  6. * Implements hook_variable_realm_info()
  7. */
  8. function variable_example_variable_realm_info() {
  9. $realm['example'] = array(
  10. 'title' => t('Example'),
  11. 'weight' => 10,
  12. 'store class' => 'VariableStoreRealmStore',
  13. 'keys' => array(
  14. 'first' => t('First example'),
  15. 'second' => t('Second example'),
  16. ),
  17. );
  18. return $realm;
  19. }
  20. /**
  21. * Implements hook_menu().
  22. */
  23. function variable_example_menu() {
  24. $items['admin/config/system/variable_example'] = array(
  25. 'title' => 'Variable example',
  26. 'description' => 'Example of auto generated settings form.',
  27. 'page callback' => 'drupal_get_form',
  28. 'page arguments' => array('variable_group_form', 'variable_example'),
  29. 'access arguments' => array('administer site configuration'),
  30. );
  31. $items['variable/example'] = array(
  32. 'title' => 'Variable example',
  33. 'description' => 'List some variables.',
  34. 'page callback' => 'variable_example_page_list',
  35. 'access arguments' => array('administer site configuration'),
  36. );
  37. $items['variable/realm/%/%'] = array(
  38. 'title' => 'Variable example realm',
  39. 'description' => 'Example of variable realms.',
  40. 'page callback' => 'variable_example_page_realm',
  41. 'page arguments' => array(2, 3),
  42. 'access arguments' => array('administer site configuration'),
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Variable example realm page.
  48. *
  49. * Will switch to given realm and display variables.
  50. */
  51. function variable_example_page_list() {
  52. variable_include();
  53. $list = variable_list_group('site_information') + variable_list_group('variable_example');
  54. foreach ($list as $name => $variable) {
  55. $build[$name] = array(
  56. '#type' => 'item',
  57. '#title' => $variable['title'],
  58. '#markup' => variable_format_value($variable),
  59. );
  60. }
  61. return $build;
  62. }
  63. /**
  64. * Variable example realm page.
  65. *
  66. * Will switch to given realm and display variables.
  67. */
  68. function variable_example_page_realm($realm, $key) {
  69. // Initialize realm from variable store.
  70. $variables = variable_store($realm, $key);
  71. // Set at least one variable for the realm
  72. $variables += array('site_name' => 'Variable example realm');
  73. variable_realm_add($realm, $key, $variables);
  74. variable_realm_switch($realm, $key);
  75. return variable_example_page_list();
  76. }