Add field to course form

Add field to course form

by Opendix - -
Number of replies: 5

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();

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?

Average of ratings: -
In reply to Opendix -

Re: Add field to course form

by Opendix - -
Ok I gured out that next to the name I do also have to set the "shortname" and the "categoryId" on the fieldController. Then I can add custom fields. So my questions on that now:

- Is this the best approach?
- How do I make sure, that I do not mess up with existing other custom fields having the same shortname?
In reply to Opendix -

Re: Add field to course form

by Dominique Palumbo -
Picture of Plugin developers
Hi,

I do not know if it's better but you can create a second plugin to create the custolm field.
https://moodledev.io/docs/4.1/apis/plugintypes/customfield

The local plugin for the event handling and the second for the additional course field. And make the local plugin dependant of the custom field plugin.

https://docs.moodle.org/dev/version.php => $plugin->dependencies

But I doi not know if it's better !

Dominique.
In reply to Dominique Palumbo

Re: Add field to course form

by Opendix - -

Isnt this kind of plugin just a custom type of a possible field?

What I need is adding an instance of a custom field to the course edit form. Or do I miss something with this plugin type?

In reply to Opendix -

Re: Add field to course form

by Dominique Palumbo -
Picture of Plugin developers
Hi,

Yes, my bad. You just want to add a custom field not define one.
so your way is good. For the shortname of the field I suppose query on the table before creating it can do it.

Dominique.
In reply to Opendix -

Re: Add field to course form

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
A custom field shortname isn't visible to users, so I'd probably ensure it was distinct using a prefix to distinguish it - perhaps to match the category shortname.
The shortname field isn't validated as unique in the db, though I think it might be validated as unique to the category in code. But using a prefix should circumvent any worries you have: hallo_gurkensalat

There are a few ways to create a new category or field. You can use handlers or you could use the core_customfield\category and core_customfield\field classes directly - they are just persistent classes.