Manual:Hooks/SidebarBeforeOutput

From Linux Web Expert

SidebarBeforeOutput
<translate> Available from <tvar name=1><translate> version <tvar </tvar></translate>
Called at the end of Skin::buildSidebar(), done to be used in order to alter the sidebar content just before the display
<translate> Define function:</translate>
public static function onSidebarBeforeOutput( Skin $skin, &$sidebar ) { ... }
<translate> Attach hook:</translate> <translate> In <tvar name=1>extension.json</tvar>:</translate>
{
	"Hooks": {
		"SidebarBeforeOutput": "MediaWiki\\Extension\\MyExtension\\Hooks::onSidebarBeforeOutput"
	}
}
<translate> Called from:</translate> <translate> File(s):</translate> skins/Skin.php
<translate> Interface:</translate> SidebarBeforeOutputHook.php

<translate> For more information about attaching hooks, see <tvar name=1>Manual:Hooks </tvar>.</translate>
<translate> For examples of extensions using this hook, see <tvar name=cat>Category:SidebarBeforeOutput extensions</tvar>.</translate>


Details

  • $skin: Skin object
  • &$sidebar: Sidebar contents

Modify $sidebar to add or modify sidebar portlets.

Warning: You should probably use SkinBuildSidebar in order to use the caching system. This hook is run on each display and should be used if the contents of the sidebar vary on a per-request basis (e.g. Extension:DynamicSidebar).

Caveats

Currently languages and toolbox menus are available for modification in this hook, however whether they display or not will be determined by the skin that is rendering.

More information can be found in https://phabricator.wikimedia.org/T253783.

Examples

The following example allows you to add a new menu to the sidebar:

$wgHooks['SidebarBeforeOutput'][] = function ( $skin, &$bar ) {
	# note: heading can be either a message key. If no message exists it will be treated as a plain text.
	$heading = 'msg-heading';
	$bar[ $heading ] = [
		[
			'msg' => 'my-label-message',
			'href' => '//example.com'
		]
	];
};

To add a new menu

$wgHooks['SidebarBeforeOutput'][] = function ( Skin $skin, &$sidebar ) {
	foreach ( $sidebar as $key => $data ) {
		$sidebar[$key][] = [
			"text" => "SidebarBeforeOutput link",
			"href" => '#SidebarBeforeOutputLocalSettings.php',
		];
	}
};

If you want append a single item in the default 'toolbox' section, use 'TOOLBOX' string as the array key

$wgHooks['SidebarBeforeOutput'][] = function ( Skin $skin, &$sidebar ) {
	$sidebar['TOOLBOX'][] = [
		"text" => "My custom label",
		"href" => '#SidebarBeforeOutputLocalSettings.php',
	];
};