custom/plugins/TcinnThemeWareLiving/src/TcinnThemeWareLiving.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TcinnThemeWareLiving;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Shopware\Storefront\Framework\ThemeInterface;
  12. use TcinnThemeWareLiving\CustomFields\CustomFieldUpdater;
  13. /* ThemeWare: Handle cms media services */
  14. use Symfony\Component\Config\FileLocator;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  17. class TcinnThemeWareLiving extends Plugin implements ThemeInterface
  18. {
  19.     // ToDo: Remove for apps in deployment
  20.     private function getCustomFieldUpdater()
  21.     {
  22.         /**
  23.          * @var EntityRepositoryInterface $customFieldSetRepository
  24.          */
  25.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  26.         /**
  27.          * @var EntityRepositoryInterface $customFieldRepository
  28.          */
  29.         $customFieldRepository $this->container->get('custom_field.repository');
  30.         return new CustomFieldUpdater(
  31.             $customFieldSetRepository,
  32.             $customFieldRepository,
  33.             $this->path
  34.         );
  35.     }
  36.     public function getThemeConfigPath(): string
  37.     {
  38.         return 'theme.json';
  39.     }
  40.     public function install(InstallContext $installContext): void
  41.     {
  42.         parent::install($installContext);
  43.         $this->getCustomFieldUpdater()->sync();
  44.     }
  45.     /**
  46.      * Copy preview media from main theme to child themes.
  47.      *
  48.      * @param UpdateContext $updateContext
  49.      */
  50.     public function postUpdate(UpdateContext $updateContext): void
  51.     {
  52.         /** @var EntityRepository $themeRepository */
  53.         $themeRepository $this->container->get('theme.repository');
  54.         $parentThemeCollection $themeRepository->search(
  55.             (new Criteria())->addFilter(new EqualsFilter('technicalName''TcinnThemeWareLiving')),
  56.             \Shopware\Core\Framework\Context::createDefaultContext()
  57.         );
  58.         if(!$parentThemeCollection) {
  59.             return;
  60.         }
  61.         $parentTheme $parentThemeCollection->first();
  62.         $childThemesCollection $themeRepository->search(
  63.             (new Criteria())->addFilter(new EqualsFilter('parentThemeId'$parentTheme->get('id'))),
  64.             \Shopware\Core\Framework\Context::createDefaultContext()
  65.         );
  66.         if(!$childThemesCollection) {
  67.             return;
  68.         }
  69.         foreach ($childThemesCollection->getElements() as $childTheme) {
  70.             if(!$childTheme->get('previewMediaId')) {
  71.                 $data = [
  72.                     [
  73.                         'id' => $childTheme->get('id'),
  74.                         'previewMediaId' => $parentTheme->get('previewMediaId')
  75.                     ]
  76.                 ];
  77.             } else {
  78.                 $data = [
  79.                     [
  80.                         'id' => $childTheme->get('id')
  81.                     ]
  82.                 ];
  83.             }
  84.             $themeRepository->update($data, \Shopware\Core\Framework\Context::createDefaultContext());
  85.         }
  86.     }
  87.     public function uninstall(UninstallContext $uninstallContext): void
  88.     {
  89.         parent::uninstall($uninstallContext);
  90.         if (!$uninstallContext->keepUserData()) {
  91.             $this->getCustomFieldUpdater()->remove();
  92.         }
  93.     }
  94.     public function update(UpdateContext $updateContext): void
  95.     {
  96.         parent::update($updateContext);
  97.         $this->getCustomFieldUpdater()->sync();
  98.     }
  99.     /**
  100.      * Skip rebuild container on activate/deactivate process
  101.      * to speedup Shopware Cloud bundle integration.
  102.      *
  103.      * @return bool
  104.      */
  105.     public function rebuildContainer(): bool
  106.     {
  107.         return false;
  108.     }
  109.     /**
  110.      * Load media.xml to add cms data resolver services
  111.      *
  112.      * @param ContainerBuilder $container
  113.      */
  114.     public function build(ContainerBuilder $container): void
  115.     {
  116.         parent::build($container);
  117.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/Core/Content/DependencyInjection'));
  118.         $loader->load('media.xml');
  119.     }
  120. }