2022-03-08 15:55:41 +01:00
|
|
|
<?php namespace ProcessWire;
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Select Multiple Inputfield
|
|
|
|
*
|
|
|
|
* An Inputfield for handling multiple selection using HTML `<select multiple>`.
|
|
|
|
* Also a base type for other multiple selection types (checkboxes, asmSelect, etc.)
|
|
|
|
*
|
|
|
|
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
|
|
|
|
* https://processwire.com
|
|
|
|
*
|
|
|
|
* @property int $size
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-03-08 15:55:41 +01:00
|
|
|
class InputfieldSelectMultiple extends InputfieldSelect implements InputfieldHasArrayValue {
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Default 'size' attribute value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const defaultSize = 10;
|
2022-03-08 15:55:41 +01:00
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Get module info
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public static function getModuleInfo() {
|
|
|
|
return array(
|
|
|
|
'title' => __('Select Multiple', __FILE__), // Module Title
|
|
|
|
'summary' => __('Select multiple items from a list', __FILE__), // Module Summary
|
|
|
|
'version' => 101,
|
|
|
|
'permanent' => true,
|
2022-11-05 18:32:48 +01:00
|
|
|
);
|
2022-03-08 15:55:41 +01:00
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Construct
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
$this->setAttribute('multiple', 'multiple');
|
|
|
|
$this->setAttribute('size', self::defaultSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add options only if they are non-blank
|
|
|
|
*
|
|
|
|
* We don't need blank options in a select multiple since the unselected state involves no selected options
|
2022-11-05 18:32:48 +01:00
|
|
|
*
|
|
|
|
* @param string|int $value
|
|
|
|
* @param string|null $label
|
|
|
|
* @param array|null $attributes
|
|
|
|
* @return InputfieldSelect|InputfieldSelectMultiple|self
|
2022-03-08 15:55:41 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function addOption($value, $label = null, array $attributes = null) {
|
|
|
|
if(is_null($value) || (is_string($value) && !strlen($value))) return $this;
|
|
|
|
return parent::addOption($value, $label, $attributes);
|
|
|
|
}
|
|
|
|
|
2022-11-05 18:32:48 +01:00
|
|
|
/**
|
|
|
|
* Configure Inputfield
|
|
|
|
*
|
|
|
|
* @return InputfieldWrapper
|
|
|
|
*
|
|
|
|
*/
|
2022-03-08 15:55:41 +01:00
|
|
|
public function ___getConfigInputfields() {
|
|
|
|
$inputfields = parent::___getConfigInputfields();
|
2022-11-05 18:32:48 +01:00
|
|
|
if($this->className() === 'InputfieldSelectMultiple') {
|
2022-03-08 15:55:41 +01:00
|
|
|
// descending classes may null out the 'size' attribute if they don't need it
|
2022-11-05 18:32:48 +01:00
|
|
|
/** @var InputfieldInteger $f */
|
|
|
|
$f = $this->wire()->modules->get('InputfieldInteger');
|
2022-03-08 15:55:41 +01:00
|
|
|
$f->label = $this->_('Size: number of rows visible at once in the select multiple');
|
|
|
|
$f->attr('name', 'size');
|
|
|
|
$f->attr('value', (int) $this->attr('size'));
|
|
|
|
$inputfields->add($f);
|
|
|
|
}
|
|
|
|
return $inputfields;
|
|
|
|
}
|
|
|
|
}
|