From b28b490df46bf47aeb6d64bfbb8570f8e871f341 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 19 Jan 2016 10:00:27 +0100 Subject: [PATCH] Add addSelectFromCollection --- .../lib/formvalidator/FormValidator.class.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main/inc/lib/formvalidator/FormValidator.class.php b/main/inc/lib/formvalidator/FormValidator.class.php index 1fd857d70a..839e082cd9 100755 --- a/main/inc/lib/formvalidator/FormValidator.class.php +++ b/main/inc/lib/formvalidator/FormValidator.class.php @@ -713,6 +713,36 @@ EOT; return $this->addElement('select', $name, $label, $options, $attributes); } + /** + * @param $name + * @param $label + * @param $collection + * @param array $attributes + * @param bool $addNoneOption + * @param string $textCallable set a function getStringValue() by default __toString() + * + * @return HTML_QuickForm_element + */ + public function addSelectFromCollection($name, $label, $collection, $attributes = array(), $addNoneOption = false, $textCallable = '') + { + $options = []; + + if ($addNoneOption) { + $options[0] = get_lang('None'); + } + + if (!empty($collection)) { + foreach ($collection as $item) { + $text = $item; + if (!empty($textCallable)) { + $text = $item->$textCallable(); + } + $options[$item->getId()] = $text; + } + } + return $this->addElement('select', $name, $label, $options, $attributes); + } + /** * @param string $label * @param string $text