Merge "HTMLForm: Dispatch field formatting explicitly instead of guessing method names"

This commit is contained in:
jenkins-bot 2023-07-11 11:47:02 +00:00 committed by Gerrit Code Review
commit b888daea79
4 changed files with 37 additions and 23 deletions

View file

@ -29,15 +29,8 @@ use MediaWiki\Html\Html;
* @since 1.41
*/
class CodexHTMLForm extends HTMLForm {
/**
* Symbolic display format name.
* Note this is `vform` rather than `div`
* to avoid the div.mw-label wrapper
* on labels.
*
* @var string
*/
protected $displayFormat = 'vform';
protected $displayFormat = 'codex';
public static function loadInputFromParameters( $fieldname, $descriptor,
HTMLForm $parent = null
@ -55,6 +48,14 @@ class CodexHTMLForm extends HTMLForm {
return parent::getHTML( $submitResult );
}
/**
* @inheritDoc
*/
protected function formatField( HTMLFormField $field, $value ) {
// The "cdx-..." classes are added magically in the Html class. :(
return $field->getVForm( $value );
}
protected function getFormAttributes() {
$attribs = parent::getFormAttributes();
$attribs['class'] = [ 'mw-htmlform', 'mw-htmlform-codex' ];

View file

@ -2013,12 +2013,18 @@ class HTMLForm extends ContextSource {
*/
protected function formatField( HTMLFormField $field, $value ) {
$displayFormat = $this->getDisplayFormat();
// Conveniently, PHP method names are case-insensitive.
// For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
$getFieldHtmlMethod = $displayFormat === 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
return $field->$getFieldHtmlMethod( $value );
switch ( $displayFormat ) {
case 'table':
return $field->getTableRow( $value );
case 'div':
return $field->getDiv( $value );
case 'raw':
return $field->getRaw( $value );
case 'inline':
return $field->getInline( $value );
default:
throw new LogicException( 'Not implemented' );
}
}
/**

View file

@ -43,10 +43,6 @@ class OOUIHTMLForm extends HTMLForm {
$this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
}
/**
* Symbolic display format name.
* @var string
*/
protected $displayFormat = 'ooui';
public static function loadInputFromParameters( $fieldname, $descriptor,
@ -172,6 +168,14 @@ class OOUIHTMLForm extends HTMLForm {
return $layout;
}
/**
* @inheritDoc
* @return OOUI\FieldLayout HTML
*/
protected function formatField( HTMLFormField $field, $value ) {
return $field->getOOUI( $value );
}
/**
* Put a form section together from the individual fields' HTML, merging it and wrapping.
* @param OOUI\FieldLayout[] $fieldsHtml Array of outputs from formatField()

View file

@ -35,10 +35,6 @@ class VFormHTMLForm extends HTMLForm {
*/
protected $mWrapperLegend = false;
/**
* Symbolic display format name.
* @var string
*/
protected $displayFormat = 'vform';
public static function loadInputFromParameters( $fieldname, $descriptor,
@ -64,6 +60,13 @@ class VFormHTMLForm extends HTMLForm {
return parent::getHTML( $submitResult );
}
/**
* @inheritDoc
*/
protected function formatField( HTMLFormField $field, $value ) {
return $field->getVForm( $value );
}
protected function getFormAttributes() {
return [ 'class' => [ 'mw-htmlform', 'mw-ui-vform', 'mw-ui-container' ] ] +
parent::getFormAttributes();