mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-83430 customfield_number: call display method from provider earlier
This commit is contained in:
parent
ae500b5728
commit
c0b2343245
@ -173,6 +173,10 @@ class field_controller extends \core_customfield\field_controller {
|
||||
* @return string|float|null
|
||||
*/
|
||||
public function prepare_field_for_display(mixed $value, ?context $context = null): string|null|float {
|
||||
if ($provider = provider_base::instance($this)) {
|
||||
return $provider->prepare_export_value($value, $context);
|
||||
}
|
||||
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
@ -185,16 +189,11 @@ class field_controller extends \core_customfield\field_controller {
|
||||
}
|
||||
} else {
|
||||
// Let's format the value.
|
||||
$provider = provider_base::instance($this);
|
||||
if ($provider) {
|
||||
$value = $provider->prepare_export_value($value, $context);
|
||||
} else {
|
||||
$value = format_float((float)$value, $decimalplaces);
|
||||
$value = format_float((float)$value, $decimalplaces);
|
||||
|
||||
// Apply the display format.
|
||||
$format = $this->get_configdata_property('display') ?? '{value}';
|
||||
$value = str_replace('{value}', $value, $format);
|
||||
}
|
||||
// Apply the display format.
|
||||
$format = $this->get_configdata_property('display') ?? '{value}';
|
||||
$value = str_replace('{value}', $value, $format);
|
||||
}
|
||||
|
||||
return format_string($value, true, ['context' => $context ?? system::instance()]);
|
||||
|
@ -157,13 +157,20 @@ class nofactivities extends provider_base {
|
||||
/**
|
||||
* Preparation for export for number of activities provider.
|
||||
*
|
||||
* @param mixed $value String or float
|
||||
* @param mixed $value String or float or null if the value is not present in the database for this instance
|
||||
* @param \context|null $context Context
|
||||
* @return ?string
|
||||
*/
|
||||
public function prepare_export_value(mixed $value, ?\context $context = null): ?string {
|
||||
if (trim((string)$value) === '') {
|
||||
if ($value === null) {
|
||||
return null;
|
||||
} else if (round((float)$value) == 0) {
|
||||
$whenzero = $this->field->get_configdata_property('displaywhenzero');
|
||||
if ((string) $whenzero === '') {
|
||||
return null;
|
||||
} else {
|
||||
return format_string($whenzero, true, ['context' => $context ?? \core\context\system::instance()]);
|
||||
}
|
||||
} else {
|
||||
return format_float((float)$value, 0);
|
||||
}
|
||||
|
@ -80,14 +80,34 @@ abstract class provider_base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider specific value preparation for export.
|
||||
* How the field should be displayed
|
||||
*
|
||||
* Called from {@see field_controller::prepare_field_for_display()}
|
||||
* The return value may contain safe HTML but all user input must be passed through
|
||||
* format_string/format_text functions
|
||||
*
|
||||
* @param mixed $value String or float
|
||||
* @param context|null $context Context
|
||||
* @return ?string
|
||||
* @return ?string null if the field should not be displayed or string representation of the field
|
||||
*/
|
||||
public function prepare_export_value(mixed $value, ?\context $context = null): ?string {
|
||||
return $value;
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// By default assumes that configuration 'decimalplaces' and 'displaywhenzero' are
|
||||
// present. If they are not used in this provider, override the method.
|
||||
$decimalplaces = (int) $this->field->get_configdata_property('decimalplaces');
|
||||
if (round((float) $value, $decimalplaces) == 0) {
|
||||
$result = $this->field->get_configdata_property('displaywhenzero');
|
||||
if ((string) $result === '') {
|
||||
return null;
|
||||
} else {
|
||||
return format_string($result, true, ['context' => $context ?? \core\context\system::instance()]);
|
||||
}
|
||||
} else {
|
||||
return format_float((float)$value, $decimalplaces);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user