From 35c7b784c7ead1b8a962791a122daa33e0dd489e Mon Sep 17 00:00:00 2001 From: chdemko Date: Wed, 6 Jun 2012 23:11:50 +0200 Subject: [PATCH] Add "preset" attribute to form fields and adapt list/checkboxes multiple default/preset values --- libraries/joomla/form/fields/checkboxes.php | 50 ++- libraries/joomla/form/fields/list.php | 64 +++- libraries/joomla/form/form.php | 56 +++- .../unit/joomla/form/JFormDataHelper.php | 5 + tests/suites/unit/joomla/form/JFormTest.php | 49 +++ .../form/fields/JFormFieldCheckboxesTest.php | 123 ++++++++ .../joomla/form/fields/JFormFieldListTest.php | 289 ++++++++++++++++++ 7 files changed, 631 insertions(+), 5 deletions(-) diff --git a/libraries/joomla/form/fields/checkboxes.php b/libraries/joomla/form/fields/checkboxes.php index f45679088d..ba52a91b2f 100644 --- a/libraries/joomla/form/fields/checkboxes.php +++ b/libraries/joomla/form/fields/checkboxes.php @@ -58,13 +58,55 @@ protected function getInput() // Get the field options. $options = $this->getOptions(); + // Get the value(s) + if (empty($this->value)) + { + // Filter the option using the preset flag + $filter = array_filter( + $options, + function($option) + { + return $option->preset; + } + ); + + // If the data exists or there is no preset + if ($this->form->existValue($this->fieldname, $this->group) || empty($filter)) + { + $filter = array_filter( + $options, + function($option) + { + return $option->default; + } + ); + } + + // Extract the values from the filter + $values = array_map( + function ($option) + { + return $option->value; + }, + $filter + ); + } + elseif (is_array($this->value)) + { + $values = $this->value; + } + else + { + $values = array($this->value); + } + // Build the checkbox field output. $html[] = '