Switcher.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Store and language switcher block
  8. */
  9. namespace Magento\Store\Block;
  10. use Magento\Directory\Helper\Data;
  11. use Magento\Store\Model\Group;
  12. use Magento\Store\Model\Store;
  13. use Magento\Framework\App\ActionInterface;
  14. use Magento\Framework\App\ObjectManager;
  15. use Magento\Framework\Url\Helper\Data as UrlHelper;
  16. /**
  17. * Switcher block
  18. *
  19. * @api
  20. * @since 100.0.2
  21. */
  22. class Switcher extends \Magento\Framework\View\Element\Template
  23. {
  24. /**
  25. * @var bool
  26. */
  27. protected $_storeInUrl;
  28. /**
  29. * @var \Magento\Framework\Data\Helper\PostHelper
  30. */
  31. protected $_postDataHelper;
  32. /**
  33. * @var UrlHelper
  34. */
  35. private $urlHelper;
  36. /**
  37. * @param \Magento\Framework\View\Element\Template\Context $context
  38. * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
  39. * @param array $data
  40. * @param UrlHelper $urlHelper
  41. */
  42. public function __construct(
  43. \Magento\Framework\View\Element\Template\Context $context,
  44. \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
  45. array $data = [],
  46. UrlHelper $urlHelper = null
  47. ) {
  48. $this->_postDataHelper = $postDataHelper;
  49. parent::__construct($context, $data);
  50. $this->urlHelper = $urlHelper ?: ObjectManager::getInstance()->get(UrlHelper::class);
  51. }
  52. /**
  53. * Get current website Id.
  54. *
  55. * @return int|null|string
  56. */
  57. public function getCurrentWebsiteId()
  58. {
  59. return $this->_storeManager->getStore()->getWebsiteId();
  60. }
  61. /**
  62. * Get current group Id.
  63. *
  64. * @return int|null|string
  65. */
  66. public function getCurrentGroupId()
  67. {
  68. return $this->_storeManager->getStore()->getGroupId();
  69. }
  70. /**
  71. * Get current Store Id.
  72. *
  73. * @return int
  74. */
  75. public function getCurrentStoreId()
  76. {
  77. return $this->_storeManager->getStore()->getId();
  78. }
  79. /**
  80. * Get raw groups.
  81. *
  82. * @return array
  83. */
  84. public function getRawGroups()
  85. {
  86. if (!$this->hasData('raw_groups')) {
  87. $websiteGroups = $this->_storeManager->getWebsite()->getGroups();
  88. $groups = [];
  89. foreach ($websiteGroups as $group) {
  90. $groups[$group->getId()] = $group;
  91. }
  92. $this->setData('raw_groups', $groups);
  93. }
  94. return $this->getData('raw_groups');
  95. }
  96. /**
  97. * Get raw stores.
  98. *
  99. * @return array
  100. */
  101. public function getRawStores()
  102. {
  103. if (!$this->hasData('raw_stores')) {
  104. $websiteStores = $this->_storeManager->getWebsite()->getStores();
  105. $stores = [];
  106. foreach ($websiteStores as $store) {
  107. /* @var $store \Magento\Store\Model\Store */
  108. if (!$store->isActive()) {
  109. continue;
  110. }
  111. $localeCode = $this->_scopeConfig->getValue(
  112. Data::XML_PATH_DEFAULT_LOCALE,
  113. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  114. $store
  115. );
  116. $store->setLocaleCode($localeCode);
  117. $params = ['_query' => []];
  118. if (!$this->isStoreInUrl()) {
  119. $params['_query']['___store'] = $store->getCode();
  120. }
  121. $baseUrl = $store->getUrl('', $params);
  122. $store->setHomeUrl($baseUrl);
  123. $stores[$store->getGroupId()][$store->getId()] = $store;
  124. }
  125. $this->setData('raw_stores', $stores);
  126. }
  127. return $this->getData('raw_stores');
  128. }
  129. /**
  130. * Retrieve list of store groups with default urls set
  131. *
  132. * @return Group[]
  133. */
  134. public function getGroups()
  135. {
  136. if (!$this->hasData('groups')) {
  137. $rawGroups = $this->getRawGroups();
  138. $rawStores = $this->getRawStores();
  139. $groups = [];
  140. $localeCode = $this->_scopeConfig->getValue(
  141. Data::XML_PATH_DEFAULT_LOCALE,
  142. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  143. );
  144. foreach ($rawGroups as $group) {
  145. /* @var $group Group */
  146. if (!isset($rawStores[$group->getId()])) {
  147. continue;
  148. }
  149. if ($group->getId() == $this->getCurrentGroupId()) {
  150. $groups[] = $group;
  151. continue;
  152. }
  153. $store = $group->getDefaultStoreByLocale($localeCode);
  154. if ($store) {
  155. $group->setHomeUrl($store->getHomeUrl());
  156. $groups[] = $group;
  157. }
  158. }
  159. $this->setData('groups', $groups);
  160. }
  161. return $this->getData('groups');
  162. }
  163. /**
  164. * Get stores.
  165. *
  166. * @return \Magento\Store\Model\Store[]
  167. */
  168. public function getStores()
  169. {
  170. if (!$this->getData('stores')) {
  171. $rawStores = $this->getRawStores();
  172. $groupId = $this->getCurrentGroupId();
  173. if (!isset($rawStores[$groupId])) {
  174. $stores = [];
  175. } else {
  176. $stores = $rawStores[$groupId];
  177. }
  178. $this->setData('stores', $stores);
  179. }
  180. return $this->getData('stores');
  181. }
  182. /**
  183. * Get current store code.
  184. *
  185. * @return string
  186. */
  187. public function getCurrentStoreCode()
  188. {
  189. return $this->_storeManager->getStore()->getCode();
  190. }
  191. /**
  192. * Is store in url.
  193. *
  194. * @return bool
  195. */
  196. public function isStoreInUrl()
  197. {
  198. if ($this->_storeInUrl === null) {
  199. $this->_storeInUrl = $this->_storeManager->getStore()->isUseStoreInUrl();
  200. }
  201. return $this->_storeInUrl;
  202. }
  203. /**
  204. * Get store code.
  205. *
  206. * @return string
  207. */
  208. public function getStoreCode()
  209. {
  210. return $this->_storeManager->getStore()->getCode();
  211. }
  212. /**
  213. * Get store name.
  214. *
  215. * @return null|string
  216. */
  217. public function getStoreName()
  218. {
  219. return $this->_storeManager->getStore()->getName();
  220. }
  221. /**
  222. * Returns target store post data.
  223. *
  224. * @param Store $store
  225. * @param array $data
  226. * @return string
  227. * @throws \Magento\Framework\Exception\NoSuchEntityException
  228. */
  229. public function getTargetStorePostData(Store $store, $data = [])
  230. {
  231. $data[\Magento\Store\Model\StoreManagerInterface::PARAM_NAME] = $store->getCode();
  232. $data['___from_store'] = $this->_storeManager->getStore()->getCode();
  233. $urlOnTargetStore = $store->getCurrentUrl(false);
  234. $data[ActionInterface::PARAM_NAME_URL_ENCODED] = $this->urlHelper->getEncodedUrl($urlOnTargetStore);
  235. $url = $this->getUrl('stores/store/redirect');
  236. return $this->_postDataHelper->getPostData(
  237. $url,
  238. $data
  239. );
  240. }
  241. }