MDL-83430 customfield_number: call display method from provider earlier

This commit is contained in:
Marina Glancy 2024-10-10 13:22:15 +01:00
parent 61d9a4ccac
commit 6d757af2c9
3 changed files with 40 additions and 14 deletions

View file

@ -173,6 +173,10 @@ class field_controller extends \core_customfield\field_controller {
* @return string|float|null * @return string|float|null
*/ */
public function prepare_field_for_display(mixed $value, ?context $context = null): string|null|float { 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) { if ($value === null) {
return null; return null;
} }
@ -185,17 +189,12 @@ class field_controller extends \core_customfield\field_controller {
} }
} else { } else {
// Let's format the value. // 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. // Apply the display format.
$format = $this->get_configdata_property('display') ?? '{value}'; $format = $this->get_configdata_property('display') ?? '{value}';
$value = str_replace('{value}', $value, $format); $value = str_replace('{value}', $value, $format);
} }
}
return format_string($value, true, ['context' => $context ?? system::instance()]); return format_string($value, true, ['context' => $context ?? system::instance()]);
} }

View file

@ -157,13 +157,20 @@ class nofactivities extends provider_base {
/** /**
* Preparation for export for number of activities provider. * 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 * @param \context|null $context Context
* @return ?string * @return ?string
*/ */
public function prepare_export_value(mixed $value, ?\context $context = null): ?string { public function prepare_export_value(mixed $value, ?\context $context = null): ?string {
if (trim((string)$value) === '') { if ($value === null) {
return 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 { } else {
return format_float((float)$value, 0); return format_float((float)$value, 0);
} }

View file

@ -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 mixed $value String or float
* @param context|null $context Context * @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 { 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);
}
} }
/** /**