{"record":{"id":"6bccc39c3b7430e2","repo":"getgrav/grav","slug":"invalid-argument-element","errorCode":null,"errorMessage":"Invalid argument $element","messagePattern":"Invalid argument \\$element","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Framework/Collection/AbstractIndexCollection.php","lineNumber":296,"sourceCode":"    /**\n     * {@inheritDoc}\n     */\n    public function set($key, $value)\n    {\n        if (!$this->isAllowedElement($value)) {\n            throw new InvalidArgumentException('Invalid argument $value');\n        }\n\n        $this->entries[$key] = $this->getElementMeta($value);\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    public function add($element)\n    {\n        if (!$this->isAllowedElement($element)) {\n            throw new InvalidArgumentException('Invalid argument $element');\n        }\n\n        $this->entries[$this->getCurrentKey($element)] = $this->getElementMeta($element);\n\n        return true;\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    public function isEmpty()\n    {\n        return empty($this->entries);\n    }\n\n    /**\n     * Required by interface IteratorAggregate.\n     *","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/Collection/AbstractIndexCollection.php#L278-L314","documentation":"AbstractIndexCollection::add() appends an element keyed by getCurrentKey($element), and like set() it first runs isAllowedElement(). For Flex collections the element must be a FlexObject instance; anything else (array, scalar, null, foreign object) throws InvalidArgumentException('Invalid argument $element').","triggerScenarios":"Calling $flexIndex->add($arrayFromJson) with decoded raw data; add(null) when a chained lookup returned nothing; porting loops that previously pushed arrays into a plain collection; adding a FlexObject from a different Flex type/collection contract that is not the expected class.","commonSituations":"Building Flex indexes from external imports (CSV/JSON rows) without hydrating to objects; generic collection utility code shared between ArrayCollection and FlexIndex; null/false propagation from find() calls feeding add().","solutions":["Hydrate before adding: convert each row to a FlexObject via the Flex type's create/update API, then add($object).","Null-guard chained lookups: if ($obj = $collection->find($id)) { $index->add($obj); }.","If mixed content is intentional, switch to a non-index collection (ArrayCollection) that imposes no element type."],"exampleFix":"// before\nforeach ($jsonRows as $row) {\n    $index->add($row); // array -> InvalidArgumentException\n}\n\n// after\nforeach ($jsonRows as $row) {\n    $index->add($flex->createObject($row));\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use Grav\\Framework\\Flex\\FlexObject;\n\nif ($element instanceof FlexObject) {\n    $index->add($element);\n} elseif (is_array($element)) {\n    $index->add($flex->createObject($element)); // hydrate arrays\n} else {\n    throw new \\InvalidArgumentException('Expected FlexObject or row array');\n}","tryCatchPattern":null,"preventionTips":["Map import rows through createObject()/update() before add().","Check instanceof FlexObject in generic collection utilities that also serve FlexIndex.","Guard against null/false from find() lookups feeding add()."],"tags":["grav","flex","collection","type-validation"],"backgroundTag":"collection-type-mismatch","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}