Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Uploader.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 
20 {
24  protected $_config;
25 
29  protected $_template = 'Magento_Backend::media/uploader.phtml';
30 
34  protected $_fileSizeService;
35 
39  private $jsonEncoder;
40 
44  private $imageConfig;
45 
53  public function __construct(
54  \Magento\Backend\Block\Template\Context $context,
55  \Magento\Framework\File\Size $fileSize,
56  array $data = [],
57  Json $jsonEncoder = null,
58  UploadConfigInterface $imageConfig = null
59  ) {
60  $this->_fileSizeService = $fileSize;
61  $this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
62  $this->imageConfig = $imageConfig ?: ObjectManager::getInstance()->get(UploadConfigInterface::class);
63 
64  parent::__construct($context, $data);
65  }
66 
72  protected function _construct()
73  {
74  parent::_construct();
75 
76  $this->setId($this->getId() . '_Uploader');
77 
78  $uploadUrl = $this->_urlBuilder->addSessionParam()->getUrl('adminhtml/*/upload');
79  $this->getConfig()->setUrl($uploadUrl);
80  $this->getConfig()->setParams(['form_key' => $this->getFormKey()]);
81  $this->getConfig()->setFileField('file');
82  $this->getConfig()->setFilters(
83  [
84  'images' => [
85  'label' => __('Images (.gif, .jpg, .png)'),
86  'files' => ['*.gif', '*.jpg', '*.png'],
87  ],
88  'media' => [
89  'label' => __('Media (.avi, .flv, .swf)'),
90  'files' => ['*.avi', '*.flv', '*.swf'],
91  ],
92  'all' => ['label' => __('All Files'), 'files' => ['*.*']],
93  ]
94  );
95  }
96 
102  public function getFileSizeService()
103  {
105  }
106 
112  public function getImageUploadMaxWidth()
113  {
114  return $this->imageConfig->getMaxWidth();
115  }
116 
122  public function getImageUploadMaxHeight()
123  {
124  return $this->imageConfig->getMaxHeight();
125  }
126 
132  protected function _prepareLayout()
133  {
134  $this->pageConfig->addPageAsset('jquery/fileUploader/css/jquery.fileupload-ui.css');
135  return parent::_prepareLayout();
136  }
137 
143  public function getJsObjectName()
144  {
145  return $this->getHtmlId() . 'JsObject';
146  }
147 
153  public function getConfigJson()
154  {
155  return $this->jsonEncoder->encode($this->getConfig()->getData());
156  }
157 
163  public function getConfig()
164  {
165  if (null === $this->_config) {
166  $this->_config = new \Magento\Framework\DataObject();
167  }
168 
169  return $this->_config;
170  }
171 
180  public function getUploaderUrl($url)
181  {
182  return $this->_assetRepo->getUrl($url);
183  }
184 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\File\Size $fileSize, array $data=[], Json $jsonEncoder=null, UploadConfigInterface $imageConfig=null)
Definition: Uploader.php:53