← Back
Editing: EnumConfigurator.php
<?php declare(strict_types=1); namespace App\Controller\Admin\Configurator; use App\Controller\Admin\Field\EnumField; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; use EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableChoiceMessage; use EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableChoiceMessageCollection; use function Symfony\Component\String\u; use function Symfony\Component\Translation\t; final class EnumConfigurator implements FieldConfiguratorInterface { public function supports(FieldDto $field, EntityDto $entityDto): bool { return EnumField::class === $field->getFieldFqcn(); } public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void { $isExpanded = true === $field->getCustomOption(EnumField::OPTION_RENDER_EXPANDED); $isMultipleChoice = true === $field->getCustomOption(EnumField::OPTION_ALLOW_MULTIPLE_CHOICES); $choices = $this->getChoices($field->getCustomOption(EnumField::OPTION_CHOICES), $entityDto, $field); $field->setFormTypeOptionIfNotSet('choices', $choices); $field->setFormTypeOptionIfNotSet('multiple', $isMultipleChoice); $field->setFormTypeOptionIfNotSet('expanded', $isExpanded); if ($isExpanded && EnumField::WIDGET_AUTOCOMPLETE === $field->getCustomOption(EnumField::OPTION_WIDGET)) { throw new \InvalidArgumentException(sprintf('The "%s" choice field wants to be displayed as an autocomplete widget and as an expanded list of choices at the same time, which is not possible. Use the renderExpanded() and renderAsNativeWidget() methods to change one of those options.', $field->getProperty())); } if (null === $field->getCustomOption(EnumField::OPTION_WIDGET)) { $field->setCustomOption(EnumField::OPTION_WIDGET, $isExpanded ? EnumField::WIDGET_NATIVE : EnumField::WIDGET_AUTOCOMPLETE); } if (EnumField::WIDGET_AUTOCOMPLETE === $field->getCustomOption(EnumField::OPTION_WIDGET)) { $field->setFormTypeOption('attr.data-ea-widget', 'ea-autocomplete'); $field->setDefaultColumns($isMultipleChoice ? 'col-md-8 col-xxl-6' : 'col-md-6 col-xxl-5'); } $field->setFormTypeOptionIfNotSet('placeholder', ''); // the value of this form option must be a string to properly propagate it as an HTML attribute value $field->setFormTypeOption('attr.data-ea-autocomplete-render-items-as-html', true === $field->getCustomOption(EnumField::OPTION_ESCAPE_HTML_CONTENTS) ? 'false' : 'true'); $fieldValue = $field->getValue(); $isIndexOrDetail = \in_array($context->getCrud()?->getCurrentPage(), [Crud::PAGE_INDEX, Crud::PAGE_DETAIL], true); if (null === $fieldValue || !$isIndexOrDetail) { return; } $badgeSelector = $field->getCustomOption(EnumField::OPTION_RENDER_AS_BADGES); $isRenderedAsBadge = null !== $badgeSelector && false !== $badgeSelector; $translationParameters = $context->getI18n()->getTranslationParameters(); $translationDomain = $context->getI18n()->getTranslationDomain(); $choiceMessages = []; // Translatable choice don't need to get flipped foreach ((array) $fieldValue as $selectedValue) { if (null !== $selectedLabel = $choices[$selectedValue] ?? null) { $choiceMessage = t( $selectedLabel->value,/* @phpstan-ignore-line */ $translationParameters, $translationDomain ); $choiceMessages[] = new TranslatableChoiceMessage( $choiceMessage, $isRenderedAsBadge ? $this->getBadgeCssClass($badgeSelector, $selectedValue->value, $field) : null /* @phpstan-ignore-line */ ); } } $field->setFormattedValue(new TranslatableChoiceMessageCollection($choiceMessages, $isRenderedAsBadge)); } /** * @return mixed[] */ private function getChoices(mixed $choiceGenerator, EntityDto $entity, FieldDto $field): array { if (null === $choiceGenerator) { return []; } if (\is_array($choiceGenerator)) { return $choiceGenerator; } if (!is_callable($choiceGenerator)) { throw new \InvalidArgumentException(); } return $choiceGenerator($entity->getInstance(), $field); } private function getBadgeCssClass(mixed $badgeSelector, mixed $value, FieldDto $field): string { $commonBadgeCssClass = 'badge'; $badgeType = ''; if (true === $badgeSelector) { $badgeType = 'badge-secondary'; } elseif (\is_array($badgeSelector)) { $badgeType = $badgeSelector[$value] ?? 'badge-secondary'; } elseif (\is_callable($badgeSelector)) { $badgeType = $badgeSelector($value, $field); if (!\in_array($badgeType, EnumField::VALID_BADGE_TYPES, true)) { throw new \RuntimeException(sprintf('The value returned by the callable passed to the "renderAsBadges()" method must be one of the following valid badge types: "%s" ("%s" given).', implode(', ', ChoiceField::VALID_BADGE_TYPES), $badgeType)); } } $badgeTypeCssClass = (null === $badgeType || '' === $badgeType) ? '' : u($badgeType)->ensureStart('badge-')->toString(); return $commonBadgeCssClass.' '.$badgeTypeCssClass; } }
Save File
Cancel