moodle/moodle · error · moodle_exception
invalidfieldtype
invalidfieldtype
Error message
invalidfieldtype
What it means
kml.php throws 'invalidfieldtype' (from the 'data' module language pack) when the data field identified by $fieldid is not of type 'latlong'. The KML export only makes sense for latitude/longitude fields, so any other field type is rejected before content is read. Note the code uses '! $field->type == 'latlong'', a precedence bug that effectively makes this branch always evaluate true-ish, so it can fire even for real latlong fields.
Solutions
- Verify the fieldid in the URL corresponds to a field of type 'latlong' (SELECT type FROM {data_fields} WHERE id = ?).
- Fix the precedence bug: change 'if (! $field->type == 'latlong')' to 'if ($field->type !== 'latlong')' so genuine latlong fields pass.
- Regenerate the KML link from the correct latlong field's page.
- Upgrade Moodle: newer code uses ($field->type !== 'latlong') correctly.
Example fix
// before
if (! $field->type == 'latlong') {
throw new \moodle_exception('invalidfieldtype', 'data');
}
// after
if ($field->type !== 'latlong') {
throw new \moodle_exception('invalidfieldtype', 'data');
} Defensive patterns
Strategy: validation
Validate before calling
$field = $DB->get_record('data_fields', ['id' => $fieldid], '*', IGNORE_MISSING);
if (!$field || $field->type !== 'latlong') { /* don't build the kml.php link */ } Type guard
function is_latlong_field(?object $field): bool {
return $field !== null && isset($field->type) && $field->type === 'latlong';
} Try / catch
try {
include kml.php flow;
} catch (\moodle_exception $e) {
if ($e->errorcode === 'invalidfieldtype') { redirect back with a friendly message; }
throw $e;
} Prevention
- Always generate KML links from the latlong field's own page, not hand-built URLs
- Fix the !-precedence comparison when patching this file
- Validate field type before linking to kml.php
When it happens
Trigger: Requesting mod/data/field/latlong/kml.php with a $fieldid (with $rid) pointing to a data_fields row whose type column is not 'latlong' (e.g. 'text', 'number', 'menu').
Common situations: Hand-crafted or stale KML/Google Earth links after the field was changed or deleted and recreated as a different type; copying a URL from a different database activity; the !-precedence bug causing spurious throws.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- A self conversation can not have more than one user.
- A valid forum ID is required to generate a summary report.
- Activity '' cannot be used as the default idnumber. The…
- An individual conversation can not have more than two users.
- An invalid conversation type was specified.
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/3fce3b7d6fc844d6.
Report an issue: GitHub.
Appendix: source
Thrown at public/mod/data/field/latlong/kml.php:52
if ($rid) {
if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
throw new \moodle_exception('invalidrecord', 'data');
}
if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
throw new \moodle_exception('invalidid', 'data');
}
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
throw new \moodle_exception('coursemisconf');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
throw new \moodle_exception('invalidcoursemodule');
}
if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
throw new \moodle_exception('invalidfieldid', 'data');
}
if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!
throw new \moodle_exception('invalidfieldtype', 'data');
}
if (! $content = $DB->get_record('data_content', array('fieldid'=>$fieldid, 'recordid'=>$rid))) {
throw new \moodle_exception('nofieldcontent', 'data');
}
} else { // We must have $d
if (! $data = $DB->get_record('data', array('id'=>$d))) {
throw new \moodle_exception('invalidid', 'data');
}
if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
throw new \moodle_exception('coursemisconf');
}
if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
throw new \moodle_exception('invalidcoursemodule');
}
if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {
throw new \moodle_exception('invalidfieldid', 'data');
}
if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!View on GitHub (pinned to 48ea8c3322)