Back to Form class

Method findFieldsByGroup

protected \SimpleXMLElement[]|bool
findFieldsByGroup
(mixed $group = null, mixed $nested = false)
Method to get an array of `<field>` elements from the form XML document which are in a control group by name.
Parameters
  • mixed $group The optional dot-separated form group path on which to find the fields. Null will return all fields. False will return fields not in a group.
  • bool $nested True to also include fields in nested groups that are inside of the group for which to find fields.
Returns
  • \SimpleXMLElement[]|bool Boolean false on error or array of SimpleXMLElement objects.
Since
  • 1.7.0
Class: Form
Project: Joomla

Method findFieldsByGroup - Source code

/**
 * Method to get an array of `<field>` elements from the form XML document which are in a control group by name.
 *
 * @param   mixed    $group   The optional dot-separated form group path on which to find the fields.
 *                            Null will return all fields. False will return fields not in a group.
 * @param   boolean  $nested  True to also include fields in nested groups that are inside of the
 *                            group for which to find fields.
 *
 * @return  \SimpleXMLElement[]|boolean  Boolean false on error or array of SimpleXMLElement objects.
 *
 * @since   1.7.0
 */
protected function &findFieldsByGroup($group = null, $nested = false)
{
    $false = 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__));
    }
    // Get only fields in a specific group?
    if ($group) {
        // Get the fields elements for a given group.
        $elements =& $this->findGroup($group);
        // Get all of the field elements for the fields elements.
        foreach ($elements as $element) {
            // If there are field elements add them to the return result.
            if ($tmp = $element->xpath('descendant::field')) {
                // If we also want fields in nested groups then just merge the arrays.
                if ($nested) {
                    $fields = array_merge($fields, $tmp);
                } else {
                    $groupNames = explode('.', $group);
                    foreach ($tmp as $field) {
                        // Get the names of the groups that the field is in.
                        $attrs = $field->xpath('ancestor::fields[@name]/@name');
                        $names = array_map('strval', $attrs ?: []);
                        // If the field is in the specific group then add it to the return list.
                        if ($names == (array) $groupNames) {
                            $fields = array_merge($fields, array($field));
                        }
                    }
                }
            }
        }
    } elseif ($group === false) {
        // Get only field elements not in a group.
        $fields = $this->xml->xpath('descendant::fields[not(@name)]/field | descendant::fields[not(@name)]/fieldset/field ');
    } else {
        // Get an array of all the <field /> elements.
        $fields = $this->xml->xpath('//field[not(ancestor::field/form/*)]');
    }
    return $fields;
}