strongarm.module 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function strongarm_menu() {
  6. $items = array();
  7. $items['admin/config/development/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. * Implements 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. * Implements hook_theme().
  28. */
  29. function strongarm_theme() {
  30. return array(
  31. 'strongarm_admin_form' => array(
  32. 'render element' => 'form',
  33. 'file' => 'strongarm.admin.inc',
  34. 'path' => drupal_get_path('module', 'strongarm'),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_flush_caches().
  40. */
  41. function strongarm_flush_caches() {
  42. cache_clear_all('variables', 'cache');
  43. cache_clear_all('strongarm', 'cache');
  44. }
  45. /**
  46. * Implements 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. * Implements 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. * Implements 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 (!empty($vars[$name]->in_code_only) || (drupal_installation_attempted() && $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_select('variable', 'v')
  160. ->fields('v', array('name', 'value'))
  161. ->condition('name', $data, 'IN')
  162. ->orderBy('name')
  163. ->execute();
  164. foreach ($result as $object) {
  165. $object = _ctools_export_unpack_object($schema, $object);
  166. $code .= _ctools_features_export_crud_export('variable', $object, ' ');
  167. $code .= " \$export[" . ctools_var_export($object->name) . "] = \${$identifier};\n\n";
  168. }
  169. $code .= ' return $export;';
  170. return array($schema['export']['default hook'] => $code);
  171. }
  172. /**
  173. * Implements hook_features_pipe_alter() for node component.
  174. * Add node type variables on behalf of core modules.
  175. */
  176. function strongarm_features_pipe_node_alter(&$pipe, $data, $export) {
  177. if (!empty($data)) {
  178. $variables = array(
  179. 'comment',
  180. 'comment_anonymous',
  181. 'comment_controls',
  182. 'comment_default_mode',
  183. 'comment_default_order',
  184. 'comment_default_per_page',
  185. 'comment_form_location',
  186. 'comment_preview',
  187. 'comment_subject_field',
  188. 'field_bundle_settings_node_',
  189. 'language_content_type',
  190. 'menu_options',
  191. 'menu_parent',
  192. 'node_options',
  193. 'node_preview',
  194. 'node_submitted',
  195. );
  196. foreach ($data as $node_type) {
  197. foreach ($variables as $variable_name) {
  198. $pipe['variable'][] = "{$variable_name}_{$node_type}";
  199. }
  200. }
  201. }
  202. }