0

I am writing setup script for the theme configuration but stuck at how to set default cms home page for this theme.

Here is the script that is setting my theme as default theme. Setup script is working fine. app/code/RLTSquare/CmsInstall/Setup/InstallData.php

<?php

namespace RLTSquare\CmsInstall\Setup;

use Magento\Framework\Setup\InstallDataInterface;  
use Magento\Framework\Setup\ModuleContextInterface;  
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\Store;

class InstallData implements InstallDataInterface  
{
    const THEME_NAME = 'Rlts/brokertheme';

     /**
     * @var \Magento\Theme\Model\Config
     */
    private $config;

    /**
     * @var \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory
     */
    private $collectionFactory;


    public function __construct(
        \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $collectionFactory,
        \Magento\Theme\Model\Config $config,
        \Magento\Framework\App\Config\ConfigResource\ConfigInterface  $resourceConfig,
    ) {
        $this->resourceConfig = $resourceConfig;
        $this->collectionFactory = $collectionFactory;
        $this->config = $config;  

    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $themeID = $this->getThemeId();
        $this->resourceConfig->saveConfig('design/theme/theme_id',$themeID,'default','0');       
        $setup->endSetup();
    }

     protected function getThemeId()
    {
        $themes = $this->collectionFactory->create()->loadRegisteredThemes();
        /**
         * @var \Magento\Theme\Model\Theme $theme
         */
        $themeId=NULL;
        foreach ($themes as $theme) {
            if ($theme->getCode() == self::THEME_NAME) {
                $themeId = $theme->getid();
                break;
            }
        }
        return $themeId;
    }
}
  • Hi Shahbaz, please add the code that you have tried with the question so that, we can see it and help you better. – Rajeev K Tomy Sep 22 '17 at 19:04
  • @RajeevKTomy kindly check the code – Muhammad Shahbaz Sep 22 '17 at 19:36
0

Try following way:

RLTSquare/CmsInstall/Setup/UpgradeData.php


namespace RLTSquare\CmsInstall\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\PageFactory;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var PageFactory
     */
    private $pageFactory;
    /**
     * @param PageFactory $pageFactory
     */
    public function __construct(PageFactory $pageFactory)
    {
        $this->pageFactory = $pageFactory;
    }

    /**
     * Upgrades data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '2.0.2', '<')) {
            $homePage = $this->pageFactory->create()->load(
                'home',
                'identifier'
            );

            if ($homePage->getId()) {
                $homePage->setCustomTheme(2);// 2 => theme id
                $homePage->save();
            }
        }
        $setup->endSetup();
    }
}

This upgrade data run if module version is 2.0.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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