HTMLFormFieldWithButton.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Enables HTMLFormField elements to be build with a button.
  4. */
  5. class HTMLFormFieldWithButton extends HTMLFormField {
  6. /** @var string $mButtonClass CSS class for the button in this field */
  7. protected $mButtonClass = '';
  8. /** @var string|int $mButtonId Element ID for the button in this field */
  9. protected $mButtonId = '';
  10. /** @var string $mButtonName Name the button in this field */
  11. protected $mButtonName = '';
  12. /** @var string $mButtonType Type of the button in this field (e.g. button or submit) */
  13. protected $mButtonType = 'submit';
  14. /** @var string $mButtonType Value for the button in this field */
  15. protected $mButtonValue;
  16. /** @var string[] $mButtonType Value for the button in this field */
  17. protected $mButtonFlags = [ 'progressive' ];
  18. public function __construct( $info ) {
  19. if ( isset( $info['buttonclass'] ) ) {
  20. $this->mButtonClass = $info['buttonclass'];
  21. }
  22. if ( isset( $info['buttonid'] ) ) {
  23. $this->mButtonId = $info['buttonid'];
  24. }
  25. if ( isset( $info['buttonname'] ) ) {
  26. $this->mButtonName = $info['buttonname'];
  27. }
  28. if ( isset( $info['buttondefault'] ) ) {
  29. $this->mButtonValue = $info['buttondefault'];
  30. }
  31. if ( isset( $info['buttontype'] ) ) {
  32. $this->mButtonType = $info['buttontype'];
  33. }
  34. if ( isset( $info['buttonflags'] ) ) {
  35. $this->mButtonFlags = $info['buttonflags'];
  36. }
  37. parent::__construct( $info );
  38. }
  39. public function getInputHTML( $value ) {
  40. $attr = [
  41. 'class' => 'mw-htmlform-submit ' . $this->mButtonClass,
  42. 'id' => $this->mButtonId,
  43. ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
  44. return Html::input( $this->mButtonName, $this->mButtonValue, $this->mButtonType, $attr );
  45. }
  46. public function getInputOOUI( $value ) {
  47. return new OOUI\ButtonInputWidget( [
  48. 'name' => $this->mButtonName,
  49. 'value' => $this->mButtonValue,
  50. 'type' => $this->mButtonType,
  51. 'label' => $this->mButtonValue,
  52. 'flags' => $this->mButtonFlags,
  53. 'id' => $this->mButtonId ?: null,
  54. ] + OOUI\Element::configFromHtmlAttributes(
  55. $this->getAttributes( [ 'disabled', 'tabindex' ] )
  56. ) );
  57. }
  58. /**
  59. * Combines the passed element with a button.
  60. * @param string $element Element to combine the button with.
  61. * @return string
  62. */
  63. public function getElement( $element ) {
  64. return $element . "\u{00A0}" . $this->getInputHTML( '' );
  65. }
  66. }