strongarm.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/strongarm/strongarm.module

Functions & methods

NameDescription
strongarm_features_pipe_node_alterImplementation of hook_features_pipe_component_alter() for node component. Add node type variables on behalf of core modules.
strongarm_flush_cachesImplementation of hook_flush_caches().
strongarm_form_system_module_alterImplementation of hook_form_alter() for system_module form. Clear strongarm & variable caches on modules page.
strongarm_helpImplementation hook_help().
strongarm_menuImplementation of hook_menu().
strongarm_schema_alterImplementation of hook_schema_alter(). Makes the variables table usable by ctools' export.inc.
strongarm_themeImplementation of hook_theme().
strongarm_vars_loadLoad all variables (DB and Strongarmed).
variable_features_exportImplements hook_features_export().
variable_features_export_renderImplements hook_features_export_render().
variable_features_rebuildImplementation of hook_features_rebuild(). Same as revert, but we only want to force variables only in code into the database

File

View source
  1. <?php
  2. /**
  3. * Implementation of hook_menu().
  4. */
  5. function strongarm_menu() {
  6. $items = array();
  7. $items['admin/settings/strongarm'] = array(
  8. 'title' => 'Strongarm',
  9. 'description' => 'Manage Drupal variable settings that have been strongarmed.',
  10. 'page callback' => 'drupal_get_form',
  11. 'page arguments' => array('strongarm_admin_form'),
  12. 'access callback' => 'user_access',
  13. 'access arguments' => array('administer site configuration'),
  14. 'file' => 'strongarm.admin.inc',
  15. 'type' => MENU_NORMAL_ITEM,
  16. );
  17. return $items;
  18. }
  19. /**
  20. * Implementation of hook_form_alter() for system_module form.
  21. * Clear strongarm & variable caches on modules page.
  22. */
  23. function strongarm_form_system_module_alter(&$form, &$form_state) {
  24. strongarm_flush_caches();
  25. }
  26. /**
  27. * Implementation of hook_theme().
  28. */
  29. function strongarm_theme() {
  30. return array(
  31. 'strongarm_admin_form' => array(
  32. 'arguments' => array(),
  33. 'file' => 'strongarm.admin.inc',
  34. 'path' => drupal_get_path('module', 'strongarm'),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implementation of hook_flush_caches().
  40. */
  41. function strongarm_flush_caches() {
  42. cache_clear_all('variables', 'cache');
  43. cache_clear_all('strongarm', 'cache');
  44. }
  45. /**
  46. * Implementation of hook_schema_alter().
  47. * Makes the variables table usable by ctools' export.inc.
  48. */
  49. function strongarm_schema_alter(&$schema) {
  50. $schema['variable']['export'] = array(
  51. 'key' => 'name',
  52. 'identifier' => 'strongarm',
  53. 'default hook' => 'strongarm',
  54. 'api' => array(
  55. 'owner' => 'strongarm',
  56. 'api' => 'strongarm',
  57. 'minimum_version' => 1,
  58. 'current_version' => 1,
  59. ),
  60. );
  61. $schema['variable']['fields']['value']['serialize'] = TRUE;
  62. }
  63. /**
  64. * Implementation hook_help().
  65. */
  66. function strongarm_help($path, $arg) {
  67. switch ($path) {
  68. case 'admin/help#strongarm':
  69. $output = file_get_contents(drupal_get_path('module', 'strongarm') .'/README.txt');
  70. return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>'. check_plain($output) .'</pre>';
  71. case 'admin/settings/strongarm':
  72. return '<p>'. t("Strongarm lets site builders manage default variable settings. All the default values provided by Strongarm are listed on this page. Any overridden value can be reverted to its default by selecting its checkbox and clicking 'Reset to defaults'.") .'</p>';
  73. }
  74. }
  75. /**
  76. * Load all variables (DB and Strongarmed).
  77. */
  78. function strongarm_vars_load($sorted = TRUE, $reset = FALSE) {
  79. ctools_include('export');
  80. static $vars;
  81. // Ensure that the schema cache is not stale when trying to load.
  82. $schema = drupal_get_schema('variable');
  83. if (!isset($schema['export']) || $reset) {
  84. ctools_export_load_object_reset('variable');
  85. drupal_get_schema('variable', TRUE);
  86. }
  87. // Load vars.
  88. if (!isset($vars) || $reset) {
  89. $vars = ctools_export_load_object('variable');
  90. if ($sorted) {
  91. ksort($vars);
  92. }
  93. }
  94. return $vars;
  95. }
  96. /**
  97. * Implementation of hook_features_revert().
  98. */
  99. if (!function_exists('variable_features_revert')) {
  100. function variable_features_revert($module) {
  101. $defaults = features_get_default('variable', $module);
  102. if (empty($defaults)) {
  103. return;
  104. }
  105. $vars = strongarm_vars_load(TRUE, TRUE);
  106. foreach ($defaults as $name => $default) {
  107. if (!empty($vars[$name]->in_code_only) || ($default->value !== $vars[$name]->value)) {
  108. variable_set($name, $default->value);
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Implementation of hook_features_rebuild().
  115. * Same as revert, but we only want to force variables only in code into the database
  116. */
  117. function variable_features_rebuild($module) {
  118. $defaults = features_get_default('variable', $module);
  119. if (empty($defaults)) {
  120. return;
  121. }
  122. $vars = strongarm_vars_load(TRUE, TRUE);
  123. foreach ($defaults as $name => $default) {
  124. if (isset($vars[$name]->in_code_only) || (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install' && $vars[$name]->export_type & EXPORT_IN_CODE)) {
  125. variable_set($name, $default->value);
  126. }
  127. }
  128. }
  129. /**
  130. * Implements hook_features_export().
  131. *
  132. * This is implemented to remove variables that are in code, but not DB.
  133. * This is a result of Strongarm vars now living in the DB, so unlike other
  134. * ctools components, an update of this Feature with a variable in code but
  135. * not the database, should remove the variable form the Feature.
  136. */
  137. function variable_features_export($data, &$export, $module_name) {
  138. // First delegate to the Features ctools export
  139. $pipe = ctools_component_features_export('variable', $data, $export, $module_name);
  140. // Then remove any vars from the export that are only in code
  141. $vars = strongarm_vars_load(TRUE, TRUE);
  142. foreach ($data as $object_name) {
  143. if(!isset($vars[$object_name]) || !empty($vars[$object_name]->in_code_only)) {
  144. unset($export['features']['variable'][$object_name]);
  145. }
  146. }
  147. return $pipe;
  148. }
  149. /**
  150. * Implements hook_features_export_render().
  151. *
  152. * Loads default values from the DB since it is now the system of record.
  153. */
  154. function variable_features_export_render($module, $data) {
  155. ctools_include('export');
  156. $schema = ctools_export_get_schema('variable');
  157. $code = ' $export = array();'."\n\n";
  158. $identifier = $schema['export']['identifier'];
  159. $result = db_query("SELECT * FROM {variable} WHERE name IN (" . db_placeholders($data, 'text') . ") ORDER BY name", $data);
  160. while ($object = db_fetch_object($result)) {
  161. $object = _ctools_export_unpack_object($schema, $object);
  162. $code .= _ctools_features_export_crud_export('variable', $object, ' ');
  163. $code .= " \$export[" . ctools_var_export($object->name) . "] = \${$identifier};\n\n";
  164. }
  165. $code .= ' return $export;';
  166. return array($schema['export']['default hook'] => $code);
  167. }
  168. /**
  169. * Implementation of hook_features_pipe_component_alter() for node component.
  170. * Add node type variables on behalf of core modules.
  171. */
  172. function strongarm_features_pipe_node_alter(&$pipe, $data, $export) {
  173. if (!empty($data)) {
  174. $variables = array(
  175. 'comment',
  176. 'comment_anonymous',
  177. 'comment_controls',
  178. 'comment_default_mode',
  179. 'comment_default_order',
  180. 'comment_default_per_page',
  181. 'comment_form_location',
  182. 'comment_preview',
  183. 'comment_subject_field',
  184. 'language_content_type',
  185. 'node_options',
  186. 'upload',
  187. );
  188. foreach ($data as $node_type) {
  189. foreach ($variables as $variable_name) {
  190. $pipe['variable'][] = "{$variable_name}_{$node_type}";
  191. }
  192. }
  193. }
  194. }