public static function create(int $id, \stdClass $record = null, category_controller $category = null) : field_controller {
        global $DB;
        if ($id && $record) {
            // This warning really should be in persistent as well.
            debugging('Too many parameters, either id need to be specified or a record, but not both.',
                DEBUG_DEVELOPER);
        }
        if ($id) {
            if (!$record = $DB->get_record(field::TABLE, array('id' => $id), '*', IGNORE_MISSING)) {
                throw new \moodle_exception('fieldnotfound', 'core_customfield');
            }
        }

        if (empty($record->categoryid)) {
            if (!$category) {
                throw new \coding_exception('Not enough parameters to initialise field_controller - unknown category');
            } else {
                $record->categoryid = $category->get('id');
            }
        }
        if (empty($record->type)) {
            throw new \coding_exception('Not enough parameters to initialise field_controller - unknown field type');
        }

        $type = $record->type;
        if (!$category) {
            $category = category_controller::create($record->categoryid);
        }
        if ($category->get('id') != $record->categoryid) {
            throw new \coding_exception('Category of the field does not match category from the parameter');
        }

        $customfieldtype = "\\customfield_{$type}\\field_controller";
        if (!class_exists($customfieldtype) || !is_subclass_of($customfieldtype, self::class)) {
            throw new \moodle_exception('errorfieldtypenotfound', 'core_customfield', '', s($type));
        }
        $fieldcontroller = new $customfieldtype(0, $record);
        $fieldcontroller->category = $category;
        $category->add_field($fieldcontroller);
        return $fieldcontroller;
    }