Manual:Hooks/SkinBuildSidebar

From Linux Web Expert

SkinBuildSidebar
<translate> Available from <tvar name=1><translate> version <tvar </tvar></translate>
At the end of Skin::buildSidebar().
<translate> Define function:</translate>
public static function onSkinBuildSidebar( Skin $skin, &$bar ) { ... }
<translate> Attach hook:</translate> <translate> In <tvar name=1>extension.json</tvar>:</translate>
{
	"Hooks": {
		"SkinBuildSidebar": "MediaWiki\\Extension\\MyExtension\\Hooks::onSkinBuildSidebar"
	}
}
<translate> Called from:</translate> <translate> File(s):</translate> skins/Skin.php
<translate> Interface:</translate> SkinBuildSidebarHook.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:SkinBuildSidebar extensions</tvar>.</translate>

Details

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

Modify $bar to add or modify sidebar portlets.

File:OOjs UI icon notice-destructive.svg <translate> Warning:</translate> As of MediaWiki 1.39, injecting HTML is no longer supported with this hook. If you are looking to inject HTML, such as to put an ad in the sidebar, you must use Manual:Hooks/SkinAfterPortlet or similar.

Simple examples

// 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 ];

};