Commit fb9c1df0 authored by Dries's avatar Dries

- Patch #328357 by bleen18, sun, skilip, alanburke, jenlampton, manarth, fgm,...

- Patch #328357 by bleen18, sun, skilip, alanburke, jenlampton, manarth, fgm, tstoeckler: allow module .info files to add CSS/JS.
parent 8e51c3b2
......@@ -604,10 +604,8 @@ function list_themes($refresh = FALSE) {
}
}
foreach ($theme->info['scripts'] as $script => $path) {
if (file_exists($path)) {
$theme->scripts[$script] = $path;
}
}
if (isset($theme->info['engine'])) {
$theme->engine = $theme->info['engine'];
}
......@@ -2274,6 +2272,7 @@ function template_preprocess_page(&$variables) {
$variables['theme_hook_suggestions'] = $suggestions;
}
}
/**
* Process variables for html.tpl.php
*
......
......@@ -13,3 +13,4 @@ files[] = aggregator.processor.inc
files[] = aggregator.install
files[] = aggregator.test
configure = admin/config/services/aggregator/settings
stylesheets[all][] = aggregator.css
......@@ -282,13 +282,6 @@ function _aggregator_category_title($category) {
return $category['title'];
}
/**
* Implements hook_init().
*/
function aggregator_init() {
drupal_add_css(drupal_get_path('module', 'aggregator') . '/aggregator.css', array('preprocess' => TRUE));
}
/**
* Find out whether there are any aggregator categories.
*
......
......@@ -10,3 +10,4 @@ files[] = book.pages.inc
files[] = book.install
files[] = book.test
configure = admin/content/book/settings
stylesheets[all][] = book.css
......@@ -214,13 +214,6 @@ function book_admin_paths() {
return $paths;
}
/**
* Implements hook_init().
*/
function book_init() {
drupal_add_css(drupal_get_path('module', 'book') . '/book.css', array('preprocess' => TRUE));
}
/**
* Implements hook_entity_info_alter().
*/
......
......@@ -12,3 +12,4 @@ files[] = comment.install
files[] = comment.test
files[] = comment.tokens.inc
configure = admin/content/comment
stylesheets[all][] = comment.css
......@@ -289,13 +289,6 @@ function comment_menu() {
return $items;
}
/**
* Implements hook_init().
*/
function comment_init() {
drupal_add_css(drupal_get_path('module', 'comment') . '/comment.css', array('preprocess' => TRUE));
}
/**
* Implements hook_menu_alter().
*/
......
......@@ -15,3 +15,4 @@ files[] = field.form.inc
files[] = tests/field.test
dependencies[] = field_sql_storage
required = TRUE
stylesheets[all][] = theme/field.css
......@@ -168,13 +168,6 @@ function field_theme() {
);
}
/**
* Implements hook_init().
*/
function field_init() {
drupal_add_css(drupal_get_path('module', 'field') . '/theme/field.css', array('preprocess' => TRUE));
}
/**
* Implements hook_cron().
*
......
......@@ -12,3 +12,4 @@ files[] = forum.pages.inc
files[] = forum.install
files[] = forum.test
configure = admin/structure/forum
stylesheets[all][] = forum.css
......@@ -217,13 +217,6 @@ function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
}
}
/**
* Implements hook_init().
*/
function forum_init() {
drupal_add_css(drupal_get_path('module', 'forum') . '/forum.css', array('preprocess' => TRUE));
}
/**
* Implements hook_entity_info_alter().
*/
......
......@@ -13,3 +13,4 @@ files[] = node.test
files[] = node.tokens.inc
required = TRUE
configure = admin/structure/types
stylesheets[all][] = node.css
......@@ -2012,13 +2012,6 @@ function _node_custom_theme() {
}
}
/**
* Implements hook_init().
*/
function node_init() {
drupal_add_css(drupal_get_path('module', 'node') . '/node.css', array('preprocess' => TRUE));
}
function node_last_changed($nid) {
return db_query('SELECT changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetch()->changed;
}
......
......@@ -9,3 +9,4 @@ files[] = poll.pages.inc
files[] = poll.install
files[] = poll.test
files[] = poll.tokens.inc
stylesheets[all][] = poll.css
......@@ -27,13 +27,6 @@ function poll_help($path, $arg) {
}
}
/**
* Implements hook_init().
*/
function poll_init() {
drupal_add_css(drupal_get_path('module', 'poll') . '/poll.css', array('preprocess' => TRUE));
}
/**
* Implements hook_theme().
*/
......
......@@ -11,3 +11,4 @@ files[] = search.install
files[] = search.test
files[] = search.extender.inc
configure = admin/config/search/settings
stylesheets[all][] = search.css
......@@ -97,13 +97,6 @@ function search_help($path, $arg) {
}
}
/**
* Implements hook_init().
*/
function search_init() {
drupal_add_css(drupal_get_path('module', 'search') . '/search.css', array('preprocess' => TRUE));
}
/**
* Implements hook_theme().
*/
......
......@@ -580,6 +580,27 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
$this->assertEqual(array(), drupal_add_css(), t('Default CSS is empty.'));
}
/**
* Test that stylesheets in module .info files are loaded.
*/
function testModuleInfo() {
$this->drupalGet('');
// Verify common_test.css in a STYLE media="all" tag.
$elements = $this->xpath('//style[@media=:media and contains(text(), :filename)]', array(
':media' => 'all',
':filename' => 'tests/common_test.css',
));
$this->assertTrue(count($elements), "Stylesheet with media 'all' in module .info file found.");
// Verify common_test.print.css in a STYLE media="print" tag.
$elements = $this->xpath('//style[@media=:media and contains(text(), :filename)]', array(
':media' => 'print',
':filename' => 'tests/common_test.print.css',
));
$this->assertTrue(count($elements), "Stylesheet with media 'print' in module .info file found.");
}
/**
* Tests adding a file stylesheet.
*/
......
......@@ -5,4 +5,6 @@ package = Testing
version = VERSION
core = 7.x
files[] = common_test.module
stylesheets[all][] = common_test.css
stylesheets[print][] = common_test.print.css
hidden = TRUE
......@@ -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');
......
......@@ -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'];
}
}
......@@ -2496,6 +2515,41 @@ function system_rebuild_theme_data() {
return $themes;
}
/**
* 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.
*/
......
......@@ -12,3 +12,4 @@ files[] = user.test
files[] = user.tokens.inc
required = TRUE
configure = admin/config/people
stylesheets[all][] = user.css
......@@ -1798,13 +1798,6 @@ function user_admin_paths() {
return $paths;
}
/**
* Implements hook_init().
*/
function user_init() {
drupal_add_css(drupal_get_path('module', 'user') . '/user.css', array('preprocess' => TRUE));
}
/**
* Load either a specified or the current user account.
*
......
......@@ -12,8 +12,6 @@ stylesheets[all][] = css/style.css
stylesheets[all][] = css/colors.css
stylesheets[print][] = css/print.css
scripts[] = scripts/search.js
regions[header] = Header
regions[help] = Help
regions[page_top] = Page top
......
Markdown is supported
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