Revert "Issue #2171071 by jessebeach, Wim Leers: Remove drupal_add_library()."

Somehow this broke PHPUnit, so rolling back for now.

This reverts commit 91b465bdc5.
8.0.x
webchick 2014-01-15 12:08:12 -08:00
parent 0b55dcd841
commit 2214ca4e27
12 changed files with 110 additions and 241 deletions

View File

@ -2474,7 +2474,7 @@ function drupal_pre_render_scripts($elements) {
* FALSE if there were any missing library dependencies; TRUE if all library
* dependencies were met.
*
* @see _drupal_add_library()
* @see drupal_add_library()
* @see _drupal_add_js()
* @see _drupal_add_css()
* @see drupal_render()
@ -2490,7 +2490,7 @@ function drupal_process_attached($elements, $dependency_check = FALSE) {
// Add the libraries first.
$success = TRUE;
foreach ($elements['#attached']['library'] as $library) {
if (_drupal_add_library($library[0], $library[1]) === FALSE) {
if (drupal_add_library($library[0], $library[1]) === FALSE) {
$success = FALSE;
// Exit if the dependency is missing.
if ($dependency_check) {
@ -2692,7 +2692,7 @@ function drupal_process_states(&$elements) {
* @see hook_library_info()
* @see hook_library_info_alter()
*/
function _drupal_add_library($module, $name, $every_page = NULL) {
function drupal_add_library($module, $name, $every_page = NULL) {
$added = &drupal_static(__FUNCTION__, array());
// Only process the library if it exists and it was not added already.
@ -2752,7 +2752,7 @@ function _drupal_add_library($module, $name, $every_page = NULL) {
* or FALSE if it does not exist. If no $name was passed, an associative array
* of libraries registered by $module is returned (which may be empty).
*
* @see _drupal_add_library()
* @see drupal_add_library()
* @see hook_library_info()
* @see hook_library_info_alter()
*

View File

@ -1566,8 +1566,7 @@ function theme_tableselect($variables) {
// checkboxes/radios in the first table column.
if ($element['#js_select']) {
// Add a "Select all" checkbox.
$attached['#attached']['library'][] = array('system', 'drupal.tableselect');
drupal_render($attached);
drupal_add_library('system', 'drupal.tableselect');
array_unshift($header, array('class' => array('select-all')));
}
else {

View File

@ -1546,8 +1546,7 @@ function theme_table($variables) {
// Add sticky headers, if applicable.
if (count($header) && $sticky) {
$attached['#attached']['library'][] = array('system', 'drupal.tableheader');
drupal_render($attached);
drupal_add_library('system', 'drupal.tableheader');
// Add 'sticky-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$attributes['class'][] = 'sticky-enabled';
@ -1556,8 +1555,7 @@ function theme_table($variables) {
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (count($header) && $responsive) {
$attached['#attached']['library'][] = array('system', 'drupal.tableresponsive');
drupal_render($attached);
drupal_add_library('system', 'drupal.tableresponsive');
// Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$attributes['class'][] = 'responsive-enabled';
@ -2140,8 +2138,7 @@ function template_preprocess_html(&$variables) {
drupal_add_html_head($element, $name);
}
$attached['#attached']['library'][] = array('system', 'html5shiv');
drupal_render($attached);
drupal_add_library('system', 'html5shiv', TRUE);
$variables['page_top'][] = array('#markup' => $page->getBodyTop());
$variables['page_bottom'][] = array('#markup' => $page->getBodyBottom());

View File

@ -124,9 +124,7 @@ class OpenDialogCommand implements CommandInterface {
*/
public function render() {
// Add the library for handling the dialog in the response.
$attached['#attached']['library'][] = array('system', 'drupal.dialog.ajax');
drupal_render($attached);
drupal_add_library('system', 'drupal.dialog.ajax');
// For consistency ensure the modal option is set to TRUE or FALSE.
$this->dialogOptions['modal'] = isset($this->dialogOptions['modal']) && $this->dialogOptions['modal'];

View File

@ -121,9 +121,9 @@ class LocaleExportTest extends WebTestBase {
* Test exportation of translation template file.
*/
function testExportTranslationTemplateFile() {
// Load an admin page with JavaScript so _drupal_add_library() fires at
// least once and _locale_parse_js_file() gets to run at least once so that
// the locales_source table gets populated with something.
// Load an admin page with JavaScript so drupal_add_library() fires at least
// once and _locale_parse_js_file() gets to run at least once so that the
// locales_source table gets populated with something.
$this->drupalGet('admin/config/regional/language');
// Get the translation template file.
$this->drupalPostForm('admin/config/regional/translate/export', array(), t('Export'));

View File

@ -36,8 +36,7 @@ class LocaleLibraryInfoAlterTest extends WebTestBase {
* @see locale_library_info_alter()
*/
public function testLibraryInfoAlter() {
$attached['#attached']['library'][] = array('system', 'jquery.ui.datepicker');
drupal_render($attached);
drupal_add_library('system', 'jquery.ui.datepicker');
$scripts = drupal_get_js();
$this->assertTrue(strpos($scripts, 'locale.datepicker.js'), 'locale.datepicker.js added to scripts.');
}

View File

@ -17,6 +17,8 @@
function theme_simpletest_test_table($variables) {
$table = $variables['table'];
drupal_add_library('simpletest', 'drupal.simpletest');
// Create header for test selection table.
$header = array(
array('class' => array('select-all')),
@ -131,7 +133,6 @@ function theme_simpletest_test_table($variables) {
// Add js array of settings.
$attached = array();
$attached['#attached']['library'][] = array('simpletest', 'drupal.simpletest');
$attached['#attached']['js'][] = array(
'data' => array('simpleTest' => $js),
'type' => 'setting',

View File

@ -47,8 +47,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
$config->set('js.preprocess', 0);
$config->save();
// Reset _drupal_add_js() statics before each test.
// Reset _drupal_add_js() and drupal_add_library() statics before each test.
drupal_static_reset('_drupal_add_js');
drupal_static_reset('drupal_add_library');
}
function tearDown() {
@ -70,9 +71,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests adding a JavaScript file.
*/
function testAddFile() {
$attached['#attached']['js']['core/misc/collapse.js'] = array();
drupal_render($attached);
$javascript = _drupal_add_js();
$javascript = _drupal_add_js('core/misc/collapse.js');
$this->assertTrue(array_key_exists('core/misc/collapse.js', $javascript), 'JavaScript files are correctly added.');
}
@ -81,8 +80,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
*/
function testAddSetting() {
// Add a file in order to test default settings.
$attached['#attached']['library'][] = array('system', 'drupalSettings');
drupal_render($attached);
drupal_add_library('system', 'drupalSettings');
$javascript = _drupal_add_js();
$last_settings = reset($javascript['settings']['data']);
$this->assertTrue(array_key_exists('currentPath', $last_settings), 'The current path JavaScript setting is set correctly.');
@ -97,9 +95,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests adding an external JavaScript File.
*/
function testAddExternal() {
$attached['#attached']['js']['http://example.com/script.js'] = array('type' => 'external');
drupal_render($attached);
$javascript = _drupal_add_js();
$path = 'http://example.com/script.js';
$javascript = _drupal_add_js($path, 'external');
$this->assertTrue(array_key_exists('http://example.com/script.js', $javascript), 'Added an external JavaScript file.');
}
@ -109,18 +106,12 @@ class JavaScriptTest extends DrupalUnitTestBase {
function testAttributes() {
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['http://example.com/script.js'] = array(
'type' => 'external',
'attributes' => array('defer' => 'defer'),
);
$attached['#attached']['js']['core/misc/collapse.js'] = array(
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('http://example.com/script.js', array('attributes' => array('defer' => 'defer')));
_drupal_add_js('core/misc/collapse.js', array('attributes' => array('defer' => 'defer')));
$javascript = drupal_get_js();
$expected_1 = '<script src="http://example.com/script.js" defer="defer"></script>';
$expected_1 = '<script src="http://example.com/script.js?' . $default_query_string . '" defer="defer"></script>';
$expected_2 = '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '" defer="defer"></script>';
$this->assertTrue(strpos($javascript, $expected_1) > 0, 'Rendered external JavaScript with correct defer attribute.');
@ -136,18 +127,12 @@ class JavaScriptTest extends DrupalUnitTestBase {
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['http://example.com/script.js'] = array(
'type' => 'external',
'attributes' => array('defer' => 'defer'),
);
$attached['#attached']['js']['core/misc/collapse.js'] = array(
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('http://example.com/script.js', array('attributes' => array('defer' => 'defer')));
_drupal_add_js('core/misc/collapse.js', array('attributes' => array('defer' => 'defer')));
$javascript = drupal_get_js();
$expected_1 = '<script src="http://example.com/script.js" defer="defer"></script>';
$expected_1 = '<script src="http://example.com/script.js?' . $default_query_string . '" defer="defer"></script>';
$expected_2 = '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '" defer="defer"></script>';
$this->assertTrue(strpos($javascript, $expected_1) > 0, 'Rendered external JavaScript with correct defer attribute with aggregation enabled.');
@ -158,9 +143,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests drupal_get_js() for JavaScript settings.
*/
function testHeaderSetting() {
$attached = array();
$attached['#attached']['library'][] = array('system', 'drupalSettings');
drupal_render($attached);
drupal_add_library('system', 'drupalSettings');
$javascript = drupal_get_js('header');
$this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
@ -169,57 +152,25 @@ class JavaScriptTest extends DrupalUnitTestBase {
$this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.');
// Only the second of these two entries should appear in drupalSettings.
$attached = array();
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTest' => 'commonTestShouldNotAppear'),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTest' => 'commonTestShouldAppear'),
);
_drupal_add_js(array('commonTest' => 'commonTestShouldNotAppear'), 'setting');
_drupal_add_js(array('commonTest' => 'commonTestShouldAppear'), 'setting');
// Only the second of these entries should appear in drupalSettings.
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralOldValue')),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralNewValue')),
);
_drupal_add_js(array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralOldValue')), 'setting');
_drupal_add_js(array('commonTestJsArrayLiteral' => array('commonTestJsArrayLiteralNewValue')), 'setting');
// Only the second of these two entries should appear in drupalSettings.
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralOldValue')),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralNewValue')),
);
_drupal_add_js(array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralOldValue')), 'setting');
_drupal_add_js(array('commonTestJsObjectLiteral' => array('key' => 'commonTestJsObjectLiteralNewValue')), 'setting');
// Real world test case: multiple elements in a render array are adding the
// same (or nearly the same) JavaScript settings. When merged, they should
// contain all settings and not duplicate some settings.
$settings_one = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359));
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldIdentical' => $settings_one),
);
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldIdentical' => $settings_one),
);
_drupal_add_js(array('commonTestRealWorldIdentical' => $settings_one), 'setting');
_drupal_add_js(array('commonTestRealWorldIdentical' => $settings_one), 'setting');
$settings_two = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359, 'thingiesOnPage' => array('id1' => array())));
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldAlmostIdentical' => $settings_two),
);
_drupal_add_js(array('commonTestRealWorldAlmostIdentical' => $settings_two), 'setting');
$settings_two = array('moduleName' => array('ui' => array('button C', 'button D'), 'magical flag' => 3.14, 'thingiesOnPage' => array('id2' => array())));
$attached['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('commonTestRealWorldAlmostIdentical' => $settings_two),
);
_drupal_add_js(array('commonTestRealWorldAlmostIdentical' => $settings_two), 'setting');
drupal_render($attached);
$javascript = drupal_get_js('header');
// Test whether _drupal_add_js can be used to override a previous setting.
@ -254,9 +205,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests to see if resetting the JavaScript empties the cache.
*/
function testReset() {
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/collapse.js'] = array();
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('core/misc/collapse.js');
drupal_static_reset('_drupal_add_js');
$this->assertEqual(array(), _drupal_add_js(), 'Resetting the JavaScript correctly empties the cache.');
}
@ -265,15 +215,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests adding inline scripts.
*/
function testAddInline() {
drupal_add_library('system', 'jquery');
$inline = 'jQuery(function () { });';
$attached['#attached']['library'][] = array('system', 'jquery');
$attached['#attached']['js'][] = array(
'type' => 'inline',
'data' => $inline,
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
$javascript = _drupal_add_js();
$javascript = _drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
$this->assertTrue(array_key_exists('core/assets/vendor/jquery/jquery.js', $javascript), 'jQuery is added when inline scripts are added.');
$data = end($javascript);
$this->assertEqual($inline, $data['data'], 'Inline JavaScript is correctly added to the footer.');
@ -283,14 +227,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests rendering an external JavaScript file.
*/
function testRenderExternal() {
drupal_add_library('system', 'drupal');
$external = 'http://example.com/example.js';
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js'][] = array(
'type' => 'external',
'data' => $external,
);
drupal_render($attached);
_drupal_add_js($external, 'external');
$javascript = drupal_get_js();
// Local files have a base_path() prefix, external files should not.
$this->assertTrue(strpos($javascript, 'src="' . $external) > 0, 'Rendering an external JavaScript file.');
@ -300,16 +239,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests drupal_get_js() with a footer scope.
*/
function testFooterHTML() {
drupal_add_library('system', 'drupal');
$inline = 'jQuery(function () { });';
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js'][] = array(
'type' => 'inline',
'data' => $inline,
'scope' => 'footer',
'attributes' => array('defer' => 'defer'),
);
drupal_render($attached);
_drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
$javascript = drupal_get_js('footer');
$this->assertTrue(strpos($javascript, $inline) > 0, 'Rendered JavaScript footer returns the inline code.');
}
@ -318,10 +250,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests _drupal_add_js() sets preproccess to FALSE when cache is also FALSE.
*/
function testNoCache() {
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/collapse.js'] = array('cache' => FALSE);
drupal_render($attached);
$javascript = _drupal_add_js();
drupal_add_library('system', 'drupal');
$javascript = _drupal_add_js('core/misc/collapse.js', array('cache' => FALSE));
$this->assertFalse($javascript['core/misc/collapse.js']['preprocess'], 'Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.');
}
@ -329,10 +259,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests adding a JavaScript file with a different group.
*/
function testDifferentGroup() {
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/collapse.js'] = array('group' => JS_THEME);
drupal_render($attached);
$javascript = _drupal_add_js();
drupal_add_library('system', 'drupal');
$javascript = _drupal_add_js('core/misc/collapse.js', array('group' => JS_THEME));
$this->assertEqual($javascript['core/misc/collapse.js']['group'], JS_THEME, 'Adding a JavaScript file with a different group caches the given group.');
}
@ -340,9 +268,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests adding a JavaScript file with a different weight.
*/
function testDifferentWeight() {
$attached['#attached']['js']['core/misc/collapse.js'] = array('weight' => 2);
drupal_render($attached);
$javascript = _drupal_add_js();
$javascript = _drupal_add_js('core/misc/collapse.js', array('weight' => 2));
$this->assertEqual($javascript['core/misc/collapse.js']['weight'], 2, 'Adding a JavaScript file with a different weight caches the given weight.');
}
@ -354,16 +280,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
function testBrowserConditionalComments() {
$default_query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/collapse.js'] = array(
'browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE),
);
$attached['#attached']['js'][] = array(
'type' => 'inline',
'data' => 'jQuery(function () { });',
'browsers' => array('IE' => FALSE),
);
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('core/misc/collapse.js', array('browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE)));
_drupal_add_js('jQuery(function () { });', array('type' => 'inline', 'browsers' => array('IE' => FALSE)));
$javascript = drupal_get_js();
$expected_1 = "<!--[if lte IE 8]>\n" . '<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '"></script>' . "\n<![endif]-->";
@ -377,10 +296,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests JavaScript versioning.
*/
function testVersionQueryString() {
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/collapse.js'] = array('version' => 'foo');
$attached['#attached']['js']['core/misc/ajax.js'] = array('version' => 'bar');
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('core/misc/collapse.js', array('version' => 'foo'));
_drupal_add_js('core/misc/ajax.js', array('version' => 'bar'));
$javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'core/misc/collapse.js?v=foo') > 0 && strpos($javascript, 'core/misc/ajax.js?v=bar') > 0 , 'JavaScript version identifiers correctly appended to URLs');
}
@ -395,13 +313,11 @@ class JavaScriptTest extends DrupalUnitTestBase {
// ahead of ones without. The order of JavaScript execution must be the
// same regardless of whether aggregation is enabled, so ensure this
// expected order, first with aggregation off.
$attached = array();
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/ajax.js'] = array();
$attached['#attached']['js']['core/misc/collapse.js'] = array('every_page' => TRUE);
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
$attached['#attached']['js']['core/misc/batch.js'] = array('every_page' => TRUE);
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('core/misc/ajax.js');
_drupal_add_js('core/misc/collapse.js', array('every_page' => TRUE));
_drupal_add_js('core/misc/autocomplete.js');
_drupal_add_js('core/misc/batch.js', array('every_page' => TRUE));
$javascript = drupal_get_js();
$expected = implode("\n", array(
'<script src="' . file_create_url('core/misc/collapse.js') . '?' . $default_query_string . '"></script>',
@ -417,13 +333,11 @@ class JavaScriptTest extends DrupalUnitTestBase {
$config = \Drupal::config('system.performance');
$config->set('js.preprocess', 1);
$config->save();
$attached = array();
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/ajax.js'] = array();
$attached['#attached']['js']['core/misc/collapse.js'] = array('every_page' => TRUE);
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
$attached['#attached']['js']['core/misc/batch.js'] = array('every_page' => TRUE);
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('core/misc/ajax.js');
_drupal_add_js('core/misc/collapse.js', array('every_page' => TRUE));
_drupal_add_js('core/misc/autocomplete.js');
_drupal_add_js('core/misc/batch.js', array('every_page' => TRUE));
$js_items = _drupal_add_js();
$javascript = drupal_get_js();
$expected = implode("\n", array(
@ -442,11 +356,9 @@ class JavaScriptTest extends DrupalUnitTestBase {
drupal_static_reset('_drupal_add_js');
// Add two JavaScript files to the current request and build the cache.
$attached = array();
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['core/misc/ajax.js'] = array();
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('core/misc/ajax.js');
_drupal_add_js('core/misc/autocomplete.js');
$js_items = _drupal_add_js();
$scripts_html = array(
@ -465,12 +377,10 @@ class JavaScriptTest extends DrupalUnitTestBase {
// Reset variables and add a file in a different scope first.
\Drupal::state()->delete('system.js_cache_files');
drupal_static_reset('_drupal_add_js');
$attached = array();
$attached['#attached']['library'][] = array('system', 'drupal');
$attached['#attached']['js']['some/custom/javascript_file.js'] = array('scope' => 'footer');
$attached['#attached']['js']['core/misc/ajax.js'] = array();
$attached['#attached']['js']['core/misc/autocomplete.js'] = array();
drupal_render($attached);
drupal_add_library('system', 'drupal');
_drupal_add_js('some/custom/javascript_file.js', array('scope' => 'footer'));
_drupal_add_js('core/misc/ajax.js');
_drupal_add_js('core/misc/autocomplete.js');
// Rebuild the cache.
$js_items = _drupal_add_js();
@ -493,50 +403,17 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Tests JavaScript ordering.
*/
function testRenderOrder() {
$shared_options = array(
'type' => 'inline',
'scope' => 'footer',
);
// Add a bunch of JavaScript in strange ordering.
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight 5 #1");})(jQuery);',
'weight' => 5,
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight 0 #1");})(jQuery);',
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight 0 #2");})(jQuery);',
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight -8 #1");})(jQuery);',
'weight' => -8,
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight -8 #2");})(jQuery);',
'weight' => -8,
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight -8 #3");})(jQuery);',
'weight' => -8,
);
$attached['#attached']['js']['http://example.com/example.js?Weight -5 #1'] = array(
'type' => 'external',
'scope' => 'footer',
'weight' => -5,
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight -8 #4");})(jQuery);',
'weight' => -8,
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight 5 #2");})(jQuery);',
'weight' => 5,
);
$attached['#attached']['js'][] = $shared_options + array(
'data' => '(function($){alert("Weight 0 #3");})(jQuery);',
);
drupal_render($attached);
_drupal_add_js('(function($){alert("Weight 5 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));
_drupal_add_js('(function($){alert("Weight 0 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
_drupal_add_js('(function($){alert("Weight 0 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
_drupal_add_js('(function($){alert("Weight -8 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
_drupal_add_js('(function($){alert("Weight -8 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
_drupal_add_js('(function($){alert("Weight -8 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
_drupal_add_js('http://example.com/example.js?Weight -5 #1', array('type' => 'external', 'scope' => 'footer', 'weight' => -5));
_drupal_add_js('(function($){alert("Weight -8 #4");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8));
_drupal_add_js('(function($){alert("Weight 5 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5));
_drupal_add_js('(function($){alert("Weight 0 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer'));
// Construct the expected result from the regex.
$expected = array(
@ -571,13 +448,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
// JavaScript files are sorted first by group, then by the 'every_page'
// flag, then by weight (see drupal_sort_css_js()), so to test the effect of
// weight, we need the other two options to be the same.
$attached['#attached']['library'][] = array('system', 'jquery');
$attached['#attached']['js']['core/misc/collapse.js'] = array(
'group' => JS_LIBRARY,
'every_page' => TRUE,
'weight' => -21,
);
drupal_render($attached);
drupal_add_library('system', 'jquery');
_drupal_add_js('core/misc/collapse.js', array('group' => JS_LIBRARY, 'every_page' => TRUE, 'weight' => -21));
$javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'core/misc/collapse.js') < strpos($javascript, 'core/assets/vendor/jquery/jquery.js'), 'Rendering a JavaScript file above jQuery.');
}
@ -589,9 +461,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
*/
function testAlter() {
// Add both tableselect.js and simpletest.js, with a larger weight on SimpleTest.
$attached['#attached']['js']['core/misc/tableselect.js'] = array();
$attached['#attached']['js'][drupal_get_path('module', 'simpletest') . '/simpletest.js'] = array('weight' => 9999);
drupal_render($attached);
_drupal_add_js('core/misc/tableselect.js');
_drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => 9999));
// Render the JavaScript, testing if simpletest.js was altered to be before
// tableselect.js. See simpletest_js_alter() to see where this alteration
@ -604,17 +475,14 @@ class JavaScriptTest extends DrupalUnitTestBase {
* Adds a library to the page and tests for both its JavaScript and its CSS.
*/
function testLibraryRender() {
$attached = array();
$attached['#attached']['library'][] = array('system', 'jquery.farbtastic');
drupal_render($attached);
$result = drupal_add_library('system', 'jquery.farbtastic');
$this->assertTrue($result !== FALSE, 'Library was added without errors.');
$scripts = drupal_get_js();
$styles = drupal_get_css();
$this->assertTrue(strpos($scripts, 'core/assets/vendor/farbtastic/farbtastic.js'), 'JavaScript of library was added to the page.');
$this->assertTrue(strpos($styles, 'core/assets/vendor/farbtastic/farbtastic.css'), 'Stylesheet of library was added to the page.');
$attached = array();
$attached['#attached']['library'][] = array('common_test', 'shorthand.plugin');
drupal_render($attached);
drupal_add_library('common_test', 'shorthand.plugin');
$path = drupal_get_path('module', 'common_test') . '/js/shorthand.js?v=0.8.3.37';
$scripts = drupal_get_js();
$this->assertTrue(strpos($scripts, $path), 'JavaScript specified in hook_library_info() using shorthand format (without any options) was added to the page.');
@ -632,8 +500,7 @@ class JavaScriptTest extends DrupalUnitTestBase {
$this->assertEqual($library['title'], 'Farbtastic: Altered Library', 'Registered libraries were altered.');
// common_test_library_info_alter() also added a dependency on jQuery Form.
$attached['#attached']['library'][] = array('system', 'jquery.farbtastic');
drupal_render($attached);
drupal_add_library('system', 'jquery.farbtastic');
$scripts = drupal_get_js();
$this->assertTrue(strpos($scripts, 'core/assets/vendor/jquery-form/jquery.form.js'), 'Altered library dependencies are added to the page.');
}
@ -656,8 +523,8 @@ class JavaScriptTest extends DrupalUnitTestBase {
$this->assertFalse($result, 'Unknown library returned FALSE.');
drupal_static_reset('drupal_get_library');
$attached['#attached']['library'][] = array('unknown', 'unknown');
drupal_render($attached);
$result = drupal_add_library('unknown', 'unknown');
$this->assertFalse($result, 'Unknown library returned FALSE.');
$scripts = drupal_get_js();
$this->assertTrue(strpos($scripts, 'unknown') === FALSE, 'Unknown library was not added to the page.');
}

View File

@ -311,7 +311,7 @@ function hook_js_alter(&$javascript) {
* An array defining libraries associated with a module.
*
* @see system_library_info()
* @see _drupal_add_library()
* @see drupal_add_library()
* @see drupal_get_library()
*/
function hook_library_info() {

View File

@ -1260,7 +1260,6 @@ function system_library_info() {
'version' => '3.6.2',
'js' => array(
'core/assets/vendor/html5shiv/html5.js' => array(
'every_page' => TRUE,
'group' => JS_LIBRARY,
'weight' => -22,
'browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE),
@ -2184,7 +2183,7 @@ function system_user_timezone(&$form, &$form_state) {
if (!$account->getTimezone() && $account->id() == $user->id() && empty($form_state['input']['timezone'])) {
$form['timezone']['#description'] = t('Your time zone setting will be automatically detected if possible. Confirm the selection and click save.');
$form['timezone']['timezone']['#attributes'] = array('class' => array('timezone-detect'));
$form['#attached']['library'][] = array('system', 'drupal.timezone');
drupal_add_library('system', 'drupal.timezone');
}
}

View File

@ -360,8 +360,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'][] = array('views', 'views.contextual-links');
drupal_render($attached);
drupal_add_library('views', 'views.contextual-links');
}
}
}

View File

@ -5,7 +5,7 @@
* Contains \Drupal\Tests\Core\Ajax\AjaxCommandsTest.
*/
namespace Drupal\Tests\Core\Ajax;
namespace Drupal\Tests\Core\Ajax {
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Ajax\AddCssCommand;
@ -424,3 +424,13 @@ class AjaxCommandsTest extends UnitTestCase {
}
}
}
namespace {
if (!function_exists('drupal_add_library')) {
function drupal_add_library() {
return TRUE;
}
}
}