Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
UploadJs.php
Go to the documentation of this file.
1 <?php
8 
14 {
21  public function execute()
22  {
23  $themeId = $this->getRequest()->getParam('id');
25  $serviceModel = $this->_objectManager->get(\Magento\Theme\Model\Uploader\Service::class);
27  $themeFactory = $this->_objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
29  $jsService = $this->_objectManager->get(\Magento\Framework\View\Design\Theme\Customization\File\Js::class);
30  try {
31  $theme = $themeFactory->create($themeId);
32  if (!$theme) {
33  throw new \Magento\Framework\Exception\LocalizedException(
34  __('We cannot find a theme with id "%1".', $themeId)
35  );
36  }
37  $jsFileData = $serviceModel->uploadJsFile('js_files_uploader');
38  $jsFile = $jsService->create();
39  $jsFile->setTheme($theme);
40  $jsFile->setFileName($jsFileData['filename']);
41  $jsFile->setData('content', $jsFileData['content']);
42  $jsFile->save();
43 
45  $customization = $this->_objectManager->create(
46  \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
47  ['theme' => $theme]
48  );
49  $customJsFiles = $customization->getFilesByType(
50  \Magento\Framework\View\Design\Theme\Customization\File\Js::TYPE
51  );
52  $result = ['error' => false, 'files' => $customization->generateFileInfo($customJsFiles)];
53  } catch (\Magento\Framework\Exception\LocalizedException $e) {
54  $result = ['error' => true, 'message' => $e->getMessage()];
55  } catch (\Exception $e) {
56  $result = ['error' => true, 'message' => __('We can\'t upload the JS file right now.')];
57  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
58  }
59  $this->getResponse()->representJson(
60  $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
61  );
62  }
63 }
__()
Definition: __.php:13
$theme