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
10 namespace Magento\Store\Block;
11 
17 use Magento\Framework\Url\Helper\Data as UrlHelper;
18 
26 {
30  protected $_storeInUrl;
31 
35  protected $_postDataHelper;
36 
40  private $urlHelper;
41 
48  public function __construct(
49  \Magento\Framework\View\Element\Template\Context $context,
50  \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
51  array $data = [],
52  UrlHelper $urlHelper = null
53  ) {
54  $this->_postDataHelper = $postDataHelper;
55  parent::__construct($context, $data);
56  $this->urlHelper = $urlHelper ?: ObjectManager::getInstance()->get(UrlHelper::class);
57  }
58 
64  public function getCurrentWebsiteId()
65  {
66  return $this->_storeManager->getStore()->getWebsiteId();
67  }
68 
74  public function getCurrentGroupId()
75  {
76  return $this->_storeManager->getStore()->getGroupId();
77  }
78 
84  public function getCurrentStoreId()
85  {
86  return $this->_storeManager->getStore()->getId();
87  }
88 
94  public function getRawGroups()
95  {
96  if (!$this->hasData('raw_groups')) {
97  $websiteGroups = $this->_storeManager->getWebsite()->getGroups();
98 
99  $groups = [];
100  foreach ($websiteGroups as $group) {
101  $groups[$group->getId()] = $group;
102  }
103  $this->setData('raw_groups', $groups);
104  }
105  return $this->getData('raw_groups');
106  }
107 
113  public function getRawStores()
114  {
115  if (!$this->hasData('raw_stores')) {
116  $websiteStores = $this->_storeManager->getWebsite()->getStores();
117  $stores = [];
118  foreach ($websiteStores as $store) {
119  /* @var $store \Magento\Store\Model\Store */
120  if (!$store->isActive()) {
121  continue;
122  }
123  $localeCode = $this->_scopeConfig->getValue(
125  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
126  $store
127  );
128  $store->setLocaleCode($localeCode);
129  $params = ['_query' => []];
130  if (!$this->isStoreInUrl()) {
131  $params['_query']['___store'] = $store->getCode();
132  }
133  $baseUrl = $store->getUrl('', $params);
134 
135  $store->setHomeUrl($baseUrl);
136  $stores[$store->getGroupId()][$store->getId()] = $store;
137  }
138  $this->setData('raw_stores', $stores);
139  }
140  return $this->getData('raw_stores');
141  }
142 
148  public function getGroups()
149  {
150  if (!$this->hasData('groups')) {
151  $rawGroups = $this->getRawGroups();
152  $rawStores = $this->getRawStores();
153 
154  $groups = [];
155  $localeCode = $this->_scopeConfig->getValue(
157  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
158  );
159  foreach ($rawGroups as $group) {
160  /* @var $group Group */
161  if (!isset($rawStores[$group->getId()])) {
162  continue;
163  }
164  if ($group->getId() == $this->getCurrentGroupId()) {
165  $groups[] = $group;
166  continue;
167  }
168 
169  $store = $group->getDefaultStoreByLocale($localeCode);
170 
171  if ($store) {
172  $group->setHomeUrl($store->getHomeUrl());
173  $groups[] = $group;
174  }
175  }
176  $this->setData('groups', $groups);
177  }
178  return $this->getData('groups');
179  }
180 
186  public function getStores()
187  {
188  if (!$this->getData('stores')) {
189  $rawStores = $this->getRawStores();
190 
191  $groupId = $this->getCurrentGroupId();
192  if (!isset($rawStores[$groupId])) {
193  $stores = [];
194  } else {
195  $stores = $rawStores[$groupId];
196  }
197  $this->setData('stores', $stores);
198  }
199  return $this->getData('stores');
200  }
201 
207  public function getCurrentStoreCode()
208  {
209  return $this->_storeManager->getStore()->getCode();
210  }
211 
217  public function isStoreInUrl()
218  {
219  if ($this->_storeInUrl === null) {
220  $this->_storeInUrl = $this->_storeManager->getStore()->isUseStoreInUrl();
221  }
222  return $this->_storeInUrl;
223  }
224 
230  public function getStoreCode()
231  {
232  return $this->_storeManager->getStore()->getCode();
233  }
234 
240  public function getStoreName()
241  {
242  return $this->_storeManager->getStore()->getName();
243  }
244 
253  public function getTargetStorePostData(Store $store, $data = [])
254  {
256  $data['___from_store'] = $this->_storeManager->getStore()->getCode();
257 
258  $urlOnTargetStore = $store->getCurrentUrl(false);
259  $data[ActionInterface::PARAM_NAME_URL_ENCODED] = $this->urlHelper->getEncodedUrl($urlOnTargetStore);
260 
261  $url = $this->getUrl('stores/store/redirect');
262 
263  return $this->_postDataHelper->getPostData(
264  $url,
265  $data
266  );
267  }
268 }
getData($key='', $index=null)
Definition: DataObject.php:119
getTargetStorePostData(Store $store, $data=[])
Definition: Switcher.php:253
$group
Definition: sections.phtml:16
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, array $data=[], UrlHelper $urlHelper=null)
Definition: Switcher.php:48
setData($key, $value=null)
Definition: DataObject.php:72
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18