Add "preset" attribute to form fields and adapt list/checkboxes multiple default/preset values #1257

Closed
wants to merge 1 commit into
from
@@ -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[] = '<ul>';
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;
}
@@ -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[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
+ $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
+ foreach ($values as $value)
+ {
+ $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $value . '"/>';
realityking
realityking Jun 6, 2012 Member

How does this work? If they all have the same name I'd think only one would get send to the server.

+ }
}
// 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;
}
@@ -622,6 +622,31 @@ public function getValue($name, $group = null, $default = null)
}
/**
+ * 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.
*
* The replace option works per field. If a field being loaded already exists in the current
@@ -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.
@@ -249,6 +249,11 @@ class JFormDataHelper
name="title"
type="text"
description="The title." />
+ <field
+ name="alias"
+ type="text"
+ description="The alias."
+ preset="preset-alias" />
<fields
name="params">
<field
@@ -814,10 +814,17 @@ public function testGetField()
'Line:'.__LINE__.' Prior to binding data, the defaults in the field should be used.'
);
+ $this->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,
@@ -837,6 +844,12 @@ public function testGetField()
);
$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),
'Line:'.__LINE__.' Check the field value bound correctly.'
@@ -1138,6 +1151,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.
*/
public function testGetFieldset()
Oops, something went wrong.