diff --git a/libraries.api.php b/libraries.api.php index 023cc0a..d4b69e7 100644 --- a/libraries.api.php +++ b/libraries.api.php @@ -159,10 +159,13 @@ * variant, a boolean indicating whether the variant is installed or * not. * Note that in this group the 'versions' property is no longer available. - * - load: Callbacks registered in this group are applied directly + * - pre-load: Callbacks registered in this group are applied directly * before this library is loaded. At this point the library contains * variant-specific information, if specified. Note that in this group the * 'variants' property is no longer available. + * - post-load: Callbacks registered in this group are applied directly + * after this library is loaded. At this point, the library contains a + * "loaded" key, which contains the number of files that were loaded. * Additional top-level properties can be registered as needed. * * @see hook_library() @@ -275,11 +278,27 @@ function hook_libraries_info() { ), ), // Optionally register callbacks to apply to the library during different - // stages of its lifetime ('callback groups'). Here, a callback is - // registered in the 'detect' group. + // stages of its lifetime ('callback groups'). 'callbacks' => array( - 'detect' => array( - 'mymodule_example_detect_callback', + // Used to alter the info associated with the library. + 'info' => array( + 'mymodule_example_libraries_info_callback', + ), + // Called before detecting the given library. + 'pre-detect' => array( + 'mymodule_example_libraries_predetect_callback', + ), + // Called after detecting the library. + 'post-detect' => array( + 'mymodule_example_libraries_postdetect_callback', + ), + // Called before the library is loaded. + 'pre-load' => array( + 'mymodule_example_libraries_preload_callback', + ), + // Called after the library is loaded. + 'post-load' => array( + 'mymodule_example_libraries_postload_callback', ), ), ); diff --git a/libraries.module b/libraries.module index 929b8ef..059bfea 100644 --- a/libraries.module +++ b/libraries.module @@ -398,7 +398,8 @@ function libraries_info_defaults(&$library, $name) { 'info' => array(), 'pre-detect' => array(), 'post-detect' => array(), - 'load' => array(), + 'pre-load' => array(), + 'post-load' => array(), ); // Add our own callbacks before any others. @@ -596,10 +597,14 @@ function libraries_load($name, $variant = NULL) { } } - // Invoke callbacks in the 'load' group. - libraries_invoke('load', $library); + // Invoke callbacks in the 'pre-load' group. + libraries_invoke('pre-load', $library); + // Load all the files associated with the library. $library['loaded'] = libraries_load_files($library); + + // Invoke callbacks in the 'post-load' group. + libraries_invoke('post-load', $library); } $loaded[$name] = $library; } diff --git a/tests/libraries.test b/tests/libraries.test index 6ff9296..3bb731b 100644 --- a/tests/libraries.test +++ b/tests/libraries.test @@ -255,13 +255,15 @@ class LibrariesTestCase extends DrupalWebTestCase { 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ), ), 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ), ), 'variants' => array( @@ -269,19 +271,22 @@ class LibrariesTestCase extends DrupalWebTestCase { 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ), ), 'callbacks' => array( 'info' => array('_libraries_test_info_callback'), 'pre-detect' => array('_libraries_test_pre_detect_callback'), 'post-detect' => array('_libraries_test_post_detect_callback'), - 'load' => array('_libraries_test_load_callback'), + 'pre-load' => array('_libraries_test_pre_load_callback'), + 'post-load' => array('_libraries_test_post_load_callback'), ), 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', 'module' => 'libraries_test', ); libraries_info_defaults($expected, 'example_callback'); @@ -322,11 +327,12 @@ class LibrariesTestCase extends DrupalWebTestCase { // already loaded variant. unset($expected['variants']); $expected['loaded'] = 0; - $expected['load callback'] = 'applied (top-level)'; + $expected['pre-load callback'] = 'applied (top-level)'; + $expected['post-load callback'] = 'applied (top-level)'; $library = libraries_load('example_callback'); $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); - $this->assertEqual($library, $expected, 'Load callback was applied correctly.'); + $this->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.'); // This is not recommended usually and is only used for testing purposes. drupal_static_reset('libraries_load'); // Successfully loaded library variants are supposed to contain the specific @@ -337,7 +343,7 @@ class LibrariesTestCase extends DrupalWebTestCase { $library = libraries_load('example_callback', 'example_variant'); $this->verbose('Expected:
' . var_export($expected, TRUE) . '
'); $this->verbose('Actual:
' . var_export($library, TRUE) . '
'); - $this->assertEqual($library, $expected, 'Load callback was applied correctly to a variant.'); + $this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.'); // Test loading of a simple library with a top-level files property. $this->drupalGet('libraries_test/files'); @@ -370,14 +376,16 @@ class LibrariesTestCase extends DrupalWebTestCase { $this->assertRaw('The info callback group was invoked.', 'Info callback invoked for uncached libraries.'); $this->assertRaw('The pre-detect callback group was invoked.', 'Pre-detect callback invoked for uncached libraries.'); $this->assertRaw('The post-detect callback group was invoked.', 'Post-detect callback invoked for uncached libraries.'); - $this->assertRaw('The load callback group was invoked.', 'Load callback invoked for uncached libraries.'); - // When the library information is cached only the load callback group should - // be invoked. + $this->assertRaw('The pre-load callback group was invoked.', 'Pre-load callback invoked for uncached libraries.'); + $this->assertRaw('The post-load callback group was invoked.', 'Post-load callback invoked for uncached libraries.'); + // When the library information is cached only the pre-load callback group + // should be invoked. $this->drupalGet('libraries_test/cache'); $this->assertNoRaw('The info callback group was not invoked.', 'Info callback not invoked for cached libraries.'); $this->assertNoRaw('The pre-detect callback group was not invoked.', 'Pre-detect callback not invoked for cached libraries.'); $this->assertNoRaw('The post-detect callback group was not invoked.', 'Post-detect callback not invoked for cached libraries.'); - $this->assertRaw('The load callback group was invoked.', 'Load callback invoked for cached libraries.'); + $this->assertRaw('The pre-load callback group was invoked.', 'Pre-load callback invoked for cached libraries.'); + $this->assertRaw('The post-load callback group was invoked.', 'Post-load callback invoked for cached libraries.'); variable_set('libraries_test_cache', FALSE); } diff --git a/tests/libraries_test.module b/tests/libraries_test.module index 3ea679a..1f67996 100644 --- a/tests/libraries_test.module +++ b/tests/libraries_test.module @@ -245,14 +245,16 @@ function libraries_test_libraries_info() { 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ), ), // These keys are for testing purposes only. 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ), ), 'variants' => array( @@ -261,20 +263,23 @@ function libraries_test_libraries_info() { 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ), ), 'callbacks' => array( 'info' => array('_libraries_test_info_callback'), 'pre-detect' => array('_libraries_test_pre_detect_callback'), 'post-detect' => array('_libraries_test_post_detect_callback'), - 'load' => array('_libraries_test_load_callback'), + 'pre-load' => array('_libraries_test_pre_load_callback'), + 'post-load' => array('_libraries_test_post_load_callback'), ), // These keys are for testing purposes only. 'info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect callback' => 'not applied', - 'load callback' => 'not applied', + 'pre-load callback' => 'not applied', + 'post-load callback' => 'not applied', ); return $libraries; @@ -392,14 +397,25 @@ function _libraries_test_post_detect_callback(&$library, $version, $variant) { } /** - * Sets the 'load callback' key. + * Sets the 'pre-load callback' key. * - * This function is used as a test callback for the 'load' callback group. + * This function is used as a test callback for the 'pre-load' callback group. * * @see _libraries_test_callback() */ -function _libraries_test_load_callback(&$library, $version, $variant) { - _libraries_test_callback($library, $version, $variant, 'load'); +function _libraries_test_pre_load_callback(&$library, $version, $variant) { + _libraries_test_callback($library, $version, $variant, 'pre-load'); +} + +/** + * Sets the 'post-load callback' key. + * + * This function is used as a test callback for the 'post-load' callback group. + * + * @see _libraries_test_callback() + */ +function _libraries_test_post_load_callback(&$library, $version, $variant) { + _libraries_test_callback($library, $version, $variant, 'post-load'); } /**