vendor/easycorp/easyadmin-bundle/src/Factory/FieldFactory.php line 102

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
  3. use Doctrine\DBAL\Types\Types;
  4. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  6. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  10. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  11. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\TimeField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Layout\EaFormRowType;
  21. use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
  22. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  23. /**
  24.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  25.  */
  26. final class FieldFactory
  27. {
  28.     private static array $doctrineTypeToFieldFqcn = [
  29.         'array' => ArrayField::class, // don't use Types::ARRAY because it was removed in Doctrine ORM 3.0
  30.         Types::BIGINT => TextField::class,
  31.         Types::BINARY => TextareaField::class,
  32.         Types::BLOB => TextareaField::class,
  33.         Types::BOOLEAN => BooleanField::class,
  34.         Types::DATE_MUTABLE => DateField::class,
  35.         Types::DATE_IMMUTABLE => DateField::class,
  36.         Types::DATEINTERVAL => TextField::class,
  37.         Types::DATETIME_MUTABLE => DateTimeField::class,
  38.         Types::DATETIME_IMMUTABLE => DateTimeField::class,
  39.         Types::DATETIMETZ_MUTABLE => DateTimeField::class,
  40.         Types::DATETIMETZ_IMMUTABLE => DateTimeField::class,
  41.         Types::DECIMAL => NumberField::class,
  42.         Types::FLOAT => NumberField::class,
  43.         Types::GUID => TextField::class,
  44.         Types::INTEGER => IntegerField::class,
  45.         Types::JSON => TextField::class,
  46.         'object' => TextField::class, // don't use Types::OBJECT because it was removed in Doctrine ORM 3.0
  47.         Types::SIMPLE_ARRAY => ArrayField::class,
  48.         Types::SMALLINT => IntegerField::class,
  49.         Types::STRING => TextField::class,
  50.         Types::TEXT => TextareaField::class,
  51.         Types::TIME_MUTABLE => TimeField::class,
  52.         Types::TIME_IMMUTABLE => TimeField::class,
  53.     ];
  54.     public function __construct(
  55.         private readonly AdminContextProviderInterface $adminContextProvider,
  56.         private readonly AuthorizationCheckerInterface $authorizationChecker,
  57.         private readonly iterable $fieldConfigurators,
  58.         private readonly FormLayoutFactory $fieldLayoutFactory)
  59.     {
  60.     }
  61.     public function processFields(EntityDto $entityDtoFieldCollection $fields): void
  62.     {
  63.         $this->preProcessFields($fields$entityDto);
  64.         $context $this->adminContextProvider->getContext();
  65.         $currentPage $context->getCrud()->getCurrentPage();
  66.         $isDetailOrIndex \in_array($currentPage, [Crud::PAGE_INDEXCrud::PAGE_DETAIL], true);
  67.         foreach ($fields as $fieldDto) {
  68.             if ((null !== $currentPage && false === $fieldDto->isDisplayedOn($currentPage))
  69.                 || false === $this->authorizationChecker->isGranted(Permission::EA_VIEW_FIELD$fieldDto)) {
  70.                 $fields->unset($fieldDto);
  71.                 continue;
  72.             }
  73.             // "form rows" only make sense in pages that contain forms
  74.             if ($isDetailOrIndex && EaFormRowType::class === $fieldDto->getFormType()) {
  75.                 $fields->unset($fieldDto);
  76.                 continue;
  77.             }
  78.             // when creating new entities with "useEntryCrudForm" on an edit page we must
  79.             // explicitly check for the "new" page because $currentPage will be "edit"
  80.             if ((null === $entityDto->getInstance()) && !$fieldDto->isDisplayedOn(Crud::PAGE_NEW)) {
  81.                 $fields->unset($fieldDto);
  82.                 continue;
  83.             }
  84.             foreach ($this->fieldConfigurators as $configurator) {
  85.                 if (!$configurator->supports($fieldDto$entityDto)) {
  86.                     continue;
  87.                 }
  88.                 $configurator->configure($fieldDto$entityDto$context);
  89.             }
  90.             // check again if the field is displayed because this can change in the configurators
  91.             if (null !== $currentPage && false === $fieldDto->isDisplayedOn($currentPage)) {
  92.                 $fields->unset($fieldDto);
  93.                 continue;
  94.             }
  95.             foreach ($fieldDto->getFormThemes() as $formThemePath) {
  96.                 $context?->getCrud()?->addFormTheme($formThemePath);
  97.             }
  98.             $fields->set($fieldDto);
  99.         }
  100.         if (!$fields->isEmpty()) {
  101.             $this->fieldLayoutFactory->createLayout($fields$this->adminContextProvider->getContext()?->getCrud()?->getCurrentPage() ?? Crud::PAGE_INDEX);
  102.         }
  103.         $entityDto->setFields($fields);
  104.     }
  105.     private function preProcessFields(FieldCollection $fieldsEntityDto $entityDto): void
  106.     {
  107.         if ($fields->isEmpty()) {
  108.             return;
  109.         }
  110.         foreach ($fields as $fieldDto) {
  111.             if (Field::class !== $fieldDto->getFieldFqcn()) {
  112.                 continue;
  113.             }
  114.             // this is a virtual field, so we can't autoconfigure it
  115.             if (!$entityDto->hasProperty($fieldDto->getProperty())) {
  116.                 continue;
  117.             }
  118.             if ($fieldDto->getProperty() === $entityDto->getPrimaryKeyName()) {
  119.                 $guessedFieldFqcn IdField::class;
  120.             } else {
  121.                 $doctrinePropertyType $entityDto->getPropertyMetadata($fieldDto->getProperty())->get('type');
  122.                 $guessedFieldFqcn self::$doctrineTypeToFieldFqcn[$doctrinePropertyType] ?? null;
  123.                 if (null === $guessedFieldFqcn) {
  124.                     throw new \RuntimeException(sprintf('The Doctrine type of the "%s" field is "%s", which is not supported by EasyAdmin. For Doctrine\'s Custom Mapping Types have a look at EasyAdmin\'s field docs.'$fieldDto->getProperty(), $doctrinePropertyType));
  125.                 }
  126.             }
  127.             $fields->set($this->transformField($fieldDto$guessedFieldFqcn));
  128.         }
  129.     }
  130.     // transforms a generic Field class into a specific <type>Field class (e.g. DateTimeField)
  131.     private function transformField(FieldDto $fieldDtostring $newFieldFqcn): FieldDto
  132.     {
  133.         /** @var FieldDto $newField */
  134.         $newField $newFieldFqcn::new($fieldDto->getProperty())->getAsDto();
  135.         $newField->setUniqueId($fieldDto->getUniqueId());
  136.         $newField->setFieldFqcn($newFieldFqcn);
  137.         $newField->setDisplayedOn($fieldDto->getDisplayedOn());
  138.         $newField->setValue($fieldDto->getValue());
  139.         $newField->setFormattedValue($fieldDto->getFormattedValue());
  140.         $newField->setCssClass(trim($newField->getCssClass().' '.$fieldDto->getCssClass()));
  141.         $newField->setColumns($fieldDto->getColumns());
  142.         $newField->setTranslationParameters($fieldDto->getTranslationParameters());
  143.         $newField->setAssets($newField->getAssets()->mergeWith($fieldDto->getAssets()));
  144.         foreach ($fieldDto->getFormThemes() as $formThemePath) {
  145.             $newField->addFormTheme($formThemePath);
  146.         }
  147.         $customFormTypeOptions $fieldDto->getFormTypeOptions();
  148.         $defaultFormTypeOptions $newField->getFormTypeOptions();
  149.         $newField->setFormTypeOptions(array_merge($defaultFormTypeOptions$customFormTypeOptions));
  150.         $customFieldOptions $fieldDto->getCustomOptions()->all();
  151.         $defaultFieldOptions $newField->getCustomOptions()->all();
  152.         $mergedFieldOptions array_merge($defaultFieldOptions$customFieldOptions);
  153.         $newField->setCustomOptions($mergedFieldOptions);
  154.         if (null !== $fieldDto->getLabel()) {
  155.             $newField->setLabel($fieldDto->getLabel());
  156.         }
  157.         if (null !== $fieldDto->isVirtual()) {
  158.             $newField->setVirtual($fieldDto->isVirtual());
  159.         }
  160.         if (null !== $fieldDto->getTextAlign()) {
  161.             $newField->setTextAlign($fieldDto->getTextAlign());
  162.         }
  163.         if (null !== $fieldDto->isSortable()) {
  164.             $newField->setSortable($fieldDto->isSortable());
  165.         }
  166.         if (null !== $fieldDto->getPermission()) {
  167.             $newField->setPermission($fieldDto->getPermission());
  168.         }
  169.         if (null !== $fieldDto->getHelp()) {
  170.             $newField->setHelp($fieldDto->getHelp());
  171.         }
  172.         if (null !== $fieldDto->getFormType()) {
  173.             $newField->setFormType($fieldDto->getFormType());
  174.         }
  175.         // don't copy the template name and path from the original Field class
  176.         // (because they are just 'crud/field/text' and ' @EasyAdmin/crud/field/text.html.twig')
  177.         // and use the template name/path from the new specific field (e.g. 'crud/field/datetime')
  178.         return $newField;
  179.     }
  180. }