FOF30\Form\Form::findField PHP Method

findField() protected method

Method to get a form field represented as an XML element object.
Since: 11.1
protected findField ( string $name, string $group = null ) : mixed
$name string The name of the form field.
$group string The optional dot-separated form group path on which to find the field.
return mixed The XML element object for the field or boolean false on error.
    protected function findField($name, $group = null)
    {
        $element = false;
        $fields = array();
        // Make sure there is a valid JForm XML document.
        if (!$this->xml instanceof SimpleXMLElement) {
            return false;
        }
        // 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.
            /** @var SimpleXMLElement $element */
            foreach ($elements as $element) {
                // If there are matching field elements add them to the fields array.
                if ($tmp = $element->xpath('descendant::field[@name="' . $name . '"]')) {
                    $fields = array_merge($fields, $tmp);
                } elseif ($tmp = $element->xpath('descendant::field[@name_from="' . $name . '"]')) {
                    $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);
            /** @var SimpleXMLElement $field */
            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 ? $attrs : array());
                // 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 . '"]');
            if (!$fields) {
                $fields = array();
            }
            $fieldsNameFrom = $this->xml->xpath('//field[@name_from="' . $name . '"]');
            if ($fieldsNameFrom) {
                $fields = array_merge($fields, $fieldsNameFrom);
            }
            // Make sure something was found.
            if (empty($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;
    }