Commit 79c34ed5 authored by catch's avatar catch
Browse files

Issue #1029460 by sun: Find a better hook than hook_init() for drupal_add_js()...

Issue #1029460 by sun: Find a better hook than hook_init() for drupal_add_js() and drupal_add_css().
parent 20c3db01
......@@ -5450,8 +5450,23 @@ function drupal_render(&$elements) {
$elements['#children'] = $elements['#markup'] . $elements['#children'];
}
// Add any JavaScript state information associated with the element.
if (!empty($elements['#states'])) {
drupal_process_states($elements);
}
// Add additional libraries, CSS, JavaScript an other custom
// attached data associated with this element.
if (!empty($elements['#attached'])) {
drupal_process_attached($elements);
}
// Let the theme functions in #theme_wrappers add markup around the rendered
// children.
// #states and #attached have to be processed before #theme_wrappers, because
// the #type 'page' render array from drupal_render_page() would render the
// $page and wrap it into the html.tpl.php template without the attached
// assets otherwise.
if (isset($elements['#theme_wrappers'])) {
foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
$elements['#children'] = theme($theme_wrapper, $elements);
......@@ -5467,17 +5482,6 @@ function drupal_render(&$elements) {
}
}
// Add any JavaScript state information associated with the element.
if (!empty($elements['#states'])) {
drupal_process_states($elements);
}
// Add additional libraries, CSS, JavaScript an other custom
// attached data associated with this element.
if (!empty($elements['#attached'])) {
drupal_process_attached($elements);
}
$prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
$suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
$output = $prefix . $elements['#children'] . $suffix;
......
......@@ -86,12 +86,11 @@ function dblog_menu() {
}
/**
* Implements hook_init().
* Implements hook_page_build().
*/
function dblog_init() {
function dblog_page_build(&$page) {
if (arg(0) == 'admin' && arg(1) == 'reports') {
// Add the CSS for this module
drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css');
$page['#attached']['css'][] = drupal_get_path('module', 'dblog') . '/dblog.css';
}
}
......
......@@ -523,9 +523,20 @@ function hook_ajax_render_alter($commands) {
* @see drupal_render_page()
*/
function hook_page_build(&$page) {
$path = drupal_get_path('module', 'foo');
// Add JavaScript/CSS assets to all pages.
// @see drupal_process_attached()
$page['#attached']['js'][$path . '/foo.css'] = array('every_page' => TRUE);
$page['#attached']['css'][$path . '/foo.base.css'] = array('every_page' => TRUE);
$page['#attached']['css'][$path . '/foo.theme.css'] = array('every_page' => TRUE);
// Add a special CSS file to a certain page only.
if (drupal_is_front_page()) {
$page['#attached']['css'][] = $path . '/foo.front.css';
}
// Append a standard disclaimer to the content region on a node detail page.
if (menu_get_object('node', 1)) {
// We are on a node detail page. Append a standard disclaimer to the
// content region.
$page['content']['disclaimer'] = array(
'#markup' => t('Acme, Inc. is not responsible for the contents of this sample code.'),
'#weight' => 25,
......@@ -1482,17 +1493,14 @@ function hook_boot() {
*
* 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.
*
* @see hook_boot()
* @see hook_exit()
*
* Do not use this hook to add CSS/JS to pages, use hook_page_build() instead.
*
* @see hook_page_build()
*/
function hook_init() {
// Since this file should only be loaded on the front page, it cannot be
// declared in the info file.
if (drupal_is_front_page()) {
drupal_add_css(drupal_get_path('module', 'foo') . '/foo.css');
}
}
/**
......
......@@ -2313,15 +2313,6 @@ function system_filetransfer_info() {
* Implements hook_init().
*/
function system_init() {
$path = drupal_get_path('module', 'system');
// Add the CSS for this module. These aren't in system.info, because they
// need to be in the CSS_SYSTEM group rather than the CSS_DEFAULT group.
drupal_add_css($path . '/system.base.css', array('group' => CSS_SYSTEM, 'every_page' => TRUE));
if (path_is_admin(current_path())) {
drupal_add_css($path . '/system.admin.css', array('group' => CSS_SYSTEM));
}
drupal_add_css($path . '/system.theme.css', array('group' => CSS_SYSTEM, 'every_page' => TRUE));
// Ignore slave database servers for this request.
//
// In Drupal's distributed database structure, new data is written to the master
......@@ -2343,9 +2334,22 @@ function system_init() {
unset($_SESSION['ignore_slave_server']);
}
}
}
/**
* Implements hook_page_build().
*/
function system_page_build(&$page) {
$path = drupal_get_path('module', 'system');
// Use the CSS_SYSTEM group to load these early.
$page['#attached']['css'][$path . '/system.base.css'] = array('group' => CSS_SYSTEM, 'every_page' => TRUE);
if (path_is_admin(current_path())) {
$page['#attached']['css'][$path . '/system.admin.css'] = array('group' => CSS_SYSTEM);
}
$page['#attached']['css'][$path . '/system.theme.css'] = array('group' => CSS_SYSTEM, 'every_page' => TRUE);
// Add CSS/JS files from module .info files.
system_add_module_assets();
system_add_module_assets($page);
}
/**
......@@ -2371,17 +2375,17 @@ function system_get_localized_date_format($languages) {
/**
* Adds CSS and JavaScript files declared in module .info files.
*/
function system_add_module_assets() {
function system_add_module_assets(&$page) {
foreach (system_get_module_info('stylesheets') as $module => $value) {
foreach ($value as $media => $stylesheets) {
foreach ($stylesheets as $stylesheet) {
drupal_add_css($stylesheet, array('every_page' => TRUE, 'media' => $media));
$page['#attached']['css'][$stylesheet] = array('every_page' => TRUE, 'media' => $media);
}
}
}
foreach (system_get_module_info('scripts') as $module => $scripts) {
foreach ($scripts as $script) {
drupal_add_js($script, array('every_page' => TRUE));
$page['#attached']['js'][$script] = array('every_page' => TRUE);
}
}
}
......
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