moodle/moodle · error · Exception
'blocks' requires the field 'reference' to be specified
Error message
'blocks' requires the field 'reference' to be specified
What it means
This exception comes from the Behat test data generator for blocks (behat_core_generator::process_block_instance). When a Behat scenario creates a block instance, the 'reference' field is mandatory: it identifies the entity (e.g. a course or course module) whose context the block is attached to, resolved via get_context($data['contextlevel'], $data['reference']). If 'reference' is absent the generator cannot compute parentcontextid and throws instead of failing later with an opaque database error.
Solutions
- Add a 'reference' field naming the existing entity (e.g. course shortname or cm idnumber).
- Ensure the 'contextlevel' field is also set and consistent with the referenced entity (course, coursecat, module, system).
- If using a Behat table, include a 'reference' column header with the entity identifier in each row.
- Verify fixture keys match process_block_instance's expected keys exactly (contextlevel, reference, blockname).
Example fix
// before (Behat fixture) | blockname | contextlevel | | comments | course | // after | blockname | contextlevel | reference | | comments | course | C1 |
Defensive patterns
Strategy: validation
Validate before calling
$required = ['contextlevel', 'reference', 'blockname'];
foreach ($required as $key) {
if (empty($data[$key])) {
throw new InvalidArgumentException("blocks generator requires field '$key'");
}
} Try / catch
try {
$generator->create_block('comments', ['contextlevel' => 'course', 'reference' => 'C1']);
} catch (Exception $e) {
if (strpos($e->getMessage(), "requires the field 'reference'") !== false) {
// log fixture name and rethrow with context
}
throw $e;
} Prevention
- Always include contextlevel+reference pairs in block fixtures.
- Copy column headers from the official Behat generator docs for blocks.
- Validate fixtures in CI by running the Behat scenario once before merging.
- Keep block fixture rows consistent with other context-based generators.
When it happens
Trigger: Calling the blocks generator (e.g. the 'And the following "blocks" exist:' step or behat_core_generator's create_block_instance path) without a 'reference' field/column, even when 'contextlevel' is provided.
Common situations: Behat Gherkin fixture tables missing the reference column; refactors renaming the identifying column; copy-pasted fixture rows from other generators that use differently-named keys.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- 'system role assigns' requires the field 'user' to be…
- contentbank_content requires the field contenttype to be…
- contentbank_content requires the field contextlevel to be…
- contentbank_content requires the field reference to be…
- 'course enrolments' requires the field 'course' to be…
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/562e7e13283d2a22.
Report an issue: GitHub.
Appendix: source
Thrown at public/lib/behat/classes/behat_core_generator.php:502
* @param array $data should mostly match the fields of the block_instances table.
* The block type is specified by blockname.
* The parentcontextid is set from contextlevel and reference.
* Missing values are filled in by testing_block_generator::prepare_record.
* $data is passed to create_block as both $record and $options. Normally
* the keys are different, so this is a way to let people set values in either place.
*/
protected function process_block_instance($data) {
if (empty($data['blockname'])) {
throw new Exception('\'blocks\' requires the field \'block\' type to be specified');
}
if (empty($data['contextlevel'])) {
throw new Exception('\'blocks\' requires the field \'contextlevel\' to be specified');
}
if (!isset($data['reference'])) {
throw new Exception('\'blocks\' requires the field \'reference\' to be specified');
}
$context = $this->get_context($data['contextlevel'], $data['reference']);
$data['parentcontextid'] = $context->id;
// Pass $data as both $record and $options. I think that is unlikely to
// cause problems since the relevant key names are different.
// $options is not used in most blocks I have seen, but where it is, it is necessary.
$this->datagenerator->create_block($data['blockname'], $data, $data);
}
/**
* Creates language customisation.
*
* @throws Exception
* @throws dml_exception
* @param array $data
* @return voidView on GitHub (pinned to 48ea8c3322)