Commit aae2bad6 authored by Jeff Miccolis's avatar Jeff Miccolis
Browse files

Initial commit of strongarm module, which lets modules enforce variable settings.

parents
/* $Id$ */
.strongarm {
color:#366 !important;
background:#e0f8f8 !important;
border:1px solid #8cc !important;
}
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Drush integration for Strongarm. Provides commands to export variables.
*/
/**
* Implementation of hook_drush_help().
*/
function strongarm_drush_help($section) {
switch ($section) {
case 'drush:strongarm':
return dt("Export Drupal variables. Without any arguments a list of all variables will be shown. If arguments are specified they'll be var_exported to stdout.");
}
}
/**
* Implementation of hook_drush_command().
*/
function strongarm_drush_command() {
$items['strongarm'] = array(
'callback' => 'strongarm_drush_export',
'description' => 'Export variable(s).',
);
return $items;
}
// Callback for strongarm command.
function strongarm_drush_export() {
$vars = func_get_args();
if (empty($vars)) {
$result = db_query('SELECT name FROM variable');
$rows = array(array('Variables'));
while ($row = db_fetch_array($result)) {
$rows[] = array_values($row);
}
drush_print_table($rows, 4, TRUE);
}
else {
$exports = array();
foreach ($vars as $v) {
// We use the very unlikely 'DRUSH_STRONGARM_NO_VALUE_HERE' as the
// default value which *should* be ok, right?
$variable = variable_get($v, 'DRUSH_STRONGARM_NO_VALUE_HERE');
if ($variable !== 'DRUSH_STRONGARM_NO_VALUE_HERE') {
$exports[$v] = $variable;
}
}
drush_print(var_export($exports));
}
}
\ No newline at end of file
; $Id$
name = "Strongarm"
description = "Enforces variable values defined by modules that need settings set to operate properly."
core = "6.x"
<?php
// $Id$
define('STRONGARM_PATTERN_PREFIX', 0);
define('STRONGARM_PATTERN_SUFFIX', 1);
/**
* Implementation of hook_boot().
* This is a very aggressive way of ensuring that these variables are
* set. Necessary for variables that are checked by modules on hook_init().
*/
function strongarm_boot() {
global $conf;
$var_conf = strongarm_get_cache();
if (!empty($var_conf)) {
foreach ($var_conf as $k => $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 ($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() {
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;
}
if (!isset($form[$elem]['#attributes'])) {
$form[$elem]['#attributes'] = array();
}
$form[$elem]['#attributes']['class'] .= " strongarm";
$altered = TRUE;
}
// If this is a tree'd element, recurse
if ($form[$elem]['#tree'] || !empty($children)) {
$altered = $altered || _strongarm_lockdown($form[$elem], $pattern, $position);
}
}
return $altered;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment