In the Last tutorial we have learned how to create a new custom console command in Magento2. This Article is a followup of our previous article, those who want to check the previous article please click the link below.
http://mydons.com/magento-2-create-custom-console-command-with-sample-usecase
Registering the New Console Command
In Order to create a new console command it needs to be registered in the di.xml.
In Addition to the existing command which lists the installed themes in the site, we need to add the new command.
1 2 3 |
<item name="mydons_themeconsolecmd_command_settheme" xsi:type="object"> Mydons\ThemeConsoleCmd\Command\SetTheme </item> |
The di.xml will look as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="mydons_themeconsolecmd_command_listthemes" xsi:type="object">Mydons\ThemeConsoleCmd\Command\ListThemes </item> <item name="mydons_themeconsolecmd_command_settheme" xsi:type="object">Mydons\ThemeConsoleCmd\Command\SetTheme </item> </argument> </arguments> </type> </config> |
Workflow of the new console command
The new console command will accept the theme code as an argument, the corresponding THEME
will be assigned to the frontend, if the theme is not found a failure message will be displayed.
Creating a subclass for registering new command
The Subclass for settheme is created with the below name
Mydons\ThemeConsoleCmd\Command\SetTheme
Defining the console command details
As per the convention our new subclass will contain two methods configure and execute.
The configure method will contain the command name,description,argument etc.
1 2 3 4 5 6 7 8 9 10 11 12 |
protected function configure() { $this->setName("mydons:settheme"); $this->setDescription("A Custom Command to set the desired theme in Frontend via CLI "); $this->addArgument( 'theme_code', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'theme code. Theme Code Should be specified' . ' For example, Magento/blank' ); parent::configure(); } |
Command Execution
The execute method accepts the theme_code as an Input and loads the theme.
The Loaded theme is assigned to the default store.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
protected function execute(InputInterface $input, OutputInterface $output) { $themeCode = $input->getArgument('theme_code'); $theme = $this->themeFactory->create()->load($themeCode, 'code'); if ($theme->getId()) { $this->themeConfig->assignToStore( $theme, [Store::DEFAULT_STORE_ID], ScopeConfigInterface::SCOPE_TYPE_DEFAULT ); $this->cache->clean(); $this->indexerRegistry->get(DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID)->reindexAll(); $output->writeln("<info>Theme " .$theme->getThemeTitle()." is Enabled</info>"); $output->writeln("<info>Cache Cleared Successfully</info>"); $output->writeln("<info>Design Grid Reindexed Successfully</info>"); $output->writeln("<info>Please run setup:static-content:deploy -f command for a newly installed theme</info>"); } else { $output->writeln("Unknown Theme or Package Provided"); } } |
Implemented Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php namespace Mydons\ThemeConsoleCmd\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Magento\Framework\App\Cache; use Magento\Theme\Model\Config as ThemeConfig; use Magento\Store\Model\Store; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Theme\Model\Theme\Registration as ThemeRegistration; use Magento\Theme\Model\ThemeFactory as ThemeFactory; use Magento\Framework\Indexer\IndexerRegistry as IndexerRegistry; use Magento\Theme\Model\Data\Design\Config as DesignConfig; class SetTheme extends Command { protected $themeFactory; protected $themeConfig; protected $themeRegistration; private $cache; protected $indexerRegistry; public function __construct( ThemeFactory $themeFactory, ThemeConfig $themeConfig, ThemeRegistration $themeRegistration, Cache $cache, IndexerRegistry $indexerRegistry ) { $this->themeFactory = $themeFactory; $this->themeConfig = $themeConfig; $this->themeRegistration = $themeRegistration; $this->cache = $cache; $this->indexerRegistry = $indexerRegistry; return parent::__construct(); } protected function configure() { $this->setName("mydons:settheme"); $this->setDescription("A Custom Command to set the desired theme in Frontend via CLI "); $this->addArgument( 'theme_code', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'theme code. Theme Code Should be specified' . ' For example, Magento/blank' ); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output) { $themeCode = $input->getArgument('theme_code'); $theme = $this->themeFactory->create()->load($themeCode, 'code'); if ($theme->getId()) { $this->themeConfig->assignToStore( $theme, [Store::DEFAULT_STORE_ID], ScopeConfigInterface::SCOPE_TYPE_DEFAULT ); $this->cache->clean(); $this->indexerRegistry->get(DesignConfig::DESIGN_CONFIG_GRID_INDEXER_ID)->reindexAll(); $output->writeln("<info>Theme " .$theme->getThemeTitle()." is Enabled</info>"); $output->writeln("<info>Cache Cleared Successfully</info>"); $output->writeln("<info>Design Grid Reindexed Successfully</info>"); $output->writeln("<info>Please run setup:static-content:deploy -f command for a newly installed theme</info>"); } else { $output->writeln("Unknown Theme or Package Provided"); } } } |
Once the above change were done and cache clean we can see the help for the command function

Change Luma Theme to Blank Theme via command

Github Link