Skip to content

Conversation

@AlexandreBulete
Copy link

Description

This PR adds a new badge field component for Sylius Grid that supports:

  • ✅ PHP 8.1+ enums with getLabel(), getColor(), and getIcon() methods
  • ✅ Customization via options.vars.colors, labels, and icons
  • ✅ Badge styling with rounded-pill
  • ✅ Optional icon
  • ✅ Sylius test attributes

Usage Example

use Sylius\Bundle\GridBundle\Builder\Field\TwigField;

TwigField::create('status', '@SyliusBootstrapAdminUi/shared/grid/field/badge.html.twig')
    ->setLabel('Status')
    ->setOptions([
        'vars' => [
            'colors' => [
                'active' => 'success',
                'inactive' => 'danger',
            ],
            'icons' => [
                'active' => 'bi:check-circle',
            ],
        ],
    ]),

With PHP Enum

enum Status: string
{
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';

    public function getLabel(): string
    {
        return match($this) {
            self::ACTIVE => 'Active',
            self::INACTIVE => 'Inactive',
        };
    }

    public function getColor(): string
    {
        return match($this) {
            self::ACTIVE => 'success',
            self::INACTIVE => 'danger',
        };
    }

    public function getIcon(): string
    {
        return match($this) {
            self::ACTIVE => 'bi:check-circle',
            self::INACTIVE => 'bi:x-circle',
        };
    }
}

Files added

  • templates/shared/grid/field/badge.html.twig
  • templates/shared/helper/field/badge.html.twig

@AlexandreBulete AlexandreBulete changed the title feat: Add badge field component with enum support [AdminUi] Add badge field component with enum support Nov 14, 2025
Copy link
Member

@loic425 loic425 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for your proposal. It's an interesting idea.

1/ You Twig template has too much complexity
2/ test_html_attribute placed on color is strange
3/ magic with getLabel, getColor without any interface is not cool.

@AlexandreBulete
Copy link
Author

Thanks for the feedback!

I've refactored the badge implementation to address all concerns:

  • Template complexity reduced (53 → 26 lines, logic moved to PHP)
  • Test attribute now value-based instead of color-based
  • Added BadgeableInterface for type safety (no more magic checks)

Created proper architecture with value objects and Twig extension. Documentation included.

Let me know if you need any changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants