Skip to content

Commit 59df7e6

Browse files
committed
Add new command theme:design_config
1 parent 281a703 commit 59df7e6

File tree

5 files changed

+149
-2
lines changed

5 files changed

+149
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.3.0] - 1 July 2024
10+
### Added
11+
- Add `--reset` flag to `theme:change` command
12+
- Add new command `theme:design_config`
13+
914
## [1.2.1] - 6 February 2024
1015
### Fixed
1116
- namespace correction for InputOption #3 @meminuygur
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Yireo\ThemeCommands\Console\Command;
4+
5+
use Magento\Framework\App\ResourceConnection;
6+
use Magento\Store\Model\StoreRepository;
7+
use Magento\Store\Model\WebsiteRepository;
8+
use Magento\Theme\Model\ResourceModel\Design\Config\Scope\CollectionFactory;
9+
use Magento\Theme\Model\Theme\ThemeProvider;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Helper\Table;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Throwable;
15+
16+
class ThemeDesignConfigCommand extends Command
17+
{
18+
19+
public function __construct(
20+
private CollectionFactory $designCollectionFactory,
21+
private WebsiteRepository $websiteRepository,
22+
private StoreRepository $storeRepository,
23+
private ThemeProvider $themeProvider,
24+
private ResourceConnection $resourceConnection,
25+
string $name = null
26+
) {
27+
parent::__construct($name);
28+
}
29+
30+
/**
31+
* Initialization of the command.
32+
*/
33+
protected function configure(): void
34+
{
35+
$this->setName('theme:design_config');
36+
$this->setDescription('Show currently active themes');
37+
parent::configure();
38+
}
39+
40+
/**
41+
* CLI command description.
42+
*
43+
* @param InputInterface $input
44+
* @param OutputInterface $output
45+
*
46+
* @return int
47+
* @throws Throwable
48+
*/
49+
protected function execute(InputInterface $input, OutputInterface $output): int
50+
{
51+
$table = new Table($output);
52+
$table->setHeaders([
53+
'Website',
54+
'Website ID',
55+
'Store View',
56+
'Store View ID',
57+
'Theme',
58+
'Theme ID',
59+
'Override',
60+
]);
61+
62+
$designCollection = $this->designCollectionFactory->create();
63+
foreach ($designCollection as $designConfig) {
64+
try {
65+
$website = $this->websiteRepository->get($designConfig->getStoreWebsiteId());
66+
$websiteId = $website->getId();
67+
$websiteName = $website->getName();
68+
} catch (\Exception $exception) {
69+
$websiteId = '';
70+
$websiteName = '';
71+
}
72+
73+
try {
74+
$store = $this->storeRepository->getById($designConfig->getStoreId());
75+
$storeId = $store->getId();
76+
$storeName = $store->getName();
77+
} catch (\Exception $exception) {
78+
$storeId = '';
79+
$storeName = '';
80+
}
81+
82+
$theme = $this->themeProvider->getThemeById($designConfig->getThemeThemeId());
83+
$themeId = $theme->getId();
84+
$themeName = $theme->getCode();
85+
86+
$override = null;
87+
if ($websiteId > 0) {
88+
$override = $this->isCustomized('websites', $websiteId, $themeId) ? 'x' : '';
89+
}
90+
91+
if ($storeId > 0) {
92+
$override = $this->isCustomized('stores', $storeId, $themeId) ? 'x' : '';
93+
}
94+
95+
$table->addRow([
96+
$websiteId,
97+
$websiteName,
98+
$storeId,
99+
$storeName,
100+
$themeId,
101+
$themeName,
102+
$override
103+
]);
104+
}
105+
106+
$table->render();
107+
108+
return Command::SUCCESS;
109+
}
110+
111+
private function isCustomized(string $scope, $scopeId, $value): bool
112+
{
113+
if ($scope === 'default') {
114+
return false;
115+
}
116+
117+
$connection = $this->resourceConnection->getConnection();
118+
$table = $this->resourceConnection->getTableName('core_config_data');
119+
$query = 'SELECT `value` FROM `'.$table.'`';
120+
$query .= ' WHERE `path` = "design/theme/theme_id" AND `scope` = "'.$scope.'" AND `scope_id` = "'.$scopeId.'"';
121+
$col = $connection->fetchCol($query);
122+
123+
if (empty($col)) {
124+
return false;
125+
}
126+
127+
return true;
128+
}
129+
}

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ List all themes:
1414
bin/magento theme:list
1515
```
1616

17+
List all assigned themes (aka design configurations):
18+
```bash
19+
bin/magento theme:design_config
20+
```
21+
22+
*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.*
23+
1724
Change the current theme to `Magento/luma` for all scopes:
1825
```bash
1926
bin/magento theme:change Magento/luma
2027
```
2128

29+
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:
30+
```bash
31+
bin/magento theme:change --reset -- Magento/luma
32+
```
33+
2234
Change the current theme to `Hyva/default` for the StoreView with ID 1:
2335
```bash
2436
bin/magento theme:change Hyva/default 1 stores
@@ -28,4 +40,4 @@ Create a new theme:
2840
```bash
2941
bin/magento theme:create --theme Yireo/example --parent Magento/luma --application frontend
3042
bin/magento theme:change Yireo/example
31-
```
43+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yireo/magento2-theme-commands",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"license": "OSL-3.0",
55
"description": "CLI commands to manipulate themes",
66
"type": "magento2-module",

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<item name="yireo_themecommands_theme_change_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeChangeCommand</item>
88
<item name="yireo_themecommands_theme_create_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeCreateCommand</item>
99
<item name="yireo_themecommands_theme_list_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeListCommand</item>
10+
<item name="yireo_themecommands_theme_design_config_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeDesignConfigCommand</item>
1011
</argument>
1112
</arguments>
1213
</type>

0 commit comments

Comments
 (0)