Source of file DependentListboxField.php
Size: 2,139 Bytes - Last Modified: 2021-12-23T10:58:07+00:00
/var/www/docs.ssmods.com/process/src/code/DependentListboxField.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 | <?php /** * A listbox that depends on another dropdown for populating values, and calls * a callback when that dropdown is updated. * * @package callbackdropdownfield * * @property FormField $depends * @property callable $source */ class DependentListboxField extends ListboxField { private static $allowed_actions = array( 'load' ); protected $depends; protected $unselected; public function load(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); $response->setBody(Convert::array2json(call_user_func( $this->source, $request->getVar('val') ))); return $response; } public function getDepends() { return $this->depends; } public function setDepends(FormField $field) { $this->depends = $field; return $this; } public function getUnselectedString() { return $this->unselected; } public function setUnselectedString($string) { $this->unselected = $string; return $this; } public function setSource($source) { $this->source = $source; return $this; } public function getSource() { if(!is_callable($this->source)) { return parent::getSource(); } if(!$val = $this->depends->Value()) { $source = array(); } else { $source = call_user_func($this->source, $val); } if($this->getHasEmptyDefault()) { return array('' => $this->getEmptyString()) + (array) $source; } else { return $source; } } public function Field($properties = array()) { Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript(DEPENDENTDROPDOWNFIELD . '/javascript/dependentlistboxfield.js'); $this->addExtraClass('dependent-listbox'); $this->addExtraClass('listbox'); $this->setAttribute('data-link', $this->Link('load')); $this->setAttribute('data-depends', $this->getDepends()->getName()); $this->setAttribute('data-empty', $this->getEmptyString()); $this->setAttribute('data-unselected', $this->getUnselectedString()); return parent::Field($properties); } } |