Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Custom fields API: Difference between revisions

From MoodleDocs
(Created page with "{{Moodle 3.7}} == Custom fields API overview == Custom fields API was added in Moodle 3.7. It allows to configure custom fields that can be added to various contexts. Each c...")
 
m (Protected "Custom fields API": Developer Docs Migration ([Edit=Allow only administrators] (indefinite)))
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Moodle 3.7}}
{{Template:Migrated|newDocId=/docs/apis/core/customfields/}}
 
== Custom fields API overview ==
 
Custom fields API was added in Moodle 3.7. It allows to configure custom fields that can be added to various contexts. Each component (or plugin) that wants to use custom fields can define several areas. For example, 'core_course' component defines an area 'course' that allows to add custom fields to the courses, the same component can define another area 'coursecat' that will allow to add custom fields to the course categories. Inside each area the component/plugin can decide either to use or not to use itemid. For example, course custom fields are the same throughout the system and they don't use itemid (it is always 0). But there could be an activity module that would want to configure different custom fields for each individual instance of module, then this module would use the module instanceid as the itemid ([https://github.com/marinaglancy/moodle-mod_surveybuilder example]). This would allow to create modules similar to mod_data and mod_feedback where each instance has it's own set of fields.
 
New plugin type "customfield" was also added as part of the Custom fields API. Additional types of custom fields can be added to /customfield/field/ .
 
== How to add custom fields ==
 
Component/plugin that uses custom fields must define a *handler class* for each area and a *configuration page*. Handler class must be called *<PLUGINNAME>/customfield/<AREA>_handler* and be placed in autoloaded location (<PLUGINDIR>/classes/customfield/<AREA>_handler.php . This class must extend *\core_customfield\handler* . Configuration page may be located anywhere. For course custom fields configuration the admin settings page is used [https://github.com/moodle/moodle/blob/master/course/customfield.php /course/customfield.php]. If the area uses itemid this page should take itemid as a parameter.
 
In the following examples we will use the course handler as an example. It has component=core_course, area=course, itemid=0
 
Configuration page contents will be:
 
$output = $PAGE->get_renderer('core_customfield');
$handler = HANDLERCLASS::create($itemid);
$outputpage = new \core_customfield\output\management($handler);
echo $output->render($outputpage);
 
=== Add custom fields to the instance edit form ===

Latest revision as of 15:14, 15 September 2023

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!