MediaWiki master
HTMLSelectLanguageField.php
Go to the documentation of this file.
1<?php
2
4
9
16 private bool $useCodex = false;
18 private array $languages = [];
19
24 public function __construct( $params ) {
25 $this->useCodex = $params['useCodex'] ?? false;
26 parent::__construct( $params );
27
28 if ( $this->mParent instanceof HTMLForm ) {
29 $config = $this->mParent->getConfig();
30 $languageCode = $config->get( MainConfigNames::LanguageCode );
31 } else {
32 $languageCode = MediaWikiServices::getInstance()->getMainConfig()->get(
34 }
35
36 // Use provided languages array if given, otherwise fetch all languages
37 if ( isset( $params['languages'] ) && is_array( $params['languages'] ) ) {
38 $this->languages = $params['languages'];
39 } else {
40 $this->languages = MediaWikiServices::getInstance()
41 ->getLanguageNameUtils()
42 ->getLanguageNames();
43 }
44
45 // Make sure the site language is in the list;
46 // a custom language code might not have a defined name…
47 if ( !array_key_exists( $languageCode, $this->languages ) ) {
48 $this->languages[$languageCode] = $languageCode;
49 }
50
51 ksort( $this->languages );
52
53 foreach ( $this->languages as $code => $name ) {
54 $this->mParams['options'][$code . ' - ' . $name] = $code;
55 }
56
57 $this->mParams['default'] ??= $languageCode;
58 }
59
64 public function getInputOOUI( $value ) {
65 if ( $this->useCodex ) {
66 return $this->getInputCodex( $value, false );
67 }
68 return parent::getInputOOUI( $value );
69 }
70
75 public function getInputCodex( $value, $hasErrors ) {
76 // Add module for language selector widget
77 $this->mParent->getOutput()->addModules( 'mediawiki.widgets.LanguageSelectWidget' );
78
79 $standardAttribs = $this->getAttributes( [ 'disabled', 'required', 'multiple' ] );
80
81 $widget = new LanguageSelectWidget( [
82 'languages' => $this->languages,
83 'name' => $this->mName,
84 'value' => $value !== null && $value !== '' ? $value : ( $this->mParams['default'] ?? null ),
85 'id' => $this->mID,
86 'cssclass' => trim( $this->mClass . ' cdx-text-input__input' ),
87 ] + $standardAttribs );
88
89 return $widget->toString();
90 }
91}
92
94class_alias( HTMLSelectLanguageField::class, 'HTMLSelectLanguageField' );
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
foreach( $this->languages as $code=> $name) $this mParams['default']
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
__construct( $params)
Initialise the object.
getAttributes(array $list)
Returns the given attributes from the parameters.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
A class containing constants representing the names of configuration variables.
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Language Select Widget is a reusable component in PHP and JS.
toString()
Get the HTML output for the widget.