/** * {@inheritdoc} * * @return $this|string */ public function compactValue($value) { if ($this->getIsAjaxRequest()) { return $this; } $attribute = $this->getAttribute(); $original = $this->_value; $toDelete = false; if ($original) { if (!$attribute->isRequired() && !empty($value['delete'])) { $toDelete = true; } if (!empty($value['tmp_name'])) { $toDelete = true; } } $path = $this->_fileSystem->getPath(\Magento\Framework\App\Filesystem::MEDIA_DIR) . '/' . $this->_entityTypeCode; $result = $original; // unlink entity file if ($toDelete) { $result = ''; $file = $path . $original; $ioFile = new \Magento\Framework\Io\File(); if ($ioFile->fileExists($file)) { $ioFile->rm($file); } } if (!empty($value['tmp_name'])) { try { $uploader = new \Magento\Framework\File\Uploader($value); $uploader->setFilesDispersion(true); $uploader->setFilenamesCaseSensitivity(false); $uploader->setAllowRenameFiles(true); $uploader->save($path, $value['name']); $fileName = $uploader->getUploadedFileName(); $result = $fileName; } catch (\Exception $e) { $this->_logger->logException($e); } } return $result; }
/** * Export attribute value to entity model * * @param array|string $value * @return $this */ public function compactValue($value) { if ($this->getIsAjaxRequest()) { return $this; } $attribute = $this->getAttribute(); $original = $this->getEntity()->getData($attribute->getAttributeCode()); $toDelete = false; if ($original) { if (!$attribute->getIsRequired() && !empty($value['delete'])) { $toDelete = true; } if (!empty($value['tmp_name'])) { $toDelete = true; } } $destinationFolder = $attribute->getEntity()->getEntityTypeCode(); // unlink entity file if ($toDelete) { $this->getEntity()->setData($attribute->getAttributeCode(), ''); $file = $destinationFolder . $original; if ($this->_directory->isExist($file)) { $this->_directory->delete($file); } } if (!empty($value['tmp_name'])) { try { $uploader = new \Magento\Framework\File\Uploader($value); $uploader->setFilesDispersion(true); $uploader->setFilenamesCaseSensitivity(false); $uploader->setAllowRenameFiles(true); $uploader->save($this->_directory->getAbsolutePath($destinationFolder), $value['name']); $fileName = $uploader->getUploadedFileName(); $this->getEntity()->setData($attribute->getAttributeCode(), $fileName); } catch (\Exception $e) { $this->_logger->logException($e); } } return $this; }