Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Switcher.php
Go to the documentation of this file.
1 <?php
11 
13 
15 {
19  protected $_groups = [];
20 
24  protected $_stores = [];
25 
29  protected $_loaded = false;
30 
36  protected $_storeFactory;
37 
44 
51  public function __construct(
52  \Magento\Framework\View\Element\Template\Context $context,
53  \Magento\Store\Model\GroupFactory $storeGroupFactory,
54  \Magento\Store\Model\StoreFactory $storeFactory,
55  array $data = []
56  ) {
57  $this->_storeGroupFactory = $storeGroupFactory;
58  $this->_storeFactory = $storeFactory;
59  parent::__construct($context, $data);
60  }
61 
65  protected function _construct()
66  {
67  $this->_loadData();
68  $this->setStores([]);
69  $this->setLanguages([]);
70  return parent::_construct();
71  }
72 
76  protected function _loadData()
77  {
78  if ($this->_loaded) {
79  return $this;
80  }
81 
82  $websiteId = $this->_storeManager->getStore()->getWebsiteId();
83  $storeCollection = $this->_storeFactory->create()->getCollection()->addWebsiteFilter($websiteId);
84  $groupCollection = $this->_storeGroupFactory->create()->getCollection()->addWebsiteFilter($websiteId);
85  foreach ($groupCollection as $group) {
86  $this->_groups[$group->getId()] = $group;
87  }
89  foreach ($storeCollection as $store) {
90  if (!$store->isActive()) {
91  continue;
92  }
93  $store->setLocaleCode($this->_scopeConfig->getValue(
95  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
96  $store->getId()
97  ));
98  $this->_stores[$store->getGroupId()][$store->getId()] = $store;
99  }
100 
101  $this->_loaded = true;
102 
103  return $this;
104  }
105 
109  public function getStoreCount()
110  {
111  $stores = [];
112  $localeCode = $this->_scopeConfig->getValue(
114  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
115  );
116  foreach ($this->_groups as $group) {
117  if (!isset($this->_stores[$group->getId()])) {
118  continue;
119  }
120  $useStore = false;
121  foreach ($this->_stores[$group->getId()] as $store) {
122  if ($store->getLocaleCode() == $localeCode) {
123  $useStore = true;
124  $stores[] = $store;
125  }
126  }
127  if (!$useStore && isset($this->_stores[$group->getId()][$group->getDefaultStoreId()])) {
128  $stores[] = $this->_stores[$group->getId()][$group->getDefaultStoreId()];
129  }
130  }
131 
132  $this->setStores($stores);
133  return count($this->getStores());
134  }
135 
139  public function getLanguageCount()
140  {
141  $groupId = $this->_storeManager->getStore()->getGroupId();
142  if (!isset($this->_stores[$groupId])) {
143  $this->setLanguages([]);
144  return 0;
145  }
146  $this->setLanguages($this->_stores[$groupId]);
147  return count($this->getLanguages());
148  }
149 
153  public function getCurrentStoreId()
154  {
155  return $this->_storeManager->getStore()->getId();
156  }
157 
161  public function getCurrentStoreCode()
162  {
163  return $this->_storeManager->getStore()->getCode();
164  }
165 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Store\Model\GroupFactory $storeGroupFactory, \Magento\Store\Model\StoreFactory $storeFactory, array $data=[])
Definition: Switcher.php:51
$group
Definition: sections.phtml:16