1

I was trying to get the theme detail used by the Magento but got nothing till now. I am using this method still get nothing special.

  public static function ThemeDetail(){
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance()
                ->get('Magento\Framework\App\ResourceConnection');
             $connection= $objectManager->getConnection();
             $theme_table = $objectManager->getTableName('theme');   
             $theme_data = $connection->fetchAll("SELECT * FROM ".$theme_table);
             if(!empty($theme_data)){
                foreach($theme_data as $theme_name => $value){
                 $themeArray[] = array('name'=> $theme_data[$theme_name]['theme_title'], 'version'=>'2.0.5');    
                }

              return $themeArray; 
             } else {
              return FALSE; 
             }

      }
8

Your class will need to have dependency on the following:

\Magento\Framework\App\Config\ScopeConfigInterface
\Magento\Store\Model\StoreManagerInterface
\Magento\Framework\View\Design\Theme\ThemeProviderInterface

Use this method to get data for current theme:

public function getThemeData()
    {
        $themeId = $this->_scopeConfig->getValue(
            \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $this->_storeManager->getStore()->getId()
        );

        /** @var $theme \Magento\Framework\View\Design\ThemeInterface */
        $theme = $this->_themeProvider->getThemeById($themeId);

        return $theme->getData();
    }
2
  • For me the getThemeById is always returns null. In the factory the function has only 1 row, return null. I've tried to delete the generated (I am in developer mode) but no success. I've checked, $themeId is 5 and that is in the theme table and that is our current theme. – vaso123 Mar 19 '19 at 16:34
  • Ok, I've figured out. You need to use Magento\Theme\Model\Theme\ThemeProvider instead \Magento\Framework\View\Design\Theme\ThemeProvider – vaso123 Mar 20 '19 at 13:17
2

you have to just keep below content for your function,

Use directly objectmanager is not right way, use __construct() way to define your class inside block is better way to use coding standard.

code for below,

  public static function ThemeDetail(){
        $connection = $objectManager = \Magento\Framework\App\ObjectManager::getInstance()
                ->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION');

    $version = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');


             $theme_table = $connection->getTableName('theme');   
             $theme_data = $connection->fetchAll("SELECT * FROM ".$theme_table);

             $themeArray = array();
             if(!empty($theme_data)){
                foreach($theme_data as $value){ echo $value['theme_title'];
                 $themeArray[] = array('name'=> $value['theme_title'], 'version'=>$version->getVersion());    
                }
             } else {
                return FALSE; 
             }

           echo "<pre>";print_r($themeArray);exit;

      }
9
  • as you see here i don't want to give the version hard-coded actually. – Ankush Chauhan Jul 26 '16 at 10:36
  • you have defined 2.0.5 in your code – Rakesh Jesadiya Jul 26 '16 at 10:43
  • yes, this was defined by me but is there any way to get the theme version by programming not hard coded.Themes names get from the database but there is no version specified. – Ankush Chauhan Jul 26 '16 at 10:49
  • 2
    yes its display version of your magento, not the theme version – Rakesh Jesadiya Jul 26 '16 at 11:54
  • 2
    i dont know for theme version, i havent seen theme version in magento 2 – Rakesh Jesadiya Jul 26 '16 at 11:56
1

Use for Theme object

$this->_design->getDesignTheme()

and theme id:

$this->_design->getDesignTheme()->getId()

where $this->_design instance of Magento\Framework\View\DesignInterface

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.