/** * Copy customizations stored in a database from one theme to another, overriding existing data * * @param ThemeInterface $source * @param ThemeInterface $target * @return void */ protected function _copyDatabaseCustomization(ThemeInterface $source, ThemeInterface $target) { /** @var $themeFile \Magento\Theme\Model\Theme\File */ foreach ($target->getCustomization()->getFiles() as $themeFile) { $themeFile->delete(); } /** @var $newFile \Magento\Theme\Model\Theme\File */ foreach ($source->getCustomization()->getFiles() as $themeFile) { /** @var $newThemeFile \Magento\Theme\Model\Theme\File */ $newThemeFile = $this->_fileFactory->create(); $newThemeFile->setData(['theme_id' => $target->getId(), 'file_path' => $themeFile->getFilePath(), 'file_type' => $themeFile->getFileType(), 'content' => $themeFile->getContent(), 'sort_order' => $themeFile->getData('sort_order')]); $newThemeFile->save(); } }
/** * Creates or updates custom single file which belong to a selected theme * * @param \Magento\Framework\View\Design\ThemeInterface $themeModel * @param string $fileContent * @return \Magento\Framework\View\Design\Theme\FileInterface */ public function update(\Magento\Framework\View\Design\ThemeInterface $themeModel, $fileContent) { $customFiles = $themeModel->getCustomization()->getFilesByType($this->_fileService->getType()); $customCss = reset($customFiles); if (empty($fileContent) && $customCss) { $customCss->delete(); return $customCss; } if (!$customCss) { $customCss = $this->_fileService->create(); } $customCss->setData('content', $fileContent); $customCss->setTheme($themeModel); $customCss->save(); return $customCss; }
/** * Save preview image for theme * * @param ThemeInterface $theme * @return $this */ protected function _savePreviewImage(ThemeInterface $theme) { $themeDirectory = $theme->getCustomization()->getThemeFilesPath(); if (!$theme->getPreviewImage() || !$themeDirectory) { return $this; } $imagePath = $themeDirectory . '/' . $theme->getPreviewImage(); if (0 === strpos($imagePath, $themeDirectory)) { $theme->getThemeImage()->createPreviewImage($imagePath); } return $this; }
/** * Get theme file (with custom CSS) * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @return \Magento\Framework\View\Design\Theme\FileInterface|null */ protected function _getCustomCss($theme) { $files = $theme->getCustomization()->getFilesByType(\Magento\Theme\Model\Theme\Customization\File\CustomCss::TYPE); return reset($files); }