strongarm.module 5.03 KB
Newer Older
1 2 3
<?php
// $Id$

4 5 6 7
/**
 * Implementation of hook_init().
 */
function strongarm_init() {
8
  strongarm_set_conf();
9 10 11 12 13 14 15

  // This is a workaround for the very early check of the 'site_frontpage'
  // variable in the Drupal bootstrap process. The workaround re-runs
  // drupal_init_path() to ensure the strongarm'ed version of
  // 'site_frontpage' is used. Note that this may be too late if other modules
  // weighted even lower than strongarm (which is a superlightweight -1000)
  // rely on $_GET['q'] or 'site_frontpage' in hook_init().
16
  $_GET['q'] = strongarm_language_strip($_REQUEST['q']);
young hahn's avatar
young hahn committed
17
  drupal_init_path();
18 19 20 21 22
}

/**
 * Retrieve variable configuration from the cache.
 */
23 24 25 26 27 28 29 30
function strongarm_set_conf($reset = FALSE) {
  $varcache = cache_get('variables', 'cache');
  $cache = cache_get('strongarm', 'cache');
  // The > comparison here is cautious but ensures that the strongarm cache
  // actually was populated after the variable cache. It is possible with
  // >= for the var cache to be populated again during the same stale second.
  if (!$reset && ($cache && $varcache && $cache->created > $varcache->created)) {
    $var_conf = $cache->data;
31
  }
32
  else {
33
    // Ensure that the schema cache is not stale when we init.
34
    $schema = drupal_get_schema('variable');
35 36
    if (!isset($schema['export'])) {
      drupal_get_schema('variable', TRUE);
37
    }
38 39 40 41 42 43 44

    $var_conf = array();
    ctools_include('export');
    foreach (ctools_export_load_object('variable') as $var) {
      $var_conf[$var->name] = $var->value;
    }
    cache_set('strongarm', $var_conf);
45
  }
46
  global $conf;
47
  $conf = array_merge($var_conf, $conf);
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/**
 * Remove the language prefix for a given path.
 * Strongarm implements this itself as language_initialize() directly affects
 * $_GET['q'] and cannot be reused.
 */
function strongarm_language_strip($path) {
  // Configured presentation language mode.
  $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);

  // Get a list of enabled languages.
  $languages = language_list('enabled');
  $languages = $languages[1];

  if (in_array($mode, array(LANGUAGE_NEGOTIATION_PATH_DEFAULT, LANGUAGE_NEGOTIATION_PATH))) {
    $args = explode('/', $path);
    $prefix = array_shift($args);
    // Search prefix within enabled languages.
    foreach ($languages as $language) {
      if (!empty($language->prefix) && $language->prefix == $prefix) {
        return implode('/', $args);
      }
    }
  }
  return $path;
}

76
/**
77
 * Implementation of hook_menu().
78
 */
79 80 81 82 83
function strongarm_menu() {
  $items = array();
  $items['admin/settings/strongarm'] = array(
    'title' => 'Strongarm',
    'description' => 'Manage Drupal variable settings that have been strongarmed.',
84 85
    'page callback' => 'drupal_get_form',
    'page arguments' => array('strongarm_admin_form'),
86 87 88 89 90 91
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'),
    'file' => 'strongarm.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
92 93 94
}

/**
95 96
 * Implementation of hook_form_alter() for system_module form.
 * Clear strongarm & variable caches on modules page.
97
 */
98 99
function strongarm_form_system_module_alter(&$form, &$form_state) {
  strongarm_flush_caches();
100 101 102
}

/**
103
 * Implementation of hook_theme().
104
 */
105 106
function strongarm_theme() {
  return array(
107 108 109 110 111
    'strongarm_admin_form' => array(
      'arguments' => array(),
      'file' => 'strongarm.admin.inc',
      'path' => drupal_get_path('module', 'strongarm'),
    ),
112
  );
113 114 115
}

/**
116
 * Implementation of hook_flush_caches().
117
 */
118 119
function strongarm_flush_caches() {
  cache_clear_all('variables', 'cache');
120 121
  cache_clear_all('strongarm', 'cache');
}
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

/**
 * Implementation of hook_schema_alter().
 * Makes the variables table usable by ctools' export.inc.
 */
function strongarm_schema_alter(&$schema) {
  $schema['variable']['export'] = array(
    'key' => 'name',
    'identifier' => 'strongarm',
    'default hook' => 'strongarm',
    'api' => array(
      'owner' => 'system',
      'api' => 'strongarm',
      'minimum_version' => 1,
      'current_version' => 1,
    ),
138 139 140
    // We declare this explicitly -- otherwise, CTools will use
    // 'system_export_variable' as the default export callback.
    'export callback' => 'strongarm_export_variable',
141 142 143 144
  );
  $schema['variable']['fields']['value']['serialize'] = TRUE;
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158
/**
 * CTools export function.
 *
 * Workaround for var_export() #fail with stdClass objects. See:
 * http://bugs.php.net/bug.php?id=48016&edit=1. A proper patch is in the
 * CTools issue queue here: http://drupal.org/node/661332. Once this is
 * committed & included in a CTools release, this workaround will no
 * longer be necessary.
 */
function strongarm_export_variable($variable, $indent = '') {
  $export = ctools_export_object('variable', $variable, $indent);
  return str_replace('stdClass::__set_state(', '(object) (', $export);
}

159 160 161 162 163 164 165 166 167
/**
 * Implementation of hook_features_revert().
 */
if (!function_exists('variable_features_revert')) {
  function variable_features_revert($module) {
    ctools_component_features_revert('variable', $module);
    cache_clear_all('variables', 'cache');
  }
}