← Back
Editing: EnumField.php
<?php declare(strict_types=1); namespace App\Controller\Admin\Field; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface; use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; final class EnumField implements FieldInterface { use FieldTrait; public const OPTION_ALLOW_MULTIPLE_CHOICES = 'allowMultipleChoices'; public const OPTION_AUTOCOMPLETE = 'autocomplete'; public const OPTION_CHOICES = 'choices'; public const OPTION_RENDER_AS_BADGES = 'renderAsBadges'; public const OPTION_RENDER_EXPANDED = 'renderExpanded'; public const OPTION_WIDGET = 'widget'; public const OPTION_ESCAPE_HTML_CONTENTS = 'escapeHtml'; public const VALID_BADGE_TYPES = ['success', 'warning', 'danger', 'info', 'primary', 'secondary', 'light', 'dark']; public const WIDGET_AUTOCOMPLETE = 'autocomplete'; public const WIDGET_NATIVE = 'native'; public static function new(string $propertyName, ?string $label = null): self { return (new self()) ->setProperty($propertyName) ->setLabel($label) ->setFormType(ChoiceType::class) ->setTemplateName('crud/field/choice') ->setCustomOption(self::OPTION_RENDER_EXPANDED, false) ->setCustomOption(self::OPTION_RENDER_AS_BADGES, null) ->setCustomOption(self::OPTION_RENDER_EXPANDED, false) ->setCustomOption(self::OPTION_WIDGET, null) ->setCustomOption(self::OPTION_ESCAPE_HTML_CONTENTS, true) ->setCustomOption(self::OPTION_ALLOW_MULTIPLE_CHOICES, false) ->setCustomOption(self::OPTION_CHOICES, function (object $entity, FieldDto $field) { $reflection = new \ReflectionProperty($entity::class, $field->getProperty()); if (!$reflection->getType() instanceof \ReflectionNamedType) { throw new \RuntimeException('Unable to reflect property'); } $out = []; $cases = ($reflection->getType()->getName())::cases(); foreach ($cases as $case) { $out[(string) $case->value] = $case; } return $out; }) ; } }
Save File
Cancel