Back to Form class

Method findField

protected \SimpleXMLElement|bool
findField
(mixed $name, mixed $group = null)
Method to get a form field represented as an XML element object.
Parameters
  • string $name The name of the form field.
  • string $group The optional dot-separated form group path on which to find the field.
Returns
  • \SimpleXMLElement|bool The XML element object for the field or boolean false on error.
Since
  • 1.7.0
Class: Form
Project: Joomla

Method findField - Source code

/**
 * Method to get a form field represented as an XML element object.
 *
 * @param   string  $name   The name of the form field.
 * @param   string  $group  The optional dot-separated form group path on which to find the field.
 *
 * @return  \SimpleXMLElement|boolean  The XML element object for the field or boolean false on error.
 *
 * @since   1.7.0
 */
protected function findField($name, $group = null)
{
    $element = false;
    $fields = [];
    // 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__));
    }
    // Let's get the appropriate field element based on the method arguments.
    if ($group) {
        // Get the fields elements for a given group.
        $elements =& $this->findGroup($group);
        // Get all of the field elements with the correct name for the fields elements.
        foreach ($elements as $el) {
            // If there are matching field elements add them to the fields array.
            if ($tmp = $el->xpath('descendant::field[@name="' . $name . '" and not(ancestor::field/form/*)]')) {
                $fields = array_merge($fields, $tmp);
            }
        }
        // Make sure something was found.
        if (!$fields) {
            return false;
        }
        // Use the first correct match in the given group.
        $groupNames = explode('.', $group);
        foreach ($fields as &$field) {
            // Get the group names as strings for ancestor fields elements.
            $attrs = $field->xpath('ancestor::fields[@name]/@name');
            $names = array_map('strval', $attrs ?: []);
            // If the field is in the exact group use it and break out of the loop.
            if ($names == (array) $groupNames) {
                $element =& $field;
                break;
            }
        }
    } else {
        // Get an array of fields with the correct name.
        $fields = $this->xml->xpath('//field[@name="' . $name . '" and not(ancestor::field/form/*)]');
        // Make sure something was found.
        if (!$fields) {
            return false;
        }
        // Search through the fields for the right one.
        foreach ($fields as &$field) {
            // If we find an ancestor fields element with a group name then it isn't what we want.
            if ($field->xpath('ancestor::fields[@name]')) {
                continue;
            } else {
                $element =& $field;
                break;
            }
        }
    }
    return $element;
}