Permalink
Browse files

adds strongarm module

  • Loading branch information...
1 parent 88409ed commit caf3d60470183d531392e1392b326b2b7fb49e62 @emjayess committed Mar 8, 2013
Oops, something went wrong.
@@ -0,0 +1,59 @@
+
+Strongarm 2.x for Drupal 7.x
+----------------------------
+Strongarm gives site builders a way to override the default variable values that
+Drupal core and contributed modules ship with. It is not an end user tool, but a
+developer and site builder tool which provides an API and a limited UI.
+
+An example of such a variable is `site_frontpage`. In Drupal this defaults to
+`node`, which ensures that the front page gets content as soon as some exists,
+but for many Drupal sites this setting is simply wrong. Strongarm gives the site
+builder a place in the equation - an opportunity to set the default value of
+`site_frontpage` to something that makes sense for their site.
+
+
+Installation
+------------
+Strongarm can be installed like any other Drupal module -- place it in
+the modules directory for your site and enable it (and its requirement,
+CTools) on the `admin/build/modules` page.
+
+Strongarm is an API module. It does absolutely nothing for the end user out of
+the box without other modules that take advantage of its API.
+
+
+How Strongarm works
+-------------------
+Strongarm uses the CTools export API to make entries in the system module's
+`variable` table exportables. Exportables are Drupal configuration objects that
+lead a dual life -- they may be *defaults* set by code exports in modules, they
+may be *overridden* if a user chooses to change the value in the database, or
+they may be *normal* if the configuration object lives only in the database but
+not in code. To learn more about exportables and the CTools export API, see the
+CTools advanced help on "Exportable objects tool."
+
+
+Exporting variables
+-------------------
+If you are a developer or site builder Strongarm gives you tools to export the
+settings of variables in your site database and manage any overrides to default
+values.
+
+To export variable values, you will need to enable either the [Features][1]
+module or the [Bulk Export][2] module provided by CTools (as of June 29, 2010
+you must use a recent checkout CTools `DRUPAL-6--1` in order to use Bulk Export
+with Strongarm). Features provides a UI for adding variable exports to a
+feature. Bulk Export provides a UI for generating defaults hooks with exported
+variables that you can add to your own modules. You do not need to enable both
+modules.
+
+
+Maintainers
+-----------
+
+- jmiccolis (Jeff Miccolis)
+- yhahn (Young Hahn)
+
+
+[1]: http://drupal.org/project/features
+[2]: http://drupal.org/project/ctools
@@ -0,0 +1,142 @@
+<?php
+
+/**
+ * Variable management strongarm form.
+ */
+function strongarm_admin_form($form_state) {
+ global $conf;
+ $vars = strongarm_vars_load(TRUE, TRUE);
+ $form = array('#theme' => 'strongarm_admin_form',);
+ foreach ($vars as $name => $variable) {
+ if ($variable->export_type & EXPORT_IN_CODE) {
+ $default = ctools_get_default_object('variable', $name);
+
+ // If variable value does not match global $conf, this value has been
+ // hardcoded (e.g. in settings.php) and has been allowed to pass
+ // through. It cannot be reverted.
+ $hardcoded = FALSE;
+ $restorable = FALSE;
+ if (isset($conf[$name]) && $conf[$name] !== $variable->value) {
+ $storage = t('Hardcoded');
+ $hardcoded = TRUE;
+ }
+ elseif (!empty($variable->in_code_only)) {
+ $storage = t('In code');
+ $restorable = TRUE;
+ }
+ elseif ($variable->value != $default->value) {
+ $storage = t('Overridden');
+ $restorable = TRUE;
+ }
+ else {
+ $storage = t('Saved to DB');
+ }
+
+ $value = $hardcoded ? $conf[$name] : $variable->value;
+
+ // If the variable is in the database and differs from its code value,
+ // allow administrator to revert its value.
+ if ($restorable) {
+ $form['revert']['#tree'] = TRUE;
+ $form['revert'][$name]['revert'] = array('#type' => 'checkbox');
+ $form['revert'][$name]['value'] = array('#type' => 'value', '#value' => $default->value);
+ }
+
+ if (module_exists('variable') && ($info = variable_get_info($name))) {
+ $form['name'][$name] = array('#markup' => $info['title'] . '<br/>' . $name);
+ $form['storage'][$name] = array('#markup' => $storage);
+ $info['value'] = $variable->value;
+ $form['value'][$name] = array('#markup' => variable_format_value($info));
+ }
+ else {
+ $form['name'][$name] = array('#markup' => $name);
+ $form['storage'][$name] = array('#markup' => $storage);
+ $form['value'][$name] = array('#markup' => check_plain(_strongarm_readable($value)));
+ }
+ }
+ }
+ if (!empty($form['revert'])) {
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Restore values to DB'),
+ '#submit' => array('strongarm_admin_revert_submit'),
+ );
+ }
+ return $form;
+}
+
+/**
+ * Revert form submit handler.
+ */
+function strongarm_admin_revert_submit(&$form, &$form_state) {
+ if (!empty($form_state['values']['revert'])) {
+ foreach ($form_state['values']['revert'] as $name => $revert) {
+ if ($revert['revert']) {
+ variable_set($name, $revert['value']);
+ }
+ }
+ strongarm_flush_caches();
+ }
+}
+
+/**
+ * Display variables in a nicer way.
+ */
+function _strongarm_readable($var) {
+ if (is_string($var) || is_numeric($var)) {
+ return truncate_utf8($var, 30, TRUE, TRUE);
+ }
+ else if (is_bool($var)) {
+ return $var ? 'TRUE' : 'FALSE';
+ }
+ else if (is_array($var)) {
+ $test = $detected = array();
+ $test['keys'] = array_keys($var);
+ $test['values'] = array_values($var);
+
+ foreach ($test as $type => $values) {
+ $numeric = TRUE;
+ $sequential = 0;
+ $boolean = TRUE;
+ foreach ($values as $v) {
+ $numeric = is_numeric($v) && $numeric;
+ $sequential = is_numeric($v) && ($sequential == $v) && $sequential !== FALSE ? $sequential + 1 : FALSE;
+ $boolean = $boolean && ($v === 0 || $v === 1 || $v === '1' || $v === '0' || $v === TRUE || $v === FALSE);
+ }
+ $detected[$type]['numeric'] = $numeric;
+ $detected[$type]['sequential'] = $sequential !== FALSE;
+ $detected[$type]['boolean'] = $boolean;
+ }
+
+ // List of things
+ if (!empty($var) && $detected['keys']['numeric'] && $detected['keys']['sequential']) {
+ return truncate_utf8(implode(', ', $var), 30, TRUE, TRUE);
+ }
+ return '-';
+ }
+}
+
+/**
+ * Theme function for the strongarm admin form.
+ */
+function theme_strongarm_admin_form(&$vars) {
+ $form = $vars['form'];
+
+ drupal_add_js('misc/tableselect.js');
+ $rows = $headers = array();
+ $headers[] = array('class' => array('select-all'));
+ $headers[] = t('Variable');
+ $headers[] = t('Storage');
+ $headers[] = t('Value');
+ foreach (element_children($form['name']) as $name) {
+ $row = array();
+ $row[] = isset($form['revert'][$name]) ? drupal_render($form['revert'][$name]) : '';
+ $row[] = drupal_render($form['name'][$name]);
+ $row[] = drupal_render($form['storage'][$name]);
+ $row[] = drupal_render($form['value'][$name]);
+ $rows[] = $row;
+ }
+ $output = theme('table', array('header' => $headers, 'rows' => $rows));
+ $output .= drupal_render_children($form);
+ return $output;
+}
@@ -0,0 +1,5 @@
+.strongarm {
+ color:#366 !important;
+ background:#e0f8f8 !important;
+ border:1px solid #8cc !important;
+ }
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Implementation of hook_drush_command().
+ */
+function strongarm_drush_command() {
+ $items = array();
+
+ $items['strongarm-revert'] = array(
+ 'description' => 'Revert all strongarmed variables from code to the database.',
+ 'options' => array(
+ 'force' => 'Reset all variables, including those that are marked as already being set to the database.',
+ ),
+ 'bootstrap' => 'DRUSH_BOOTSTRAP_DRUPAL_FULL',
+ );
+
+ return $items;
+}
+
+/**
+ * Command callback for strongarm_revert.
+ */
+function drush_strongarm_revert() {
+ _drush_strongarm_revert(drush_get_option('force', FALSE));
+ drush_log('Pushed variables from code to the database.', 'success');
+}
+
+/**
+ * Handle the revert of variables into the database.
+ */
+function _drush_strongarm_revert($force) {
+ global $conf;
+
+ $vars = strongarm_vars_load(TRUE, TRUE);
+ foreach ($vars as $name => $var) {
+ if ($force || !empty($var->in_code_only)) {
+ if (!isset($conf[$name]) || $var->value != $conf[$name]) {
+ variable_set($name, $var->value);
+ }
+ }
+ }
+}
@@ -0,0 +1,15 @@
+name = Strongarm
+description = Enforces variable values defined by modules that need settings set to operate properly.
+core = 7.x
+dependencies[] = ctools
+
+files[] = strongarm.admin.inc
+files[] = strongarm.install
+files[] = strongarm.module
+
+; Information added by drupal.org packaging script on 2012-06-13
+version = "7.x-2.0"
+core = "7.x"
+project = "strongarm"
+datestamp = "1339604214"
+
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * @file
+ * Install, update and uninstall functions for the strongarm module.
+ */
+
+/**
+ * Implements hook_enable().
+ */
+function strongarm_enable() {
+ // Weight strongarm exceptionally light.
+ db_update('system')
+ ->fields(array('weight' => -1000))
+ ->condition('name', 'strongarm')
+ ->condition('type', 'module')
+ ->execute();
+}
+
+/**
+ * Transition Strongarm variables from code to the database.
+ */
+function strongarm_update_7201() {
+ module_load_include('module', 'strongarm');
+ $variables = strongarm_vars_load(TRUE, TRUE);
+ if (!empty($variables)) {
+ foreach ($variables as $var_name => $var) {
+ $exists = db_query("SELECT name FROM {variable} WHERE name = :name", array(':name' => $var_name))->fetchField();
+ if (!$exists) {
+ variable_set($var_name, $var->value);
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit caf3d60

Please sign in to comment.