Manual:Hooks/ResourceLoaderGetConfigVars

From MediaWiki.org
Jump to navigation Jump to search

Other languages:
English • ‎dansk • ‎日本語
ResourceLoaderGetConfigVars
Available from version 1.17.0
Called right before ResourceLoaderStartUpModule::getConfig returns, to set static (not request-specific) configuration variables. Can not depend on current page, current user or current request; see below.
Define function:
public static function onResourceLoaderGetConfigVars( &$vars, string $skin ) { ... }
Attach hook:

In extension.json:

{
	"Hooks": {
		"ResourceLoaderGetConfigVars": "MyExtensionHooks::onResourceLoaderGetConfigVars"
	}
}

For MediaWiki ≤1.25:

$wgHooks['ResourceLoaderGetConfigVars'][] = 'MyExtensionHooks::onResourceLoaderGetConfigVars';
Called from:File(s): ResourceLoaderStartUpModule.php

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

Details[edit]

ResourceLoaderStartUpModule::getConfig() runs this hook. Use it to export static configuration variables to JavaScript. Values that depend on the current page, user or request state must be added through MakeGlobalVariablesScriptTemplate:Enlink/list1 instead.

  • &$vars: Array of variables to be added into the output of the startup module.
  • $skin: Current skin name to restrict config variables to a certain skin (if needed)

Example[edit]

Register the configuration variables from the hook:

class VisualEditorHooks {
	public static function onResourceLoaderGetConfigVars( array &$vars ) {
		global $wgVisualEditorDisableForAnons, $wgVisualEditorEnableExperimentalCode;


		$vars['wgVisualEditor'] = [
			'disableForAnons' => $wgVisualEditorDisableForAnons,
			'enableExperimentalCode' => $wgVisualEditorEnableExperimentalCode,
		];

		return true;
	}
}

Retrieve them using mw.config , like:

conf = mw.config.get( 'wgVisualEditor' );
if ( conf.disableForAnons ) {
	// Do stuff
}

See also[edit]