vendor/shopware/core/System/CustomField/CustomFieldService.php line 79

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\CustomField;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
  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\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CustomFieldService implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $attributeRepository;
  22.     /**
  23.      * @var CustomFieldEntity[]|null
  24.      */
  25.     private $customFields;
  26.     public function __construct(EntityRepositoryInterface $attributeRepository)
  27.     {
  28.         $this->attributeRepository $attributeRepository;
  29.     }
  30.     public function getCustomField(string $attributeName): ?Field
  31.     {
  32.         $attribute $this->getCustomFields()[$attributeName] ?? null;
  33.         if (!$attribute) {
  34.             return null;
  35.         }
  36.         switch ($attribute->getType()) {
  37.             case CustomFieldTypes::INT:
  38.                 return new IntField($attributeName$attributeName);
  39.             case CustomFieldTypes::FLOAT:
  40.                 return new FloatField($attributeName$attributeName);
  41.             case CustomFieldTypes::BOOL:
  42.                 return new BoolField($attributeName$attributeName);
  43.             case CustomFieldTypes::DATETIME:
  44.                 return new DateTimeField($attributeName$attributeName);
  45.             case CustomFieldTypes::TEXT:
  46.                 return new LongTextField($attributeName$attributeName);
  47.             case CustomFieldTypes::HTML:
  48.                 return (new LongTextField($attributeName$attributeName))->addFlags(new AllowHtml());
  49.             case CustomFieldTypes::JSON:
  50.             default:
  51.                 return new JsonField($attributeName$attributeName);
  52.         }
  53.     }
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             CustomFieldEvents::CUSTOM_FIELD_DELETED_EVENT => 'invalidateCache',
  58.             CustomFieldEvents::CUSTOM_FIELD_WRITTEN_EVENT => 'invalidateCache',
  59.         ];
  60.     }
  61.     /**
  62.      * @internal
  63.      */
  64.     public function invalidateCache(): void
  65.     {
  66.         $this->customFields null;
  67.     }
  68.     /**
  69.      * @return CustomFieldEntity[]
  70.      */
  71.     private function getCustomFields(): array
  72.     {
  73.         if ($this->customFields !== null) {
  74.             return $this->customFields;
  75.         }
  76.         $this->customFields = [];
  77.         $criteria = new Criteria();
  78.         $criteria->addFilter(new EqualsFilter('active'true));
  79.         // attributes should not be context dependent
  80.         $result $this->attributeRepository->search($criteriaContext::createDefaultContext());
  81.         /** @var CustomFieldEntity $attribute */
  82.         foreach ($result as $attribute) {
  83.             $this->customFields[$attribute->getName()] = $attribute;
  84.         }
  85.         return $this->customFields;
  86.     }
  87. }