Manual:Hooks/ResourceLoaderRegisterModules

From MediaWiki.org
Jump to: navigation, search
ResourceLoaderRegisterModules
Available from version 1.17.0
Allows conditionally registering of modules with ResourceLoader
Define function:
public static function onResourceLoaderRegisterModules( &$resourceLoader ) { ... }
Attach hook:

In extension.json:

{
	"Hooks": {
		"ResourceLoaderRegisterModules": [
			"MyExtensionHooks::onResourceLoaderRegisterModules"
		]
	}
}

For MediaWiki ≤1.25:

$wgHooks['ResourceLoaderRegisterModules'][] = 'MyExtensionHooks::onResourceLoaderRegisterModules';
Called from: File(s): ResourceLoader.php

For more information about attaching hooks, see Manual:Hooks.
For examples of extensions using this hook, see Category:ResourceLoaderRegisterModules extensions.

Details[edit]

  • &$resourceLoader - The ResourceLoader object.

Usage[edit]

Warning Warning: Only use this hook when required (e.g. for a conditional dependency). If possible, use $wgResourceModules. See also ResourceLoader/Migration guide (developers).

ResourceLoaderModule objects, which provide access to scripts, styles, and messages can be added to the ResourceLoader at this point. A common use case is registering a resource with "soft" dependencies on other extensions and classes. For example, a module can depend on an EventLogging schema only if EventLogging is installed. Using this hook allows adding to the dependencies array conditionally before registering the module. For a single registration:

$resourceLoader->register( 'myModule', [ 'scripts' => '...' ] );

You can pass in an array of 'script', 'styles', 'localBasePath', etc. as you do when appending an item to $wgResourceModules.

For multiple registrations:

$resourceLoader->register( array( 'myModule' => [ 'scripts' => '...' ], ... ) );

Note that for MW 1.26 and later, you can not modify $wgResourceModules using this hook listener.

See also[edit]