themeImageFactory = $themeImageFactory; $this->updateCollection = $updateCollection; $this->themeConfig = $themeConfig; } /** * Clean related contents to a theme (before save) * * @param EventObserver $observer * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function execute(EventObserver $observer) { $theme = $observer->getEvent()->getData('theme'); if (!($theme instanceof \Magento\Framework\View\Design\ThemeInterface)) { return; } /** @var $theme \Magento\Framework\View\Design\ThemeInterface */ if ($this->themeConfig->isThemeAssignedToStore($theme)) { throw new \Magento\Framework\Exception\LocalizedException(__('Theme isn\'t deletable.')); } $this->themeImageFactory->create(['theme' => $theme])->removePreviewImage(); $this->updateCollection->addThemeFilter($theme->getId())->delete(); } }