/**
* Method to filter a field value.
*
* @param mixed $value The optional value to use as the default for the field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @param Registry $input An optional Registry object with the entire data set to filter
* against the entire form.
*
* @return mixed The filtered value.
*
* @since 4.0.0
* @throws \UnexpectedValueException
*/
public function filter($value, $group = null, Registry $input = null)
{
// Make sure there is a valid SimpleXMLElement.
if (!$this->element instanceof \SimpleXMLElement) {
throw new \UnexpectedValueException(sprintf('%s::filter `element` is not an instance of SimpleXMLElement', \get_class($this)));
}
// Get the field filter type.
$filter = (string) $this->element['filter'];
if ($filter !== '') {
return parent::filter($value, $group, $input);
}
// Dirty way of ensuring required fields in subforms are submitted and filtered the way other fields are
$subForm = $this->loadSubForm();
// Subform field may have a default value, that is a JSON string
if ($value && is_string($value)) {
$value = json_decode($value, true);
// The string is invalid json
if (!$value) {
return null;
}
}
if ($this->multiple) {
$return = [];
if ($value) {
foreach ($value as $key => $val) {
$return[$key] = $subForm->filter($val);
}
}
} else {
$return = $subForm->filter($value);
}
return $return;
}