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

  1. Verify the fieldid in the URL corresponds to a field of type 'latlong' (SELECT type FROM {data_fields} WHERE id = ?).
  2. Fix the precedence bug: change 'if (! $field->type == 'latlong')' to 'if ($field->type !== 'latlong')' so genuine latlong fields pass.
  3. Regenerate the KML link from the correct latlong field's page.
  4. 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

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


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)