{"record":{"id":"3fce3b7d6fc844d6","repo":"moodle/moodle","slug":"invalidfieldtype","errorCode":"invalidfieldtype","errorMessage":"invalidfieldtype","messagePattern":"invalidfieldtype","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/mod/data/field/latlong/kml.php","lineNumber":52,"sourceCode":"\nif ($rid) {\n    if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {\n        throw new \\moodle_exception('invalidrecord', 'data');\n    }\n    if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {\n        throw new \\moodle_exception('invalidid', 'data');\n    }\n    if (! $course = $DB->get_record('course', array('id'=>$data->course))) {\n        throw new \\moodle_exception('coursemisconf');\n    }\n    if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {\n        throw new \\moodle_exception('invalidcoursemodule');\n    }\n    if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {\n        throw new \\moodle_exception('invalidfieldid', 'data');\n    }\n    if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!\n        throw new \\moodle_exception('invalidfieldtype', 'data');\n    }\n    if (! $content = $DB->get_record('data_content', array('fieldid'=>$fieldid, 'recordid'=>$rid))) {\n        throw new \\moodle_exception('nofieldcontent', 'data');\n    }\n} else {   // We must have $d\n    if (! $data = $DB->get_record('data', array('id'=>$d))) {\n        throw new \\moodle_exception('invalidid', 'data');\n    }\n    if (! $course = $DB->get_record('course', array('id'=>$data->course))) {\n        throw new \\moodle_exception('coursemisconf');\n    }\n    if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {\n        throw new \\moodle_exception('invalidcoursemodule');\n    }\n    if (! $field = $DB->get_record('data_fields', array('id'=>$fieldid))) {\n        throw new \\moodle_exception('invalidfieldid', 'data');\n    }\n    if (! $field->type == 'latlong') { // Make sure we're looking at a latlong data type!","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/mod/data/field/latlong/kml.php#L34-L70","documentation":"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.","triggerScenarios":"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').","commonSituations":"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.","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."],"exampleFix":"// before\nif (! $field->type == 'latlong') {\n    throw new \\moodle_exception('invalidfieldtype', 'data');\n}\n// after\nif ($field->type !== 'latlong') {\n    throw new \\moodle_exception('invalidfieldtype', 'data');\n}","handlingStrategy":"validation","validationCode":"$field = $DB->get_record('data_fields', ['id' => $fieldid], '*', IGNORE_MISSING);\nif (!$field || $field->type !== 'latlong') { /* don't build the kml.php link */ }","typeGuard":"function is_latlong_field(?object $field): bool {\n    return $field !== null && isset($field->type) && $field->type === 'latlong';\n}","tryCatchPattern":"try {\n    include kml.php flow;\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'invalidfieldtype') { redirect back with a friendly message; }\n    throw $e;\n}","preventionTips":["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"],"tags":["moodle","database-activity","validation","operator-precedence"],"backgroundTag":"invalid-enum-value","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"}