User:Austin J. Che/Extensions/PreferencesExtension

From OpenWetWare
Jump to navigationJump to search

This extension hooks into the default Special:Preferences page and allows other extensions to easily add new preferences.


A typical usage is in your extension function to call wfAddPreferences with an array of new preferences to be added. Each preference is specified as another associative array. The valid keys for this associative array are (everything is optional):

  • name: the name of the preference (the name for the form element)
  • section: the name of the section in the preferences dialog. This should be the internal name used not the displayed name (e.g. 'prefs-misc'). To create a new section, simply use a different name. The actual display text will be whatever is returned using wfMsg(section) so you can set the text by adding the appropriate value to the message cache.
  • type: The type of form element being added. The valid types are the constants PREF_USER_T, PREF_TOGGLE_T, PREF_TEXT_T, PREF_PASSWORD_T, PREF_INT_T, PREF_DROPDOWN_T. The default is PREF_USER_T.
  • size: For type=PREF_TEXT_T, PREF_PASSWORD_T, and PREF_INT_T, defines the size of the textbox
  • html: For PREF_USER_T, allows specification of the complete html to be inserted into the preferences form. The string @VALUE@ inside this html will be expanded to the current value of this element.
  • caption: Specifies the caption for the label for PREF_INT_T, PREF_TEXT_T, PREF_PASSWORD_T, PREF_DROPDOWN_T. If not given, uses wfMsg(name).
  • pos: For PREF_INT_T, PREF_TEXT_T, PREF_PASSWORD_T, PREF_DROPDOWN_T, specifies the position in a table. It can be 'first', 'last', or empty.
  • min: Defines the minimum allowed integer value for PREF_INT_T types
  • max: Defines the maximum allowed integer value for PREF_INT_T types
  • validate: The name of a function. If defined, for PREF_TEXT_T, PREF_PASSWORD_T, and PREF_USER_T, this function will be called to validate the form value on submission. The function should take one argument (the form value) and return the value that should be used as the submitted value.
  • save: The name of a function. If defined, this function will be called to set the element's value instead of using the default of setting the value via $wgUser->setOption. The function takes two arguments ($name, $value).
  • load: The name of a function. If defined, this function will be called to load an element's value instead of the default using $wgUser->getOption. This function takes one argument $name and should return the value.
  • default: The default value to use for this element in displaying the form.
  • onchange: For PREF_DROPDOWN_T, provides an onchange handler
  • options: For PREF_DROPDOWN_T, should be an array containing the dropdown options

See the source of UserDefaultPage for a simple example of using this extension.


<syntax type="php" file=""></syntax>