src/Core/System/CustomField/CustomFieldService.php line 82

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\CustomField;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\FloatField;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
  14. use Shopware\Core\Framework\Log\Package;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. /**
  18.  * @internal
  19.  */
  20. #[Package('core')]
  21. class CustomFieldService implements EventSubscriberInterfaceResetInterface
  22. {
  23.     /**
  24.      * @var array<string>|null
  25.      */
  26.     private ?array $customFields null;
  27.     /**
  28.      * @internal
  29.      */
  30.     public function __construct(private readonly Connection $connection)
  31.     {
  32.     }
  33.     public function getCustomField(string $attributeName): ?Field
  34.     {
  35.         $type $this->getCustomFields()[$attributeName] ?? null;
  36.         if (!$type) {
  37.             return null;
  38.         }
  39.         return match ($type) {
  40.             CustomFieldTypes::INT => (new IntField($attributeName$attributeName))->addFlags(new ApiAware()),
  41.             CustomFieldTypes::FLOAT => (new FloatField($attributeName$attributeName))->addFlags(new ApiAware()),
  42.             CustomFieldTypes::BOOL => (new BoolField($attributeName$attributeName))->addFlags(new ApiAware()),
  43.             CustomFieldTypes::DATETIME => (new DateTimeField($attributeName$attributeName))->addFlags(new ApiAware()),
  44.             CustomFieldTypes::TEXT => (new LongTextField($attributeName$attributeName))->addFlags(new ApiAware()),
  45.             CustomFieldTypes::HTML => (new LongTextField($attributeName$attributeName))->addFlags(new ApiAware(), new AllowHtml()),
  46.             CustomFieldTypes::PRICE => (new PriceField($attributeName$attributeName))->addFlags(new ApiAware()),
  47.             default => (new JsonField($attributeName$attributeName))->addFlags(new ApiAware()),
  48.         };
  49.     }
  50.     /**
  51.      * @return array<string, string>
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             CustomFieldEvents::CUSTOM_FIELD_DELETED_EVENT => 'reset',
  57.             CustomFieldEvents::CUSTOM_FIELD_WRITTEN_EVENT => 'reset',
  58.         ];
  59.     }
  60.     public function reset(): void
  61.     {
  62.         $this->customFields null;
  63.     }
  64.     /**
  65.      * @return array<string>
  66.      */
  67.     private function getCustomFields(): array
  68.     {
  69.         if ($this->customFields !== null) {
  70.             return $this->customFields;
  71.         }
  72.         $this->customFields $this->connection->fetchAllKeyValue('SELECT `name`, `type` FROM `custom_field` WHERE `active` = 1');
  73.         return $this->customFields;
  74.     }
  75. }