{"record":{"id":"38f9882738622106","repo":"moodle/moodle","slug":"contentbank-content-requires-the-field-reference-to-be","errorCode":null,"errorMessage":"contentbank_content requires the field reference to be specified","messagePattern":"contentbank_content requires the field reference to be specified","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"public/lib/behat/classes/behat_core_generator.php","lineNumber":1038,"sourceCode":"    protected function process_user_preferences(array $data) {\n        set_user_preference($data['preference'], $data['value'], $data['userid']);\n    }\n\n    /**\n     * Create content in the given context's content bank.\n     *\n     * @param array $data\n     * @return void\n     */\n    protected function process_contentbank_content(array $data) {\n        global $CFG;\n\n        if (empty($data['contextlevel'])) {\n            throw new Exception('contentbank_content requires the field contextlevel to be specified');\n        }\n\n        if (!isset($data['reference'])) {\n            throw new Exception('contentbank_content requires the field reference to be specified');\n        }\n\n        if (empty($data['contenttype'])) {\n            throw new Exception('contentbank_content requires the field contenttype to be specified');\n        }\n\n        $contenttypeclass = \"\\\\\".$data['contenttype'].\"\\\\contenttype\";\n        if (class_exists($contenttypeclass)) {\n            $context = $this->get_context($data['contextlevel'], $data['reference']);\n            $contenttype = new $contenttypeclass($context);\n            $record = new stdClass();\n            $record->usercreated = $data['userid'];\n            $record->name = $data['contentname'];\n            if (isset($data['visibility'])) {\n                $record->visibility = $data['visibility'];\n            }\n            $content = $contenttype->create_content($record);\n","sourceCodeStart":1020,"sourceCodeEnd":1056,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/behat/classes/behat_core_generator.php#L1020-L1056","documentation":"process_contentbank_content throws this when the 'reference' field is missing. After validating contextlevel, the generator needs reference (the instance identifier within that contextlevel) to resolve the exact context id for storing the content.","triggerScenarios":"A 'contentbank content' Behat table includes contextlevel but no 'reference' column or an empty reference cell; raised at behat_core_generator.php:1038.","commonSituations":"Fixtures where the instance shortname column was dropped during editing; users providing the course fullname in a differently-named column instead of the required 'reference'; generated fixtures missing the last column due to CSV parsing.","solutions":["Add a 'reference' row with the instance identifier matching the contextlevel (course shortname like C1, username for user context)","Ensure the referenced instance exists earlier in the scenario","Verify the header is exactly 'reference'","Confirm the empty() vs isset() semantics: an empty reference string is treated as missing"],"exampleFix":"// before\n  | contextlevel | contenttype     | name       |\n  | course       | contenttype_h5p | sample.h5p |\n// after\n  | contextlevel | reference | contenttype     | name       |\n  | course       | C1        | contenttype_h5p | sample.h5p |","handlingStrategy":"validation","validationCode":"if (empty($data['contextlevel']) || empty($data['reference']) || empty($data['contenttype'])) {\n    throw new ExpectationException(\"'contentbank content' requires contextlevel, reference and contenttype\", $this->getSession());\n}","typeGuard":"function isCompleteContentbankRow(array $data): bool {\n    return !empty($data['contextlevel']) && !empty($data['reference']) && !empty($data['contenttype']);\n}","tryCatchPattern":"try {\n    $this->getGenerator()->process_contentbank_content($data);\n} catch (\\Exception $e) {\n    if (str_contains($e->getMessage(), 'reference')) {\n        throw new ExpectationException('Add a reference row (instance id matching contextlevel) to your fixture', $this->getSession());\n    }\n    throw $e;\n}","preventionTips":["Validate all three mandatory columns (contextlevel, reference, contenttype) before running the step","Use exact course shortnames as reference values","Build fixtures from a template containing all required columns"],"tags":["behat","moodle","content-bank","missing-field"],"backgroundTag":"missing-required-argument","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}