-
Improvement
-
Resolution: Unresolved
-
Minor
-
None
-
3.7.9, 3.8.6, 3.9.3, 3.10
-
None
-
MOODLE_310_STABLE, MOODLE_37_STABLE, MOODLE_38_STABLE, MOODLE_39_STABLE
Hi,
After using for a bit the Custom Field API, I find that sometimes the library is quite awkward to use for simple task such as:
- Adding/creating data related an entity.
- Creating a field object.
- Listing field definition (out of categories) or finding a field by its shortname in either a given area or by type.
I will add more information to this ticket but let me detail the first point. It might be some misunderstanding of the API and I wanted to check first if it was a valid remark.
Let's say that I want to set the value of a custom field for a given course.
$handler = \core_course\customfield\course_handler::create($course->id);
|
$coursedatafields = $handler->get_instance_data($course->id);
|
$context = $handler->get_instance_context($course->id);
|
...
|
foreach ($coursedatafields as $fid => $datafield) {
|
if ($datafield->get_field()->get('shortname') == $targetfieldname) {
|
$datafield->set('value', $value);
|
$datafield->set($datafield->datafield(), $value);
|
$datafield->set('contextid', $context->id);
|
$datafield->save();
|
}
|
}
|
- The first remark is that we need to recreate a $context. Why is this not set in the returned datafield ?
- The second one is that we should be able to get the datafield from its shortname directly (which is supposed to be unique). I would be great to have a method that would help to do this, for example $handler->get_field_data($course->id, 'myshortname').
- The "value" vs "charvalue" or "intvalue" or "decvalue". We should have a unique way of setting the value and this should be delegated to the field (so that the right column in the table is assigned to the right value). My take on this is that we should not have to set the 'value' and the '$datafield->datafield()' value. This should be done when we set the 'value' and that's it.
- is child of
-
MDL-57898 Add custom field types plugin and course custom fields functionality
-
- Closed
-