Manual:Hooks/GetPreferences/ru

From mediawiki.org
This page is a translated version of the page Manual:Hooks/GetPreferences and the translation is 35% complete.
GetPreferences
Доступен с version 1.16.0
Modify user preferences.
Определяет функцию:
public static function onGetPreferences( User $user, array &$preferences ) { ... }
Прикрепить хук: В extension.json:
{
	"Hooks": {
		"GetPreferences": "MediaWiki\\Extension\\MyExtension\\Hooks::onGetPreferences"
	}
}
Вызывается из: Файла(Файлов): preferences/DefaultPreferencesFactory.php
Interface: GetPreferencesHook.php

Для получения дополнительной информации о присоединении перехватчиков, см Руководство:Прерывания (хуки) .
Примеры расширений с использованием этого хука см. в Category:GetPreferences extensions/ru.

Использование

Параметры

Параметр/Опция Описание
$user Пользователь, чьи настройки изменяются
&$preferences Preferences description array, to be fed to an HTMLForm object

Пример

In extension.json:

"Hooks": {
  "GetPreferences": [ "MediaWiki\\Extension\\ExampleExtension\\Hooks::onGetPreferences" ]
}

In includes/Hooks.php:

namespace MediaWiki\Extension\ExampleExtension;

class Hooks {
	/**
	 * @param User $user
	 * @param array $preferences
	 */
	public static function onGetPreferences( $user, &$preferences ) {
		// A checkbox
		$preferences['mypref'] = [
			'type' => 'toggle',
			'label-message' => 'tog-mypref', // a system message
			'section' => 'personal/info',
		];

		// A set of radio buttons. Notice that in the 'options' array,
		// the keys are the text (not system messages), and the values are the HTML values.
		// They keys/values might be the opposite of what you expect. PHP's array_flip()
		// can be helpful here.
		$preferences['mypref2'] = [
			'type' => 'radio',
			'label-message' => 'tog-mypref2', // a system message
			'section' => 'personal/info',
			// Array of options. Key = text to display. Value = HTML <option> value.
			'options' => [
				'Pick me please' => 'choice1',
				'No, pick me!' => 'choice2',
				'Seriously, pick me right now' => 'choice3',
			],
			'default' => 'choice1',  // A 'default' key is required!
			'help-message' => 'tog-help-mypref2', // a system message (optional)
		];
	}
}

Tabs and sections

The section array key specifies which tab and section of Preferences contains your preferences. If your section value is foo/bar, this means your preference will appear on the foo tab (named by system message prefs-foo) within the bar section (named by system message prefs-bar). If no such tab or section exists, it is created automatically.

List of default tabs

Identifier Displays as
personal Личные данные
rendering Внешний вид
editing Редактирование
rc Свежие правки
watchlist Список наблюдения
misc Другие настройки

Поддерживаемые типы

Visible types

The type can take on various values found in the HTMLForm::$typeMappings array in the file includes/htmlform/HTMLForm.php, including info, multiselect, radio, etc.

Most preferences are stored in the same format as is used by the HTMLFormField, but in the case of 'type' => 'usersmultiselect' a transformation should be carried out from a newline-separated list of usernames (which is what the form widget works with) and a newline-separated list of user IDs (which is what gets stored in the database). See the treatment of email-blacklist (in core) or echo-notifications-blacklist (in Echo ) for examples of this.

Floats

For float types, you can set min and max, which will be validated on save.

Настройки API

API preferences use type api. They are not displayed in Special:Preferences. They are usually set via custom front-end interfaces that call the API.

Note that you should not use 'type' => 'hidden' for API preferences (that type exists for HTML forms, not preferences).

Настройки по умолчанию

To set the default value for a preference (i.e. the value that is set for a new user that hasn't customized their preferences yet), add the setting to the $wgDefaultUserOptions global variable. Use the same key name as you use for $preferences in the hook.

Alternatively, if you're writing an extension, you can add to the DefaultUserOptions section of the file extensions.json.