Jump to content

Manual:Hooks/SkinBuildSidebar

From mediawiki.org
SkinBuildSidebar
Available from version 1.14.0
At the end of Skin::buildSidebar().
Define function:
public static function onSkinBuildSidebar( Skin $skin, &$bar ) { ... }
Attach hook: In extension.json:
{
	"Hooks": {
		"SkinBuildSidebar": "MediaWiki\\Extension\\MyExtension\\Hooks::onSkinBuildSidebar"
	}
}
Called from: File(s): skins/Skin.php
Interface: SkinBuildSidebarHook.php

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

Details

[edit]
  • $skin: Skin object
  • &$bar: Sidebar contents

Modify $bar to add or modify sidebar portlets.

Warning Warning: As of MediaWiki 1.39, injecting HTML is no longer supported with this hook.[citation needed] If you are looking to inject HTML, such as to put an ad in the sidebar, you must use Manual:Hooks/SkinAfterPortlet or similar.

Note about caching

[edit]

Consumers of this hook must be vary that the response is stored in the WAN Object Cache. At time of writing (1.43) this varies on the following criteria:

  • skin name
  • current language code

For this reason, consumers of the hook should be careful not to vary the result by any dimensions other than the above (e.g. do not have custom behaviour specific to the page title).

Consider using Manual:Hooks/SidebarBeforeOutput instead if you need the behaviour to vary.

Simple examples

[edit]
// This will create a new sidebar item and put it in 'navigation' section.
// This is usually the topmost section near the logo and is untitled.
/**
 * @param Skin $skin
 * @param array $bar
 */
$wgHooks['SkinBuildSidebar'][] = function( $skin, &$bar ) {
	$bar['navigation'][] = [
		'text'  => $skin->msg( 'wikimediashoplink-linktext' ),
		'href'  => '//shop.wikimedia.org',
		'title' => $skin->msg( 'wikimediashoplink-link-tooltip' ),
		'id'    => 'n-shoplink',
	];
};
// If you want create your own section in the sidebar, use the example below.
// You can change 'name of heading' to be the name of the section as you like.
// You can add more elements to the section by extending the [ $mylink1 ] array;
// So for example [ $mylink1, $mylink2, $mylink3  ] would add three items in the
// section. Each $mylink* must be an array in the same format as specified below
// else fatal error may occur.
/**
 * @param Skin $skin
 * @param array &$bar
 */
$wgHooks['SkinBuildSidebar'][] = function( $skin, &$bar ) {
 
      $query = [ 'action' => 'edit', 'page_id' => $page_id ];
      $link_url = SpecialPage::getTitleFor( 'TestPage' )->getLinkURL( $query );

      $mylink1 = [
           'text'   => 'TestPage',
           'href'   => $link_url,
           'id'     => 'n-login',
           'active' => ''
       ];

       $bar['name of heading'] = [ $mylink1 ];

};