11 use Magento\Customer\Model\FileProcessorFactory;
20 private $customerMetadataService;
25 private $addressMetadataService;
30 private $elementFactory;
35 private $fileProcessorFactory;
40 private $attributeMetadata;
45 private $entityTypeCode;
65 FileProcessorFactory $fileProcessorFactory,
70 $this->customerMetadataService = $customerMetadataService;
71 $this->addressMetadataService = $addressMetadataService;
72 $this->elementFactory = $elementFactory;
73 $this->fileProcessorFactory = $fileProcessorFactory;
74 $this->attributeMetadata = $attributeMetadata;
75 $this->entityTypeCode = $entityTypeCode;
76 $this->scope = $scope;
86 $formElement = $this->elementFactory->create(
87 $this->attributeMetadata,
92 $errors = $formElement->validateValue($this->getData());
102 public function upload()
105 $fileProcessor = $this->fileProcessorFactory->create([
106 'entityTypeCode' => $this->entityTypeCode,
107 'allowedExtensions' => $this->getAllowedExtensions(),
110 $result = $fileProcessor->saveTemporaryFile($this->scope .
'[' . $this->getAttributeCode() .
']');
115 $result[
'url'] = $fileProcessor->getViewUrl(
117 $this->attributeMetadata->getFrontendInput()
128 private function getAttributeCode()
130 return key($_FILES[$this->scope][
'name']);
138 private function getData()
142 $fileAttributes = $_FILES[$this->scope];
143 foreach ($fileAttributes as $attributeName => $attributeValue) {
144 $data[$attributeName] = $attributeValue[$this->getAttributeCode()];
155 private function getAllowedExtensions()
159 $validationRules = $this->attributeMetadata->getValidationRules();
160 foreach ($validationRules as $validationRule) {
161 if ($validationRule->getName() ==
'file_extensions') {
__construct(CustomerMetadataInterface $customerMetadataService, AddressMetadataInterface $addressMetadataService, ElementFactory $elementFactory, FileProcessorFactory $fileProcessorFactory, AttributeMetadataInterface $attributeMetadata, $entityTypeCode, $scope)