Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Customization.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $_storeManager;
17 
21  protected $_design;
22 
26  protected $themeProvider;
27 
34  protected $_assignedTheme;
35 
42  protected $_unassignedTheme;
43 
49  public function __construct(
51  \Magento\Framework\View\DesignInterface $design,
53  ) {
54  $this->_storeManager = $storeManager;
55  $this->_design = $design;
56  $this->themeProvider = $themeProvider;
57  }
58 
66  {
67  if ($this->_assignedTheme === null) {
68  $this->_prepareThemeCustomizations();
69  }
70  return $this->_assignedTheme;
71  }
72 
80  {
81  if ($this->_unassignedTheme === null) {
82  $this->_prepareThemeCustomizations();
83  }
85  }
86 
92  public function getStoresByThemes()
93  {
94  $storesByThemes = [];
95  $stores = $this->_storeManager->getStores();
97  foreach ($stores as $store) {
98  $themeId = $this->_getConfigurationThemeId($store);
99  if (!isset($storesByThemes[$themeId])) {
100  $storesByThemes[$themeId] = [];
101  }
102  $storesByThemes[$themeId][] = $store;
103  }
104  return $storesByThemes;
105  }
106 
114  public function isThemeAssignedToStore($theme, $store = null)
115  {
116  if (null === $store) {
117  $assignedThemes = $this->getAssignedThemeCustomizations();
118  return isset($assignedThemes[$theme->getId()]);
119  }
120  return $this->_isThemeAssignedToSpecificStore($theme, $store);
121  }
122 
128  public function hasThemeAssigned()
129  {
130  return count($this->getAssignedThemeCustomizations()) > 0;
131  }
132 
140  protected function _isThemeAssignedToSpecificStore($theme, $store)
141  {
142  return $theme->getId() == $this->_getConfigurationThemeId($store);
143  }
144 
151  protected function _getConfigurationThemeId($store)
152  {
153  return $this->_design->getConfigurationDesignTheme(
154  \Magento\Framework\App\Area::AREA_FRONTEND,
155  ['store' => $store]
156  );
157  }
158 
168  protected function _prepareThemeCustomizations()
169  {
171  $themeCollection = $this->themeProvider->getThemeCustomizations(\Magento\Framework\App\Area::AREA_FRONTEND);
172 
173  $assignedThemes = $this->getStoresByThemes();
174 
175  $this->_assignedTheme = [];
176  $this->_unassignedTheme = [];
177 
179  foreach ($themeCollection as $theme) {
180  if (isset($assignedThemes[$theme->getId()])) {
181  $theme->setAssignedStores($assignedThemes[$theme->getId()]);
182  $this->_assignedTheme[$theme->getId()] = $theme;
183  } else {
184  $this->_unassignedTheme[$theme->getId()] = $theme;
185  }
186  }
187 
188  return $this;
189  }
190 }
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\View\DesignInterface $design, \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider)
$storeManager
$theme