diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-09-05 02:21:38 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-09-05 02:21:38 +0000 |
commit | fb9c1df0b942df39115a9130dfa76e2b3fd33217 (patch) | |
tree | 7dce29a2f9eebf607d9106fa2f1aed6fbe0d8b61 /modules/system | |
parent | 8e51c3b2a5796737583b59a4970812ff66755b38 (diff) | |
download | brdo-fb9c1df0b942df39115a9130dfa76e2b3fd33217.tar.gz brdo-fb9c1df0b942df39115a9130dfa76e2b3fd33217.tar.bz2 |
- Patch #328357 by bleen18, sun, skilip, alanburke, jenlampton, manarth, fgm, tstoeckler: allow module .info files to add CSS/JS.
Diffstat (limited to 'modules/system')
-rw-r--r-- | modules/system/system.api.php | 9 | ||||
-rw-r--r-- | modules/system/system.module | 94 |
2 files changed, 79 insertions, 24 deletions
diff --git a/modules/system/system.api.php b/modules/system/system.api.php index 276d39f8b..ebd4f20ed 100644 --- a/modules/system/system.api.php +++ b/modules/system/system.api.php @@ -1441,9 +1441,10 @@ function hook_boot() { * used to set up global parameters which are needed later in the request. * when this hook is called, all modules are already loaded in memory. * - * For example, this hook is a typical place for modules to add CSS or JS - * that should be present on every page. This hook is not run on cached - * pages - though CSS or JS added this way will be present on a cached page. + * This hook is not run on cached pages. + * + * To add CSS or JS that should be present on all pages, modules should not + * implement this hook, but declare these files in their .info file. */ function hook_init() { drupal_add_css(drupal_get_path('module', 'book') . '/book.css'); @@ -3955,7 +3956,7 @@ function hook_filetransfer_backends() { /** * Control site status before menu dispatching. * - * The hook is called after checking whether the site is offline but before + * The hook is called after checking whether the site is offline but before * the current router item is retrieved and executed by * menu_execute_active_handler(). If the site is in offline mode, * $menu_site_status is set to MENU_SITE_OFFLINE. diff --git a/modules/system/system.module b/modules/system/system.module index 33f232f74..f0cab512c 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -1837,7 +1837,6 @@ function system_init() { drupal_add_css($path . '/system-menus.css', array('weight' => CSS_SYSTEM, 'preprocess' => TRUE)); drupal_add_css($path . '/system-messages.css', array('weight' => CSS_SYSTEM, 'preprocess' => TRUE)); - // Ignore slave database servers for this request. // // In Drupal's distributed database structure, new data is written to the master @@ -1859,6 +1858,29 @@ function system_init() { unset($_SESSION['ignore_slave_server']); } } + + // Add CSS/JS files from module .info files. + system_add_module_assets(); +} + +/** + * Adds CSS and JavaScript files declared in module .info files. + */ +function system_add_module_assets() { + foreach (system_get_info('module') as $module => $info) { + if (!empty($info['stylesheets'])) { + foreach ($info['stylesheets'] as $media => $stylesheets) { + foreach ($stylesheets as $stylesheet) { + drupal_add_css($stylesheet, array('media' => $media, 'preprocess' => TRUE)); + } + } + } + if (!empty($info['scripts'])) { + foreach ($info['scripts'] as $script) { + drupal_add_js($script, array('preprocess' => TRUE)); + } + } + } } /** @@ -2305,6 +2327,15 @@ function _system_rebuild_module_data() { // Merge in defaults and save. $modules[$key]->info = $module->info + $defaults; + // Prefix stylesheets and scripts with module path. + $path = dirname($module->uri); + if (isset($module->info['stylesheets'])) { + $module->info['stylesheets'] = _system_info_add_path($module->info['stylesheets'], $path); + } + if (isset($module->info['scripts'])) { + $module->info['scripts'] = _system_info_add_path($module->info['scripts'], $path); + } + // Install profiles are hidden by default, unless explicitly specified // otherwise in the .info file. if ($key == $profile && !isset($modules[$key]->info['hidden'])) { @@ -2396,6 +2427,8 @@ function _system_rebuild_theme_data() { 'features' => _system_default_theme_features(), 'screenshot' => 'screenshot.png', 'php' => DRUPAL_MINIMUM_PHP, + 'stylesheets' => array(), + 'scripts' => array(), ); $sub_themes = array(); @@ -2428,28 +2461,14 @@ function _system_rebuild_theme_data() { } } - // Give the stylesheets proper path information. - $pathed_stylesheets = array(); - if (isset($themes[$key]->info['stylesheets'])) { - foreach ($themes[$key]->info['stylesheets'] as $media => $stylesheets) { - foreach ($stylesheets as $stylesheet) { - $pathed_stylesheets[$media][$stylesheet] = dirname($themes[$key]->uri) . '/' . $stylesheet; - } - } - } - $themes[$key]->info['stylesheets'] = $pathed_stylesheets; + // Prefix stylesheets and scripts with module path. + $path = dirname($theme->uri); + $theme->info['stylesheets'] = _system_info_add_path($theme->info['stylesheets'], $path); + $theme->info['scripts'] = _system_info_add_path($theme->info['scripts'], $path); - // Give the scripts proper path information. - $scripts = array(); - if (isset($themes[$key]->info['scripts'])) { - foreach ($themes[$key]->info['scripts'] as $script) { - $scripts[$script] = dirname($themes[$key]->uri) . '/' . $script; - } - } - $themes[$key]->info['scripts'] = $scripts; // Give the screenshot proper path information. if (!empty($themes[$key]->info['screenshot'])) { - $themes[$key]->info['screenshot'] = dirname($themes[$key]->uri) . '/' . $themes[$key]->info['screenshot']; + $themes[$key]->info['screenshot'] = $path . '/' . $themes[$key]->info['screenshot']; } } @@ -2497,6 +2516,41 @@ function system_rebuild_theme_data() { } /** + * Prefixes all values in an .info file array with a given path. + * + * This helper function is mainly used to prefix all array values of an .info + * file property with a single given path (to the module or theme); e.g., to + * prefix all values of the 'stylesheets' or 'scripts' properties with the file + * path to the defining module/theme. + * + * @param $info + * A nested array of data of an .info file to be processed. + * @param $path + * A file path to prepend to each value in $info. + * + * @return + * The $info array with prefixed values. + * + * @see _system_rebuild_module_data() + * @see _system_rebuild_theme_data() + */ +function _system_info_add_path($info, $path) { + foreach ($info as $key => $value) { + // Recurse into nested values until we reach the deepest level. + if (is_array($value)) { + $info[$key] = _system_info_add_path($info[$key], $path); + } + // Unset the original value's key and set the new value with prefix, using + // the original value as key, so original values can still be looked up. + else { + unset($info[$key]); + $info[$value] = $path . '/' . $value; + } + } + return $info; +} + +/** * Returns an array of default theme features. */ function _system_default_theme_features() { |