From 32a1533cdcd7cc9dfba828bb45a193e096d06248 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 29 Aug 2023 20:25:49 +0200 Subject: [PATCH] Add an option to hide true/false values in boolean fields --- doc/fields/BooleanField.rst | 34 +++++++++++++++++++ src/Field/BooleanField.php | 20 ++++++++++- .../views/crud/field/boolean.html.twig | 15 ++++++-- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/doc/fields/BooleanField.rst b/doc/fields/BooleanField.rst index 0bb02e78a3..1f33d61784 100644 --- a/doc/fields/BooleanField.rst +++ b/doc/fields/BooleanField.rst @@ -28,6 +28,40 @@ Basic Information Options ------- +``hideWhenFalse`` +~~~~~~~~~~~~~~~~ + +Use this option to not display anything when the field value is ``false``. This +is useful to reduce the "visual noise" in listings where most rows have the same +``false`` value and you want to ignore those and better highlight the rows with +the ``true`` value:: + + yield BooleanField::new('...')->hideWhenFalse(); + +Keep in mind that: + +* This option is ignored when using the ``renderAsSwitch()`` option, which always + displays a switch/toggle with the field value; +* This option is only applied to the ``index`` page; in the ``detail`` page you + will always see the field value to avoid any confussion. + +``hideWhenTrue`` +~~~~~~~~~~~~~~~~ + +Use this option to not display anything when the field value is ``true``. This +is useful to reduce the "visual noise" in listings where most rows have the same +``true`` value and you want to ignore those and better highlight the rows with +the ``false`` value:: + + yield BooleanField::new('...')->hideWhenTrue(); + +Keep in mind that: + +* This option is ignored when using the ``renderAsSwitch()`` option, which always + displays a switch/toggle with the field value; +* This option is only applied to the ``index`` page; in the ``detail`` page you + will always see the field value to avoid any confussion. + ``renderAsSwitch`` ~~~~~~~~~~~~~~~~~~ diff --git a/src/Field/BooleanField.php b/src/Field/BooleanField.php index 505ebae6f1..e9a2e9699c 100644 --- a/src/Field/BooleanField.php +++ b/src/Field/BooleanField.php @@ -16,6 +16,8 @@ final class BooleanField implements FieldInterface use FieldTrait; public const OPTION_RENDER_AS_SWITCH = 'renderAsSwitch'; + public const OPTION_HIDE_WHEN_TRUE = 'hideWhenTrue'; + public const OPTION_HIDE_WHEN_FALSE = 'hideWhenFalse'; /** @internal */ public const OPTION_TOGGLE_URL = 'toggleUrl'; /** @internal */ @@ -34,7 +36,9 @@ public static function new(string $propertyName, $label = null): self ->setFormType(CheckboxType::class) ->addCssClass('field-boolean') ->addJsFiles(Asset::fromEasyAdminAssetPackage('field-boolean.js')->onlyOnIndex()) - ->setCustomOption(self::OPTION_RENDER_AS_SWITCH, true); + ->setCustomOption(self::OPTION_RENDER_AS_SWITCH, true) + ->setCustomOption(self::OPTION_HIDE_WHEN_TRUE, false) + ->setCustomOption(self::OPTION_HIDE_WHEN_FALSE, false); } public function renderAsSwitch(bool $isASwitch = true): self @@ -43,4 +47,18 @@ public function renderAsSwitch(bool $isASwitch = true): self return $this; } + + public function hideWhenTrue(bool $hide = true): self + { + $this->setCustomOption(self::OPTION_HIDE_WHEN_TRUE, $hide); + + return $this; + } + + public function hideWhenFalse(bool $hide = true): self + { + $this->setCustomOption(self::OPTION_HIDE_WHEN_FALSE, $hide); + + return $this; + } } diff --git a/src/Resources/views/crud/field/boolean.html.twig b/src/Resources/views/crud/field/boolean.html.twig index 3f7734e920..62f319af9c 100644 --- a/src/Resources/views/crud/field/boolean.html.twig +++ b/src/Resources/views/crud/field/boolean.html.twig @@ -4,9 +4,18 @@ {% trans_default_domain 'EasyAdminBundle' %} {% if ea.crud.currentAction == 'detail' or not field.customOptions.get('renderAsSwitch') %} - - {{ (field.value == true ? 'label.true' : 'label.false')|trans }} - + {% set badge_is_hidden = ea.crud.currentAction == 'index' + and ( + (field.value == true and field.customOptions.get('hideWhenTrue') == true) + or + (field.value == false and field.customOptions.get('hideWhenFalse') == true) + ) %} + + {% if not badge_is_hidden %} + + {{ (field.value == true ? 'label.true' : 'label.false')|trans }} + + {% endif %} {% else %}