Commit 3f5bb7d9 authored by catch's avatar catch

Issue #2346369 by Wim Leers: Support special '#attached' variable for...

Issue #2346369 by Wim Leers: Support special '#attached' variable for attaching assets in preprocess functions.
parent 93c6556d
......@@ -372,6 +372,15 @@ function _theme($hook, $variables = array()) {
$preprocessor_function($variables, $hook, $info);
}
}
// Allow theme preprocess functions to set $variables['#attached'] and use
// it like the #attached property on render arrays. In Drupal 8, this is the
// (only) officially supported method of attaching assets from preprocess
// functions. Assets attached here should be associated with the template
// that we're preprocessing variables for.
if (isset($variables['#attached'])) {
$preprocess_attached = ['#attached' => $variables['#attached']];
drupal_render($preprocess_attached, TRUE);
}
}
// Generate the output using either a function or a template.
......@@ -1946,15 +1955,8 @@ function template_preprocess_maintenance_page(&$variables) {
$attributes['class'] = $classes;
// @see system_page_build()
$attached = array(
'#attached' => array(
'library' => array(
'core/normalize',
'system/maintenance',
),
),
);
drupal_render($attached);
$variables['#attached']['library'][] = 'core/normalize';
$variables['#attached']['library'][] = 'system/maintenance';
}
/**
......
......@@ -410,6 +410,31 @@ function testDrupalRenderThemeArguments() {
$this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works');
}
/**
* Tests theme preprocess functions being able to attach assets.
*/
function testDrupalRenderThemePreprocessAttached() {
\Drupal::state()->set('theme_preprocess_attached_test', TRUE);
$test_element = [
'#theme' => 'common_test_render_element',
'foo' => [
'#markup' => 'Kittens!',
],
];
drupal_render($test_element);
$expected_attached = [
'library' => [
'test/generic_preprocess',
'test/specific_preprocess',
]
];
$this->assertEqual($expected_attached, $test_element['#attached'], 'All expected assets from theme preprocess hooks attached.');
\Drupal::state()->set('theme_preprocess_attached_test', FALSE);
}
/**
* Tests caching of an empty render item.
*/
......
......@@ -142,6 +142,30 @@ function theme_common_test_empty($variables) {
return '';
}
/**
* Implements MODULE_preprocess().
*
* @see RenderTest::testDrupalRenderThemePreprocessAttached()
*/
function common_test_preprocess(&$variables, $hook) {
if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
return;
}
$variables['#attached']['library'][] = 'test/generic_preprocess';
}
/**
* Implements MODULE_preprocess_HOOK().
*
* @see RenderTest::testDrupalRenderThemePreprocessAttached()
*/
function common_test_preprocess_common_test_render_element(&$variables) {
if (!\Drupal::state()->get('theme_preprocess_attached_test', FALSE)) {
return;
}
$variables['#attached']['library'][] = 'test/specific_preprocess';
}
/**
* Implements hook_library_info_alter().
*/
......
......@@ -159,6 +159,21 @@
* suggestions as input, and can change this array (adding suggestions and
* removing them).
*
* @section Assets
*
* We can distinguish between two types of assets:
* 1. global assets (loaded on all pages where the theme is in use): these are
* defined in the theme's *.info.yml file.
* 2. template-specific assets (loaded on all pages where a specific template is
* in use): these can be added by in preprocessing functions, using @code
* $variables['#attached'] @endcode, e.g.:
* @code
* function seven_preprocess_menu_local_action(array &$variables) {
* // We require Modernizr's touch test for button styling.
* $variables['#attached']['library'][] = 'core/modernizr';
* }
* @endcode
*
* @see hooks
* @see callbacks
* @see theme_render
......
......@@ -329,8 +329,7 @@ function views_preprocess_page(&$variables) {
unset($class[$key]);
$attributes['class'] = $class;
$attributes['data-views-page-contextual-id'] = $variables['title_suffix']['contextual_links']['#id'];
$attached['#attached']['library'][] = 'views/views.contextual-links';
drupal_render($attached);
$variables['#attached']['library'][] = 'views/views.contextual-links';
}
}
}
......
......@@ -87,16 +87,9 @@ function bartik_preprocess_maintenance_page(&$variables) {
if (!$variables['db_is_active']) {
$variables['site_name'] = '';
}
// Normally we could attach libraries via hook_page_alter(), but when the
// database is inactive it's not called so we add them here.
$libraries = array(
'#attached' => array(
'library' => array(
'bartik/maintenance_page',
),
),
);
drupal_render($libraries);
// Bartik has custom styling for the maintenance page.
$variables['#attached']['library'][] = 'bartik/maintenance_page';
// Set the options that apply to both page and maintenance page.
_bartik_process_page($variables);
......
......@@ -16,7 +16,7 @@ install-page:
theme:
css/theme/install-page.css: {}
dependencies:
- system/maintenance
- seven/maintenance-page
drupal.nav-tabs:
version: VERSION
......
......@@ -128,14 +128,7 @@ function seven_preprocess_menu_local_action(array &$variables) {
$variables['link']['#options']['attributes']['class'][] = 'button--small';
// We require Modernizr's touch test for button styling.
$libraries = array(
'#attached' => array(
'library' => array(
'core/modernizr',
),
),
);
drupal_render($libraries);
$variables['#attached']['library'][] = 'core/modernizr';
}
/**
......@@ -158,17 +151,8 @@ function seven_preprocess_install_page(&$variables) {
$classes[] = 'install-background';
$attributes['class'] = $classes;
// Normally we could attach libraries via hook_page_alter(), but when the
// database is inactive it's not called so we add them here.
$libraries = array(
'#attached' => array(
'library' => array(
'seven/maintenance-page',
'seven/install-page',
),
),
);
drupal_render($libraries);
// Seven has custom styling for the install page.
$variables['#attached']['library'][] = 'seven/install-page';
}
/**
......@@ -181,16 +165,8 @@ function seven_preprocess_maintenance_page(&$variables) {
$classes[] = 'maintenance-background';
$attributes['class'] = $classes;
// // Normally we could attach libraries via hook_page_alter(), but when the
// // database is inactive it's not called so we add them here.
$libraries = array(
'#attached' => array(
'library' => array(
'seven/maintenance-page',
),
),
);
drupal_render($libraries);
// Seven has custom styling for the maintenance page.
$variables['#attached']['library'][] = 'seven/maintenance-page';
}
/**
......
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