/**
* Method to attach a Form object to the field.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
*
* @return boolean True on success.
*
* @since 1.7.0
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
// Make sure there is a valid FormField XML element.
if ((string) $element->getName() !== 'field') {
return false;
}
// Reset the input and label values.
$this->input = null;
$this->label = null;
// Set the XML element object.
$this->element = $element;
// Set the group of the field.
$this->group = $group;
$attributes = array('multiple', 'name', 'id', 'hint', 'class', 'description', 'labelclass', 'onchange', 'onclick', 'validate', 'pattern', 'validationtext', 'default', 'required', 'disabled', 'readonly', 'autofocus', 'hidden', 'autocomplete', 'spellcheck', 'translateHint', 'translateLabel', 'translate_label', 'translateDescription', 'translate_description', 'size', 'showon');
$this->default = isset($element['value']) ? (string) $element['value'] : $this->default;
// Set the field default value.
if ($element['multiple'] && \is_string($value) && \is_array(json_decode($value, true))) {
$this->value = (array) json_decode($value);
} else {
$this->value = $value;
}
// Lets detect miscellaneous data attribute. For eg, data-*
foreach ($this->element->attributes() as $key => $value) {
if (strpos($key, 'data-') === 0) {
// Data attribute key value pair
$this->dataAttributes[$key] = $value;
}
}
foreach ($attributes as $attributeName) {
$this->__set($attributeName, $element[$attributeName]);
}
// Allow for repeatable elements
$repeat = (string) $element['repeat'];
$this->repeat = $repeat === 'true' || $repeat === 'multiple' || !empty($this->form->repeat) && $this->form->repeat == 1;
// Set the visibility.
$this->hidden = $this->hidden || strtolower((string) $this->element['type']) === 'hidden';
$this->layout = !empty($this->element['layout']) ? (string) $this->element['layout'] : $this->layout;
$this->parentclass = isset($this->element['parentclass']) ? (string) $this->element['parentclass'] : $this->parentclass;
// Add required to class list if field is required.
if ($this->required) {
$this->class = trim($this->class . ' required');
}
return true;
}