diff --git a/CHANGELOG.md b/CHANGELOG.md index 888d096..ef57e9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.3.0] - 1 July 2024 +### Added +- Add `--reset` flag to `theme:change` command +- Add new command `theme:design_config` + ## [1.2.1] - 6 February 2024 ### Fixed - namespace correction for InputOption #3 @meminuygur diff --git a/Console/Command/ThemeDesignConfigCommand.php b/Console/Command/ThemeDesignConfigCommand.php new file mode 100644 index 0000000..d450e86 --- /dev/null +++ b/Console/Command/ThemeDesignConfigCommand.php @@ -0,0 +1,129 @@ +setName('theme:design_config'); + $this->setDescription('Show currently active themes'); + parent::configure(); + } + + /** + * CLI command description. + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + * @throws Throwable + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $table = new Table($output); + $table->setHeaders([ + 'Website', + 'Website ID', + 'Store View', + 'Store View ID', + 'Theme', + 'Theme ID', + 'Override', + ]); + + $designCollection = $this->designCollectionFactory->create(); + foreach ($designCollection as $designConfig) { + try { + $website = $this->websiteRepository->get($designConfig->getStoreWebsiteId()); + $websiteId = $website->getId(); + $websiteName = $website->getName(); + } catch (\Exception $exception) { + $websiteId = ''; + $websiteName = ''; + } + + try { + $store = $this->storeRepository->getById($designConfig->getStoreId()); + $storeId = $store->getId(); + $storeName = $store->getName(); + } catch (\Exception $exception) { + $storeId = ''; + $storeName = ''; + } + + $theme = $this->themeProvider->getThemeById($designConfig->getThemeThemeId()); + $themeId = $theme->getId(); + $themeName = $theme->getCode(); + + $override = null; + if ($websiteId > 0) { + $override = $this->isCustomized('websites', $websiteId, $themeId) ? 'x' : ''; + } + + if ($storeId > 0) { + $override = $this->isCustomized('stores', $storeId, $themeId) ? 'x' : ''; + } + + $table->addRow([ + $websiteId, + $websiteName, + $storeId, + $storeName, + $themeId, + $themeName, + $override + ]); + } + + $table->render(); + + return Command::SUCCESS; + } + + private function isCustomized(string $scope, $scopeId, $value): bool + { + if ($scope === 'default') { + return false; + } + + $connection = $this->resourceConnection->getConnection(); + $table = $this->resourceConnection->getTableName('core_config_data'); + $query = 'SELECT `value` FROM `'.$table.'`'; + $query .= ' WHERE `path` = "design/theme/theme_id" AND `scope` = "'.$scope.'" AND `scope_id` = "'.$scopeId.'"'; + $col = $connection->fetchCol($query); + + if (empty($col)) { + return false; + } + + return true; + } +} diff --git a/README.md b/README.md index c8491da..64d7f00 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,23 @@ List all themes: bin/magento theme:list ``` +List all assigned themes (aka design configurations): +```bash +bin/magento theme:design_config +``` + +*The `theme:design_config` output also shows an **Override** column, which identifies whether a specific value (like, a theme ID for a specific Store View) is indeed overriding the default or not.* + Change the current theme to `Magento/luma` for all scopes: ```bash bin/magento theme:change Magento/luma ``` +Note that the `theme:change` command also includes a flag `--reset` (valid only without additional parameters) which resets all stores to the default, so that only 1 theme is active: +```bash +bin/magento theme:change --reset -- Magento/luma +``` + Change the current theme to `Hyva/default` for the StoreView with ID 1: ```bash bin/magento theme:change Hyva/default 1 stores @@ -28,4 +40,4 @@ Create a new theme: ```bash bin/magento theme:create --theme Yireo/example --parent Magento/luma --application frontend bin/magento theme:change Yireo/example -``` \ No newline at end of file +``` diff --git a/composer.json b/composer.json index ec168cc..42b4491 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "yireo/magento2-theme-commands", - "version": "1.2.1", + "version": "1.3.0", "license": "OSL-3.0", "description": "CLI commands to manipulate themes", "type": "magento2-module", diff --git a/etc/di.xml b/etc/di.xml index 1b59e1a..551b49e 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -7,6 +7,7 @@ Yireo\ThemeCommands\Console\Command\ThemeChangeCommand Yireo\ThemeCommands\Console\Command\ThemeCreateCommand Yireo\ThemeCommands\Console\Command\ThemeListCommand + Yireo\ThemeCommands\Console\Command\ThemeDesignConfigCommand