-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-64838: Unable to create order from store front if customer ad…
…dress custom attribute is required. - Fixed an issue witn incorrent work of file/image custom attributes on checkout;
- Loading branch information
Alexey Yakimovich
committed
Feb 22, 2019
1 parent
94383aa
commit ffbf8e6
Showing
11 changed files
with
712 additions
and
7 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
app/code/Magento/Customer/Controller/Address/File/Upload.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Customer\Controller\Address\File; | ||
|
||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\App\Action\HttpPostActionInterface; | ||
use Magento\Framework\Api\CustomAttributesDataInterface; | ||
use Magento\Customer\Api\AddressMetadataInterface; | ||
use Magento\Customer\Model\FileUploader; | ||
use Magento\Customer\Model\FileUploaderFactory; | ||
use Magento\Framework\Controller\ResultFactory; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Psr\Log\LoggerInterface; | ||
use Magento\Customer\Model\FileProcessorFactory; | ||
|
||
/** | ||
* Class for upload files for customer custom address attributes | ||
*/ | ||
class Upload extends Action implements HttpPostActionInterface | ||
{ | ||
/** | ||
* @var FileUploaderFactory | ||
*/ | ||
private $fileUploaderFactory; | ||
|
||
/** | ||
* @var AddressMetadataInterface | ||
*/ | ||
private $addressMetadataService; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @var FileProcessorFactory | ||
*/ | ||
private $fileProcessorFactory; | ||
|
||
/** | ||
* @param Context $context | ||
* @param FileUploaderFactory $fileUploaderFactory | ||
* @param AddressMetadataInterface $addressMetadataService | ||
* @param LoggerInterface $logger | ||
* @param FileProcessorFactory $fileProcessorFactory | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
FileUploaderFactory $fileUploaderFactory, | ||
AddressMetadataInterface $addressMetadataService, | ||
LoggerInterface $logger, | ||
FileProcessorFactory $fileProcessorFactory | ||
) { | ||
$this->fileUploaderFactory = $fileUploaderFactory; | ||
$this->addressMetadataService = $addressMetadataService; | ||
$this->logger = $logger; | ||
$this->fileProcessorFactory = $fileProcessorFactory; | ||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute() | ||
{ | ||
try { | ||
$requestedFiles = $this->getRequest()->getFiles('custom_attributes'); | ||
if (empty($requestedFiles)) { | ||
$result = $this->processError(__('No files for upload.')); | ||
} else { | ||
$attributeCode = key($requestedFiles); | ||
$attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode); | ||
|
||
/** @var FileUploader $fileUploader */ | ||
$fileUploader = $this->fileUploaderFactory->create([ | ||
'attributeMetadata' => $attributeMetadata, | ||
'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS, | ||
'scope' => CustomAttributesDataInterface::CUSTOM_ATTRIBUTES, | ||
]); | ||
|
||
$errors = $fileUploader->validate(); | ||
if (true !== $errors) { | ||
$errorMessage = implode('</br>', $errors); | ||
$result = $this->processError(($errorMessage)); | ||
} else { | ||
$result = $fileUploader->upload(); | ||
} | ||
} | ||
} catch (LocalizedException $e) { | ||
$result = $this->processError($e->getMessage(), $e->getCode()); | ||
} catch (\Exception $e) { | ||
$this->logger->critical($e); | ||
$result = $this->processError($e->getMessage(), $e->getCode()); | ||
} | ||
|
||
$this->moveTmpFileToSuitableFolder($result); | ||
/** @var \Magento\Framework\Controller\Result\Json $resultJson */ | ||
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); | ||
$resultJson->setData($result); | ||
return $resultJson; | ||
} | ||
|
||
/** | ||
* Move file from temporary folder to the 'customer_address' media folder | ||
* | ||
* @param array $fileInfo | ||
* @throws LocalizedException | ||
*/ | ||
private function moveTmpFileToSuitableFolder(&$fileInfo) | ||
{ | ||
$fileName = $fileInfo['file']; | ||
$fileProcessor = $this->fileProcessorFactory | ||
->create(['entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS]); | ||
|
||
$newFilePath = $fileProcessor->moveTemporaryFile($fileName); | ||
$fileInfo['file'] = $newFilePath; | ||
$fileInfo['url'] = $fileProcessor->getViewUrl( | ||
$newFilePath, | ||
'file' | ||
); | ||
} | ||
|
||
/** | ||
* Prepare result array for errors | ||
* | ||
* @param string $message | ||
* @param int $code | ||
* @return array | ||
*/ | ||
private function processError($message, $code = 0) | ||
{ | ||
$result = [ | ||
'error' => $message, | ||
'errorcode' => $code, | ||
]; | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/code/Magento/Ui/view/frontend/web/templates/form/element/uploader/uploader.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
|
||
<div class="field-control" css="'_with-tooltip': $data.tooltip"> | ||
<div class="file-uploader" data-role="drop-zone" css="_loading: isLoading"> | ||
<div class="file-uploader-area"> | ||
<input type="file" afterRender="onElementRender" attr="id: uid, name: inputName, multiple: isMultipleFiles" | ||
disable="disabled"/> | ||
<label class="file-uploader-button action-default" attr="for: uid" translate="'Upload'"/> | ||
|
||
<span class="file-uploader-spinner"/> | ||
<render args="fallbackResetTpl" if="$data.showFallbackReset && $data.isDifferedFromDefault"/> | ||
</div> | ||
|
||
<render args="tooltipTpl" if="$data.tooltip"/> | ||
|
||
<div class="field-note" if="$data.notice" attr="id: noticeId"> | ||
<span html="notice"/> | ||
</div> | ||
|
||
<each args="data: value, as: '$file'" render="$parent.getPreviewTmpl($file)"/> | ||
|
||
<div if="isMultipleFiles" class="file-uploader-summary"> | ||
<label attr="for: uid" | ||
class="file-uploader-placeholder" | ||
css="'placeholder-' + placeholderType"> | ||
<span class="file-uploader-placeholder-text" | ||
translate="'Click here or drag and drop to add files.'"/> | ||
</label> | ||
</div> | ||
</div> | ||
<render args="$data.service.template" if="$data.hasService()"/> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.