wiki.techinc.nl/includes/htmlform/fields/HTMLRadioField.php
Bartosz Dziewoński f2a83fe4ac HTMLRadioField: Do not automatically infuse our RadioSelectInputWidgets
This is really a workaround for an issue in a completely different place:
JS RadioSelectInputWidget internally uses `<input type="hidden">`
rather than real radio buttons, which does not work correctly with the
code in mediawiki.special.preferences.confirmClose.js. Ideally we would
change RadioSelectInputWidget to not do such weird things.

However, I think this is actually a good thing to do in general.
From the user's perspective, PHP RadioSelectInputWidget and JS
RadioSelectInputWidget look and behave the same, so there's no reason
to infuse and rebuild them.

This behavior was implemented in f50cee1375
in which unfortunately I did not document the reason for it. For other
fields it makes obvious sense (the JS widgets have improvements like
autocompletion, or at least look "pretty"), but I have no idea why
I did it for this one.

Bug: T180643
Change-Id: I53e50f8cda39466b2396b374e642c154487888bb
2017-11-16 17:33:03 +01:00

111 lines
3 KiB
PHP

<?php
/**
* Radio checkbox fields.
*/
class HTMLRadioField extends HTMLFormField {
/**
* @param array $params
* In adition to the usual HTMLFormField parameters, this can take the following fields:
* - flatlist: If given, the options will be displayed on a single line (wrapping to following
* lines if necessary), rather than each one on a line of its own. This is desirable mostly
* for very short lists of concisely labelled options.
*/
public function __construct( $params ) {
parent::__construct( $params );
if ( isset( $params['flatlist'] ) ) {
$this->mClass .= ' mw-htmlform-flatlist';
}
}
public function validate( $value, $alldata ) {
$p = parent::validate( $value, $alldata );
if ( $p !== true ) {
return $p;
}
if ( !is_string( $value ) && !is_int( $value ) ) {
return $this->msg( 'htmlform-required' );
}
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
if ( in_array( strval( $value ), $validOptions, true ) ) {
return true;
} else {
return $this->msg( 'htmlform-select-badoption' );
}
}
/**
* This returns a block of all the radio options, in one cell.
* @see includes/HTMLFormField#getInputHTML()
*
* @param string $value
*
* @return string
*/
public function getInputHTML( $value ) {
$html = $this->formatOptions( $this->getOptions(), strval( $value ) );
return $html;
}
public function getInputOOUI( $value ) {
$options = [];
foreach ( $this->getOptions() as $label => $data ) {
$options[] = [
'data' => $data,
'label' => $this->mOptionsLabelsNotFromMessage ? new OOUI\HtmlSnippet( $label ) : $label,
];
}
return new OOUI\RadioSelectInputWidget( [
'name' => $this->mName,
'id' => $this->mID,
'value' => $value,
'options' => $options,
] + OOUI\Element::configFromHtmlAttributes(
$this->getAttributes( [ 'disabled', 'tabindex' ] )
) );
}
public function formatOptions( $options, $value ) {
global $wgUseMediaWikiUIEverywhere;
$html = '';
$attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
$elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
# @todo Should this produce an unordered list perhaps?
foreach ( $options as $label => $info ) {
if ( is_array( $info ) ) {
$html .= Html::rawElement( 'h1', [], $label ) . "\n";
$html .= $this->formatOptions( $info, $value );
} else {
$id = Sanitizer::escapeIdForAttribute( $this->mID . "-$info" );
$classes = [ 'mw-htmlform-flatlist-item' ];
if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
$classes[] = 'mw-ui-radio';
}
$radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + [ 'id' => $id ] );
$radio .= '&#160;' . call_user_func( $elementFunc, 'label', [ 'for' => $id ], $label );
$html .= ' ' . Html::rawElement(
'div',
[ 'class' => $classes ],
$radio
);
}
}
return $html;
}
protected function needsLabel() {
return false;
}
}