variable_realm.features.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * Features support for Variable store.
  5. */
  6. /**
  7. * Implements hook_features_export_options().
  8. */
  9. function variable_realm_features_export_options() {
  10. foreach (variable_realm_info() as $name => $realm) {
  11. $controller = variable_realm_controller($name);
  12. $realm_keys = $controller->getAllKeys();
  13. if (count($realm_keys) > 1) {
  14. $options[$name] = $controller->getTitle() . ': ' . t('all');
  15. }
  16. foreach ($realm_keys as $key => $title) {
  17. $options[$name . ':' . $key] = $controller->getTitle() . ': ' . $title;
  18. }
  19. }
  20. return $options;
  21. }
  22. /**
  23. * Implements hook_features_export().
  24. */
  25. function variable_realm_features_export($data, &$export, $module_name) {
  26. $export['dependencies']['variable_realm'] = 'variable_realm';
  27. $list = variable_realm_features_selection($data);
  28. foreach ($list as $machine_name) {
  29. // Add module that provides the exported realm
  30. list($realm, $key) = explode(':', $machine_name, 2); // limit 2 because keys can contain colons
  31. if ($realm_info = variable_realm_info($realm)) {
  32. $export['dependencies'][$realm_info['module']] = $realm_info['module'];
  33. }
  34. $export['features']['variable_realm'][$machine_name] = $machine_name;
  35. }
  36. return array();
  37. }
  38. /**
  39. * Processes export data selections consistently.
  40. *
  41. * @param $data
  42. * Array of selections from the features component form.
  43. *
  44. * @return
  45. * An array of realms, keyed by machine_name.
  46. */
  47. function variable_realm_features_selection($data) {
  48. $list = array();
  49. foreach ($data as $machine_name) {
  50. if (strpos($machine_name, ':')) {
  51. $list[] = $machine_name;
  52. }
  53. else {
  54. foreach (variable_realm_keys($machine_name) as $key => $title) {
  55. $list[] = "$machine_name:$key";
  56. }
  57. }
  58. }
  59. return $list;
  60. }
  61. /**
  62. * Implements hook_features_export_render().
  63. */
  64. function variable_realm_features_export_render($module_name, $data, $export = NULL) {
  65. variable_realm_features_load($module_name, 'variable_realm_default_variables', FALSE);
  66. $code = array();
  67. $code[] = '$realm_variables = array();';
  68. foreach ($data as $machine_name) {
  69. list($realm, $key) = explode(':', $machine_name, 2); // limit 2 because keys can contain colons
  70. $variable_realm = variable_realm($realm, $key);
  71. $variables = $variable_realm->variable_list();
  72. $code[] = " \$realm_variables['{$realm}']['{$key}'] = " . features_var_export($variables) .";";
  73. }
  74. $code[] = "\nreturn \$realm_variables;";
  75. $output = implode("\n", $code);
  76. return array('variable_realm_default_variables' => $output);
  77. }
  78. /**
  79. * Implements hook_features_revert().
  80. */
  81. function variable_realm_features_revert($module) {
  82. return variable_realm_features_rebuild($module);
  83. }
  84. /**
  85. * Implements hook_features_rebuild().
  86. */
  87. function variable_realm_features_rebuild($module) {
  88. if ($defaults = variable_realm_features_load($module, 'variable_realm_default_variables', TRUE)) {
  89. foreach ($defaults as $realm => $realm_data) {
  90. foreach ($realm_data as $key => $variables) {
  91. $variable_realm = variable_realm($realm, $key);
  92. foreach ($variables as $name => $value) {
  93. $variable_realm->variable_set($name, $value);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * Features doesn't know how to load custom includes.
  101. *
  102. * @param $module
  103. * The name of the feature to load.
  104. * @param $hook
  105. * The name of the domain hook.
  106. * @param $return
  107. * Boolean indicator to return the results of the function.
  108. *
  109. * @return
  110. * The results of the $hook implemenation, if requested.
  111. */
  112. function variable_realm_features_load($module, $hook, $return = TRUE) {
  113. // Features does not handle module loading of custom files.
  114. module_load_include('inc', $module, $module . '.variable');
  115. $function = $module . '_' . $hook;
  116. if ($return && function_exists($function)) {
  117. return $function();
  118. }
  119. }