_theme = $theme; $this->_themeFactory = $themeFactory; $this->_themeCopyService = $themeCopyService; $this->_themeCollection = $themeCollection; } /** * Create theme customization * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @return \Magento\Framework\View\Design\ThemeInterface */ public function createVirtualTheme($theme) { $themeData = $theme->getData(); $themeData['parent_id'] = $theme->getId(); $themeData['theme_id'] = null; $themeData['theme_path'] = null; $themeData['theme_title'] = $this->_getVirtualThemeTitle($theme); $themeData['type'] = \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL; /** @var $themeCustomization \Magento\Framework\View\Design\ThemeInterface */ $themeCustomization = $this->_themeFactory->create()->setData($themeData); $themeCustomization->getThemeImage()->createPreviewImageCopy($theme); $themeCustomization->save(); $this->_themeCopyService->copy($theme, $themeCustomization); return $themeCustomization; } /** * Get virtual theme title * * @param \Magento\Framework\View\Design\ThemeInterface $theme * @return string */ protected function _getVirtualThemeTitle($theme) { $themeCopyCount = $this->_themeCollection->addAreaFilter( \Magento\Framework\App\Area::AREA_FRONTEND )->addTypeFilter( \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL )->addFilter( 'parent_id', $theme->getId() )->count(); $title = sprintf("%s - %s #%s", $theme->getThemeTitle(), __('Copy'), $themeCopyCount + 1); return $title; } }