We develop a custom moodle plugin which will be of the type "local" as we do a lot of event-handing when user get enroled/unroled from a course (sany data around..).
Next step ist, that I do want to add a custom field to a course. I want to make sure that as soon our plugin is installed, this field is added to the "new course"-form.
My approach looks something like this:
$handler = \core_customfield\handler::get_handler('core_course', 'course');
$categoryId = $handler->create_category('HALLO');
$categoryController = \core_customfield\category_controller::create($categoryId, null, $handler);
$fieldController = \core_customfield\field_controller::create(0, (object)['type' => 'text'], $categoryController);
$fieldController -> set('name', 'GURKENSALAT');
$fieldController -> save();
$categoryId = $handler->create_category('HALLO');
$categoryController = \core_customfield\category_controller::create($categoryId, null, $handler);
$fieldController = \core_customfield\field_controller::create(0, (object)['type' => 'text'], $categoryController);
$fieldController -> set('name', 'GURKENSALAT');
$fieldController -> save();
Getting the handler and adding a category works fine. But I'm struggling with adding a field. Is there any documentation on that? Or maybe any sample? Is this even the right approach to do something like this?