Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 49 additions & 1 deletion libraries/joomla/form/fields/checkboxes.php
Expand Up @@ -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"' : '';

Expand Down Expand Up @@ -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;
}
Expand Down
64 changes: 61 additions & 3 deletions libraries/joomla/form/fields/list.php
Expand Up @@ -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 . '"/>';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Expand Down Expand Up @@ -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;
}
Expand Down
56 changes: 55 additions & 1 deletion libraries/joomla/form/form.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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';

Expand All @@ -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'))
{
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions tests/suites/unit/joomla/form/JFormDataHelper.php
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/suites/unit/joomla/form/JFormTest.php
Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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.
*/
Expand Down