1

In magento 1.X we can change theme programmatically in controller action by below code :

Mage::getDesign()->setArea('frontend') //Area (frontend|adminhtml)
    ->setPackageName('packagename') //Name of Package
    ->setTheme('default');// Name of theme

How to change theme programmatically for specific controller in magento 2 ?

3 Answers 3

3

After digging the code I found the solution.

Write below code at the starting of your controller's action execute function's body.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get(\Magento\Framework\View\DesignInterface::class)->setDesignTheme('Magento/luma', 'frontend');

For example :

public function execute()
    {

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $objectManager->get(\Magento\Framework\View\DesignInterface::class)->setDesignTheme('Magento/luma', 'frontend');

        $post = $this->initModel();

        if (!$post) {
            throw new NotFoundException(__('Page not found'));
            die;
        }

        /* @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

        $this->_eventManager->dispatch(
            'blog_page_render',
            ['post' => $post, 'controller_action' => $this]
        );

        return $resultPage;
    }

In setDesignTheme pass your theme's name and area (frontend|adminhtml) as :

$objectManager->get(\Magento\Framework\View\DesignInterface::class)->setDesignTheme('Theme_Name', 'Area');

Hope this will help others :)

7
  • i used this code but css and js no loading have you done those also if done can you pls tell me
    – Pawankumar
    Jun 20, 2019 at 20:42
  • run following commands : Jun 21, 2019 at 8:49
  • which commands your saying
    – Pawankumar
    Jun 21, 2019 at 8:52
  • rm -rf pub/static/* /*to remove files excluding .htaccess*/ rm -rf var/view_preprocessed/* /*to remove files excluding .htaccess*/ php bin/magento setup:static-content:deploy -f --theme Vendor/theme Jun 21, 2019 at 8:54
  • I did these already and files are created in pub/static but not showing in frontend.you did right that time you got all files loaded ? $objectManager->get(\Magento\Framework\View\DesignInterface::class)->setDesignTheme('Infortis/pawan', 'frontend');
    – Pawankumar
    Jun 21, 2019 at 8:57
0

See vendor/magento/framework/App/Area.php

It uses: $this->_getDesign()->setArea($this->_code)->setDesignTheme($theme);

and

            $this->_design->loadChange(
                $this->_scopeResolver->getScope()->getId()
            )->changeDesign(
                $this->_getDesign()
            );

loadChange and changeDesign are defined in vendor/magento/module-theme/Model/Design.php

/**
 * Apply design change from self data into specified design package instance
 *
 * @param \Magento\Framework\View\DesignInterface $packageInto
 * @return $this
 */
public function changeDesign(\Magento\Framework\View\DesignInterface $packageInto)
{
    $design = $this->getDesign();
    if ($design) {
        $packageInto->setDesignTheme($design);
    }
    return $this;
}

I hope this helps.

0
0

If you want to set theme for specific website in controller then use below code in your controller file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$config = $objectManager->get("\Magento\Theme\Model\Config"); 
$storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

//I have used default luma theme ID, You can change it based on theme.
$themeId = 2; 
$theme = $objectManager->get("\Magento\Theme\Model\ThemeFactory")->create()->load($themeId);

$websiteId = $storeManager->getWebsite()->getId();
if ($theme->getId()) {
    $config->assignToStore($theme,[$websiteId],'website');
}

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.