Skip to content

Commit

Permalink
Add new command theme:design_config
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Jul 1, 2024
1 parent 281a703 commit 59df7e6
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
129 changes: 129 additions & 0 deletions Console/Command/ThemeDesignConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php declare(strict_types=1);

namespace Yireo\ThemeCommands\Console\Command;

use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreRepository;
use Magento\Store\Model\WebsiteRepository;
use Magento\Theme\Model\ResourceModel\Design\Config\Scope\CollectionFactory;
use Magento\Theme\Model\Theme\ThemeProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

class ThemeDesignConfigCommand extends Command
{

public function __construct(
private CollectionFactory $designCollectionFactory,
private WebsiteRepository $websiteRepository,
private StoreRepository $storeRepository,
private ThemeProvider $themeProvider,
private ResourceConnection $resourceConnection,
string $name = null
) {
parent::__construct($name);
}

/**
* Initialization of the command.
*/
protected function configure(): void
{
$this->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;
}
}
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<item name="yireo_themecommands_theme_change_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeChangeCommand</item>
<item name="yireo_themecommands_theme_create_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeCreateCommand</item>
<item name="yireo_themecommands_theme_list_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeListCommand</item>
<item name="yireo_themecommands_theme_design_config_command" xsi:type="object">Yireo\ThemeCommands\Console\Command\ThemeDesignConfigCommand</item>
</argument>
</arguments>
</type>
Expand Down

0 comments on commit 59df7e6

Please sign in to comment.