$v) { $conf[$k] = $v; } } } /** * Implementation of hook_init(). */ function strongarm_init() { $var_conf = strongarm_get_cache(); if (empty($var_conf)) { strongarm_set_cache(TRUE); } } /** * Implementation of hook_flush_caches(). */ function strongarm_flush_caches() { strongarm_invalidate_cache(); } /** * Implementation of hook_form_alter(). */ function strongarm_form_alter(&$form, &$form_state, $form_id) { $pattern = ''; $position = STRONGARM_PATTERN_PREFIX; if (!empty($form['#submit']) && is_array($form['#submit']) && in_array('system_settings_form_submit', $form['#submit'])) { $lockdown = TRUE; } else if ($form_id == 'node_type_form') { $lockdown = TRUE; $pattern = $form['#node_type']->type; $position = STRONGARM_PATTERN_SUFFIX; } if (!empty($lockdown)) { drupal_add_css(drupal_get_path('module', 'strongarm') .'/strongarm.css'); $altered = _strongarm_lockdown($form, $pattern, $position); if ($altered) { drupal_set_message(t('The hilighted fields below may need to retain their current values for your site to operate properly. Settings may be disabled or reverted automatically if they are not compatible with one or more of your site\'s modules.'), 'warning'); } } } /** * Retrieve variable configuration from the cache. */ function strongarm_get_cache($reset = FALSE) { static $conf; if (!isset($conf) || $reset) { $conf = array(); $cache = cache_get('strongarm', 'cache'); if ($cache) { $conf = $cache->data; } } return $conf; } /** * Set the variable configuration cache. Optionally overrides the global * $conf array as well to populate variable values. */ function strongarm_set_cache($set_vars = FALSE) { $var_conf = array(); // We don't use module_invoke_all() here since // array_merge_recursive() can alter the desired structure of // some variables. foreach (module_implements('strongarm') as $module) { $module_conf = module_invoke($module, 'strongarm'); foreach ($module_conf as $name => $value) { if (isset($var_conf[$name])) { if (is_array($value)) { $var_conf[$name] = array_merge($var_conf[$name], $value); } else { // Blow the earlier one away $var_conf[$name] = $value; } } else { $var_conf[$name] = $value; } } } cache_set('strongarm', $var_conf); if ($set_vars) { global $conf; if (!empty($var_conf)) { foreach ($var_conf as $k => $v) { $conf[$k] = $v; } } } } /** * Invalidates the strongarm cache. */ function strongarm_invalidate_cache() { cache_clear_all('strongarm', 'cache'); } /** * Recursive function that hunts down form elements that are likely to * be related to strongarm'd variables and locks them down. */ function _strongarm_lockdown(&$form, $pattern = '', $position = STRONGARM_PATTERN_PREFIX) { $altered = FALSE; $var_conf = strongarm_get_cache(); foreach (element_children($form) as $elem) { $children = element_children($form[$elem]); $key = $elem; $match = FALSE; if (isset($var_conf[$key])) { $match = TRUE; } else if (!empty($pattern)) { $key = ($position == STRONGARM_PATTERN_PREFIX) ? "{$pattern}_{$key}" : "{$key}_{$pattern}"; if (isset($var_conf[$key])) { $match = TRUE; } } if ($match) { // If the default form value matches the strongarm value, // it is highly likely we have a match. Disable the field. if (isset($form[$elem]['#default_value']) && $var_conf[$key] == $form[$elem]['#default_value']) { $form[$elem]['#disabled'] = TRUE; $form[$elem]['#value'] = $form[$elem]['#default_value']; } if (!isset($form[$elem]['#attributes']['class'])) { $form[$elem]['#attributes']['class'] = "strongarm"; } else { $form[$elem]['#attributes']['class'] .= " strongarm"; } $altered = TRUE; } // If this is a tree'd element, recurse if (!empty($form[$elem]['#tree']) || !empty($children)) { $altered = $altered || _strongarm_lockdown($form[$elem], $pattern, $position); } } return $altered; }