Jump to content

Manual talk:Hooks/GetPreferences

Add topic
From mediawiki.org
Latest comment: 5 days ago by WikiForMen in topic Changes in REL1_35

Here's an example how to make use of a preference:

$user = $this->getUser(); 
if ( $user->getOption( 'mypref2' ) == 'choice3' ) { /* do something... */ }

Default?

[edit]

I see this line in the example for a radio button:

		'default' => 'choice1',  // A 'default' key is required!

but I think that actually one shouldn't put a 'default' key in this structure: you should leave it to the Preferences class to assign 'default' to whatever the user's preference choice is. If you provide 'default' in this way, it will always display the value you list even if the user has actually chosen a different value! Please correct me if I'm wrong about this. - Nonstandard (talk) 02:02, 21 April 2015 (UTC)Reply

Please see the example I added to the Manual!
You are right in the way, that the user's preference choice should be respected, but:
A default value is required for the case, the user still not made a choice. ;-) --WikiForMen (talk) 05:31, 16 September 2024 (UTC)Reply

Changes in REL1_35

[edit]

Things have changed with REL1_35. I do not really know, how to change the manual.

This

{{MediaWikiHook
|name = GetPreferences
|version = 1.35.0
|args = $user, &$preferences
|source = preferences/Hook/GetPreferencesHook.php
}}

doesn't result the expected:

Define function:
public function onGetPreferences( $user, &$preferences ) { ... }
Attach hook: In extension.json:
{
	"Hooks": {
		"GetPreferences": "example_tag"
	},
	"HookHandlers": {
		"example_tag": {
			"class": "MediaWiki\\Extension\\ExampleExtension\\Hooks",
			"services": [
				"MainConfig",
				"UserOptionsLookup"
			]
		}
	}
}

Someone can deal with it? --WikiForMen (talk) 05:40, 16 September 2024 (UTC)Reply