Skip to content

Commit

Permalink
Subform Field: support JSON as default value
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Sep 6, 2015
1 parent f8c3a82 commit d8e665f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions libraries/joomla/form/fields/subform.php
Expand Up @@ -109,7 +109,7 @@ public function __set($name, $value)
$this->formsource = (string) $value;

// Add root path if we have a path to XML file
if(strrpos($this->formsource, '.xml') === strlen($this->formsource) - 4)
if (strrpos($this->formsource, '.xml') === strlen($this->formsource) - 4)
{
$this->formsource = JPath::clean(JPATH_ROOT . '/' . $this->formsource);
}
Expand All @@ -133,19 +133,19 @@ public function __set($name, $value)
break;

case 'buttons':
if(!$this->multiple)
if (!$this->multiple)
{
$this->buttons = array();
break;
}

if($value && !is_array($value))
if ($value && !is_array($value))
{
$value = explode(',', (string) $value);
$value = array_fill_keys(array_filter($value), true);
}

if($value)
if ($value)
{
$value = array_merge(array('add' => false, 'remove' => false, 'move' => false), $value);
$this->buttons = $value;
Expand All @@ -170,7 +170,7 @@ public function __set($name, $value)
*/
public function setup(SimpleXMLElement $element, $value, $group = null)
{
if(!parent::setup($element, $value, $group))
if (!parent::setup($element, $value, $group))
{
return false;
}
Expand All @@ -180,6 +180,12 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
$this->__set($attributeName, $element[$attributeName]);
}

if ($this->value && is_string($this->value))
{
// Guess here is the JSON string from 'default' attribute
$this->value = json_decode($this->value, true);
}

return true;
}

Expand Down Expand Up @@ -208,14 +214,14 @@ protected function getInput()
$tmpl = JForm::getInstance($formname, $this->formsource, array('control' => $tmplcontrol));

// Prepare the forms for exiting values
if($this->multiple)
if ($this->multiple)
{
$value = array_values($value);
$c = max($this->min, min(count($value), $this->max));
for($i = 0; $i < $c; $i++){
$itemcontrol = $control . '[' . $this->fieldname . $i . ']';
$itemform = JForm::getInstance($formname.$i, $this->formsource, array('control' => $itemcontrol));
if(!empty($value[$i]))
if (!empty($value[$i]))
{
$itemform->bind($value[$i]);
}
Expand Down Expand Up @@ -256,7 +262,7 @@ protected function getInput()

// Add hidden input on front of the subform inputs, in multiple mode
// for allow to submit an empty value
if($this->multiple)
if ($this->multiple)
{
$html = '<input name="' . $this->name . '" type="hidden" value="" />' . $html;
}
Expand Down

0 comments on commit d8e665f

Please sign in to comment.