Manual:Hooks/SidebarBeforeOutput

SidebarBeforeOutput
Available from version 1.24.0
Called at the end of Skin::buildSidebar(), done to be used in order to alter the sidebar content just before the display
Define function:
public static function onSidebarBeforeOutput( Skin $skin, &$sidebar ) { ... }
Attach hook:

In extension.json:

{
	"Hooks": {
		"SidebarBeforeOutput": "MyExtensionHooks::onSidebarBeforeOutput"
	}
}
Called from:File(s): Skin.php
Interface:SidebarBeforeOutputHook.php

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

DetailsEdit

  • $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).

CaveatsEdit

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.

ExamplesEdit

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',
	];
};