From 62081448de3dc77a54e4175fd7262b98fda556dc Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 17 Sep 2024 18:31:00 +0530 Subject: [PATCH] Reduce code duplication --- src/View/Helper/CrudViewHelper.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/View/Helper/CrudViewHelper.php b/src/View/Helper/CrudViewHelper.php index 1382acb..6a9bd3b 100644 --- a/src/View/Helper/CrudViewHelper.php +++ b/src/View/Helper/CrudViewHelper.php @@ -228,7 +228,7 @@ public function formatBoolean(string $field, mixed $value, array $options): stri public function formatDateTime(string $field, mixed $value, array $options): string { if ($value === null) { - return $this->Html->badge(__d('crud', 'N/A'), ['class' => 'info']); + return $this->nullValueDisplay(); } if ($value instanceof Date) { @@ -244,7 +244,7 @@ public function formatDateTime(string $field, mixed $value, array $options): str } return (string)$this->Time->i18nFormat($value, $options['format'] ?? $this->getConfig('dateTimeFormat'), '') - ?: $this->Html->badge(__d('crud', 'N/A'), ['class' => 'info']); + ?: $this->nullValueDisplay(); } /** @@ -257,7 +257,7 @@ public function formatDateTime(string $field, mixed $value, array $options): str public function formatEnum(string $field, UnitEnum|BackedEnum|string|int|null $value, array $options): string { if ($value === null) { - return $this->Html->badge(__d('crud', 'N/A'), ['class' => 'info']); + return $this->nullValueDisplay(); } if (is_scalar($value)) { @@ -292,6 +292,16 @@ public function formatDisplayField(string $value, array $options): string return $this->createViewLink($value, ['escape' => false]); } + /** + * Display for `null` values + * + * @return string + */ + protected function nullValueDisplay(): string + { + return $this->Html->badge(__d('crud', 'N/A'), ['class' => 'info']); + } + /** * Returns a formatted relation output for a given field *