6 * Multiple checkbox input widget. Intended to be used within a OO.ui.FormLayout.
8 class CheckboxMultiselectInputWidget extends InputWidget {
15 protected $name = null;
22 protected $value = [];
25 * Layouts for this input, as FieldLayouts.
29 protected $fields = [];
32 * @param array $config Configuration options
33 * @param array[] $config['options'] Array of menu options in the format
34 * `[ 'data' => …, 'label' => …, 'disabled' => … ]`
36 public function __construct( array $config = [] ) {
38 parent::__construct( $config );
40 if ( isset( $config['name'] ) ) {
41 $this->name = $config['name'];
45 $this->setOptions( isset( $config['options'] ) ? $config['options'] : [] );
46 // Have to repeat this from parent, as we need options to be set up for this to make sense
47 $this->setValue( isset( $config['value'] ) ? $config['value'] : null );
48 $this->addClasses( [ 'oo-ui-checkboxMultiselectInputWidget' ] );
51 protected function getInputElement( $config ) {
53 return new Tag( 'unused' );
57 * Set the value of the input.
59 * @param string[] $value New value
62 public function setValue( $value ) {
63 $this->value = $this->cleanUpValue( $value );
64 // Deselect all options
65 foreach ( $this->fields as $field ) {
66 $field->getField()->setSelected( false );
68 // Select the requested ones
69 foreach ( $this->value as $key ) {
70 $this->fields[ $key ]->getField()->setSelected( true );
76 * Clean up incoming value.
78 * @param string[] $value Original value
79 * @return string[] Cleaned up value
81 protected function cleanUpValue( $value ) {
83 if ( !is_array( $value ) ) {
86 foreach ( $value as $singleValue ) {
87 $singleValue = parent::cleanUpValue( $singleValue );
88 // Remove options that we don't have here
89 if ( !isset( $this->fields[ $singleValue ] ) ) {
92 $cleanValue[] = $singleValue;
98 * Set the options available for this input.
100 * @param array[] $options Array of menu options in the format
101 * `[ 'data' => …, 'label' => …, 'disabled' => … ]`
104 public function setOptions( $options ) {
107 // Rebuild the checkboxes
108 $this->clearContent();
110 foreach ( $options as $opt ) {
111 $optValue = parent::cleanUpValue( $opt['data'] );
112 $optDisabled = isset( $opt['disabled'] ) ? $opt['disabled'] : false;
113 $field = new FieldLayout(
114 new CheckboxInputWidget( [
116 'value' => $optValue,
117 'disabled' => $this->isDisabled() || $optDisabled,
120 'label' => isset( $opt['label'] ) ? $opt['label'] : $optValue,
125 $this->fields[ $optValue ] = $field;
126 $this->appendContent( $field );
129 // Re-set the value, checking the checkboxes as needed.
130 // This will also get rid of any stale options that we just removed.
131 $this->setValue( $this->getValue() );
136 public function setDisabled( $state ) {
137 parent::setDisabled( $state );
138 foreach ( $this->fields as $field ) {
139 $field->getField()->setDisabled( $this->isDisabled() );
144 public function getConfig( &$config ) {
146 foreach ( $this->fields as $field ) {
147 $label = $field->getLabel();
148 $data = $field->getField()->getValue();
149 $disabled = $field->getField()->isDisabled();
150 $o[] = [ 'data' => $data, 'label' => $label, 'disabled' => $disabled ];
152 $config['options'] = $o;
153 return parent::getConfig( $config );