Jump to content

Manual:Hooks/ResourceLoaderRegisterModules

From mediawiki.org
ResourceLoaderRegisterModules
Available from version 1.17.0
Allows conditionally registering of modules with ResourceLoader
Define function:
public static function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ) { ... }
Attach hook: In extension.json:
{
	"Hooks": {
		"ResourceLoaderRegisterModules": "MediaWiki\\Extension\\MyExtension\\Hooks::onResourceLoaderRegisterModules"
	}
}
Called from: File(s): includes/ServiceWiring.php
Interface: ResourceLoaderRegisterModulesHook.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