public bool
setField
(\SimpleXMLElement $element, mixed $group = null, mixed $replace = true, mixed $fieldset = 'default')
Method to set a field XML element to the form definition. If the replace flag is set then
the field will be set whether it already exists or not. If it isn't set, then the field
will not be replaced if it already exists.
Parameters
- \SimpleXMLElement $element The XML element object representation of the form field.
- string $group The optional dot-separated form group path on which to set the field.
- bool $replace True to replace an existing field if one already exists.
- string $fieldset The name of the fieldset we are adding the field to.
Returns
Since
-
- \UnexpectedValueException
/**
* Method to set a field XML element to the form definition. If the replace flag is set then
* the field will be set whether it already exists or not. If it isn't set, then the field
* will not be replaced if it already exists.
*
* @param \SimpleXMLElement $element The XML element object representation of the form field.
* @param string $group The optional dot-separated form group path on which to set the field.
* @param boolean $replace True to replace an existing field if one already exists.
* @param string $fieldset The name of the fieldset we are adding the field to.
*
* @return boolean True on success.
*
* @since 1.7.0
* @throws \UnexpectedValueException
*/
public function setField(\SimpleXMLElement $element, $group = null, $replace = true, $fieldset = 'default')
{
// Make sure there is a valid Form XML document.
if (!$this->xml instanceof \SimpleXMLElement) {
throw new \UnexpectedValueException(sprintf('%s::%s `xml` is not an instance of SimpleXMLElement', \get_class($this), __METHOD__));
}
// Find the form field element from the definition.
$old = $this->findField((string) $element['name'], $group);
// If an existing field is found and replace flag is false do nothing and return true.
if (!$replace && !empty($old)) {
return true;
}
// If an existing field is found and replace flag is true remove the old field.
if ($replace && !empty($old) && $old instanceof \SimpleXMLElement) {
$dom = dom_import_simplexml($old);
// Get the parent element, this should be the fieldset
$parent = $dom->parentNode;
$fieldset = $parent->getAttribute('name');
$parent->removeChild($dom);
}
// Create the search path
$path = '//';
if (!empty($group)) {
$path .= 'fields[@name="' . $group . '"]/';
}
$path .= 'fieldset[@name="' . $fieldset . '"]';
$fs = $this->xml->xpath($path);
if (isset($fs[0]) && $fs[0] instanceof \SimpleXMLElement) {
// Add field to the form.
self::addNode($fs[0], $element);
// Synchronize any paths found in the load.
$this->syncPaths();
return true;
}
// We couldn't find a fieldset to add the field. Now we are checking, if we have set only a group
if (!empty($group)) {
$fields =& $this->findGroup($group);
// If an appropriate fields element was found for the group, add the element.
if (isset($fields[0]) && $fields[0] instanceof \SimpleXMLElement) {
self::addNode($fields[0], $element);
}
// Synchronize any paths found in the load.
$this->syncPaths();
return true;
}
// We couldn't find a parent so we are adding it at root level
// Add field to the form.
self::addNode($this->xml, $element);
// Synchronize any paths found in the load.
$this->syncPaths();
return true;
}