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[] = '
';
foreach ($options as $i => $option)
{
// Initialize some option attributes.
- $checked = (in_array((string) $option->value, (array) $this->value) ? ' checked="checked"' : '');
+ $checked = (in_array((string) $option->value, $values) ? ' checked="checked"' : '');
$class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
$disabled = !empty($option->disable) ? ' disabled="disabled"' : '';
@@ -119,6 +161,12 @@ protected function getOptions()
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
+ // Set the default attribute.
+ $tmp->default = (string) $option['default'] == 'true';
+
+ // Set the preset attribute.
+ $tmp->preset = (string) $option['preset'] == 'true';
+
// Add the option object to the result set.
$options[] = $tmp;
}
diff --git a/libraries/joomla/form/fields/list.php b/libraries/joomla/form/fields/list.php
index b770c9aa4d..e8698f129f 100644
--- a/libraries/joomla/form/fields/list.php
+++ b/libraries/joomla/form/fields/list.php
@@ -59,16 +59,68 @@ protected function getInput()
// Get the field options.
$options = (array) $this->getOptions();
+ // Get the value(s)
+ if ($this->multiple)
+ {
+ 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);
+ }
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
- $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
- $html[] = '';
+ $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
+ foreach ($values as $value)
+ {
+ $html[] = '';
+ }
}
// Create a regular list.
else
{
- $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
+ $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $values, $this->id);
}
return implode($html);
@@ -108,6 +160,12 @@ protected function getOptions()
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
+ // Set the default attribute.
+ $tmp->default = (string) $option['default'] == 'true';
+
+ // Set the preset attribute.
+ $tmp->preset = (string) $option['preset'] == 'true';
+
// Add the option object to the result set.
$options[] = $tmp;
}
diff --git a/libraries/joomla/form/form.php b/libraries/joomla/form/form.php
index d9656f67c6..d87d8de172 100644
--- a/libraries/joomla/form/form.php
+++ b/libraries/joomla/form/form.php
@@ -621,6 +621,31 @@ public function getValue($name, $group = null, $default = null)
return $return;
}
+ /**
+ * Method to test if the value of a field exists.
+ *
+ * @param string $name The name of the field for which to get the value.
+ * @param string $group The optional dot-separated form group path on which to get the value.
+ *
+ * @return bool TRUE on success, FALSE on failure.
+ *
+ * @since 12.2
+ */
+ public function existValue($name, $group = null)
+ {
+ // If a group is set use it.
+ if ($group)
+ {
+ $return = $this->data->exists($group . '.' . $name);
+ }
+ else
+ {
+ $return = $this->data->exists($name);
+ }
+
+ return $return;
+ }
+
/**
* Method to load the form description from an XML string or object.
*
@@ -1667,6 +1692,9 @@ protected function loadField($element, $group = null, $value = null)
return false;
}
+ // Get the field name.
+ $name = (string) $element['name'];
+
// Get the field type.
$type = $element['type'] ? (string) $element['type'] : 'text';
@@ -1687,6 +1715,7 @@ protected function loadField($element, $group = null, $value = null)
*/
if ($value === null)
{
+ // Get the default value
$default = (string) $element['default'];
if (($translate = $element['translate_default']) && ((string) $translate == 'true' || (string) $translate == '1'))
{
@@ -1702,7 +1731,32 @@ protected function loadField($element, $group = null, $value = null)
$default = JText::_($default);
}
}
- $value = $this->getValue((string) $element['name'], $group, $default);
+
+ // Get the value
+ if ($this->existValue($name, $group) || !isset($element['preset']))
+ {
+ $value = $this->getValue($name, $group, $default);
+ }
+ else
+ {
+ // Get the preset value
+ $preset = (string) $element['preset'];
+ if (($translate = $element['translate_preset']) && ((string) $translate == 'true' || (string) $translate == '1'))
+ {
+ $lang = JFactory::getLanguage();
+ if ($lang->hasKey($default))
+ {
+ $debug = $lang->setDebug(false);
+ $preset = JText::_($preset);
+ $lang->setDebug($debug);
+ }
+ else
+ {
+ $preset = JText::_($preset);
+ }
+ }
+ $value = $this->getValue((string) $element['name'], $group, $preset);
+ }
}
// Setup the JFormField object.
diff --git a/tests/suites/unit/joomla/form/JFormDataHelper.php b/tests/suites/unit/joomla/form/JFormDataHelper.php
index 147df80ec1..d4e98dbbfc 100644
--- a/tests/suites/unit/joomla/form/JFormDataHelper.php
+++ b/tests/suites/unit/joomla/form/JFormDataHelper.php
@@ -249,6 +249,11 @@ class JFormDataHelper
name="title"
type="text"
description="The title." />
+
assertThat(
+ $form->getField('alias')->value,
+ $this->equalTo('preset-alias'),
+ 'Line:'.__LINE__.' Prior to binding data, the presets in the field should be used.'
+ );
+
// Check values after binding.
$data = array(
'title' => 'The title',
+ 'alias' => '',
'show_title' => 3,
'params' => array(
'show_title' => 2,
@@ -836,6 +843,12 @@ public function testGetField()
'Line:'.__LINE__.' Check the field value bound correctly.'
);
+ $this->assertThat(
+ $form->getField('alias')->value,
+ $this->equalTo(''),
+ 'Line:'.__LINE__.' Check the field value bound correctly.'
+ );
+
$this->assertThat(
$form->getField('show_title', 'params')->value,
$this->equalTo(2),
@@ -1137,6 +1150,42 @@ public function testGetValue()
);
}
+ /**
+ * Test for JForm::existValue method.
+ */
+ public function testExistValue()
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load(JFormDataHelper::$loadFieldDocument),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->existValue('title'),
+ $this->equalTo(false),
+ 'Line:'.__LINE__.' The value should not exist.'
+ );
+
+ $data = array(
+ 'title' => 'Avatar',
+ );
+
+ $this->assertThat(
+ $form->bind($data),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->existValue('title'),
+ $this->equalTo(true),
+ 'Line:'.__LINE__.' The value should exist.'
+ );
+ }
+
/**
* Test for JForm::getFieldset method.
*/
diff --git a/tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php b/tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php
index b82f352544..0274d365d1 100644
--- a/tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php
+++ b/tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php
@@ -48,4 +48,127 @@ public function testGetInput()
// TODO: Should check all the attributes have come in properly.
}
+
+ /**
+ * Data provider for testGetInputDefaultPreset
+ */
+ public function caseGetInputDefaultPreset()
+ {
+ return array(
+ // no default, no preset, existing value
+ array(
+ '',
+ '',
+ '1',
+ '',
+ '',
+ ),
+
+ // one default, no preset, existing value
+ array(
+ 'default="1"',
+ '',
+ '2',
+ '',
+ '',
+ ),
+
+ // one default, no preset, empty value
+ array(
+ 'default="1"',
+ '',
+ '',
+ '',
+ '',
+ ),
+
+ // no default, one preset, existing value
+ array(
+ 'preset="1"',
+ '',
+ '2',
+ '',
+ '',
+ ),
+
+ // one default, one preset, empty value
+ array(
+ 'preset="1" default="2"',
+ '',
+ '',
+ '',
+ '',
+ ),
+
+ // one default, one preset, existing value
+ array(
+ 'preset="1" default="2"',
+ '',
+ '3',
+ '',
+ '',
+ ),
+
+ // many default, many preset, empty value
+ array(
+ '',
+ '',
+ '',
+ '',
+ '',
+ ),
+
+ // many default, many preset, existing value
+ array(
+ '',
+ '',
+ '2',
+ '',
+ '',
+ ),
+
+ // many default, many preset, existing value
+ array(
+ '',
+ '',
+ array('2', '3'),
+ '',
+ '',
+ ),
+ );
+ }
+
+ /**
+ * Test the getInput method.
+ *
+ * @dataProvider caseGetInputDefaultPreset
+ */
+ public function testGetInputDefaultPreset($attributes, $options, $bind, $before, $after)
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load(''),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('checkboxes')->input,
+ $this->equalTo($before),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+
+ $this->assertThat(
+ $form->bind(array('checkboxes' => $bind)),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('checkboxes')->input,
+ $this->equalTo($after),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+ }
}
diff --git a/tests/suites/unit/joomla/form/fields/JFormFieldListTest.php b/tests/suites/unit/joomla/form/fields/JFormFieldListTest.php
index b71d97cf33..3fc71d0ea0 100644
--- a/tests/suites/unit/joomla/form/fields/JFormFieldListTest.php
+++ b/tests/suites/unit/joomla/form/fields/JFormFieldListTest.php
@@ -53,4 +53,293 @@ public function testGetInput()
// TODO: Should check all the attributes have come in properly.
}
+
+ /**
+ * Data provider for testGetInputDefaultPreset
+ */
+ public function caseGetInputDefaultPreset()
+ {
+ return array(
+ // no multiple, no default, no preset, existing value
+ array(
+ '',
+ '',
+ '1',
+'
+',
+'
+'
+ ),
+
+ // multiple, no default, no preset, existing value
+ array(
+ 'multiple="true"',
+ '',
+ '1',
+'
+',
+'
+'
+ ),
+
+ // no multiple, one default, no preset, existing value
+ array(
+ 'default="1"',
+ '',
+ '2',
+'
+',
+'
+'
+ ),
+
+ // no multiple, one default, no preset, empty value
+ array(
+ 'default="1"',
+ '',
+ '',
+'
+',
+'
+'
+ ),
+
+ // no multiple, no default, one preset, existing value
+ array(
+ 'preset="1"',
+ '',
+ '2',
+'
+',
+'
+'
+ ),
+
+ // no multiple, no default, no preset, existing value, readonly
+ array(
+ 'readonly="true"',
+ '',
+ '2',
+'
+',
+'
+'
+ ),
+
+ // multiple, no default, one preset, existing value, readonly
+ array(
+ 'multiple="true" readonly="true"',
+ '',
+ '2',
+'
+',
+'
+'
+ ),
+
+ // multiple, no default, one preset, existing value, readonly
+ array(
+ 'multiple="true" readonly="true"',
+ '',
+ array('1', '2'),
+'
+',
+'
+'
+ ),
+
+ // no multiple, one default, one preset, empty value
+ array(
+ 'preset="1" default="2"',
+ '',
+ '',
+'
+',
+'
+'
+ ),
+
+ // no multiple, one default, one preset, existing value
+ array(
+ 'preset="1" default="2"',
+ '',
+ '3',
+'
+',
+'
+'
+ ),
+
+ // multiple, many default, many preset, empty value
+ array(
+ 'multiple="true"',
+ '',
+ '',
+'
+',
+'
+'
+ ),
+
+ // multiple, many default, many preset, existing value
+ array(
+ 'multiple="true"',
+ '',
+ '2',
+'
+',
+'
+'
+ ),
+
+ // multiple, many default, many preset, existing value
+ array(
+ 'multiple="true"',
+ '',
+ array('2', '3'),
+'
+',
+'
+'
+ ),
+ );
+ }
+
+ /**
+ * Test the getInput method.
+ *
+ * @dataProvider caseGetInputDefaultPreset
+ */
+ public function testGetInputDefaultPreset($attributes, $options, $bind, $before, $after)
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load(''),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('list')->input,
+ $this->equalTo($before),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+
+ $this->assertThat(
+ $form->bind(array('list' => $bind)),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('list')->input,
+ $this->equalTo($after),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+ }
}