Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Switcher Class Reference
Inheritance diagram for Switcher:
Template AbstractBlock DataObject BlockInterface

Public Member Functions

 __construct (\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, array $data=[], UrlHelper $urlHelper=null)
 
 getCurrentWebsiteId ()
 
 getCurrentGroupId ()
 
 getCurrentStoreId ()
 
 getRawGroups ()
 
 getRawStores ()
 
 getGroups ()
 
 getStores ()
 
 getCurrentStoreCode ()
 
 isStoreInUrl ()
 
 getStoreCode ()
 
 getStoreName ()
 
 getTargetStorePostData (Store $store, $data=[])
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Attributes

 $_storeInUrl
 
 $_postDataHelper
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Switcher block

@api

Since
100.0.2

Definition at line 25 of file Switcher.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\View\Element\Template\Context  $context,
\Magento\Framework\Data\Helper\PostHelper  $postDataHelper,
array  $data = [],
UrlHelper  $urlHelper = null 
)
Parameters
\Magento\Framework\View\Element\Template\Context$context
\Magento\Framework\Data\Helper\PostHelper$postDataHelper
array$data
UrlHelper$urlHelper

Definition at line 48 of file Switcher.php.

53  {
54  $this->_postDataHelper = $postDataHelper;
55  parent::__construct($context, $data);
56  $this->urlHelper = $urlHelper ?: ObjectManager::getInstance()->get(UrlHelper::class);
57  }

Member Function Documentation

◆ getCurrentGroupId()

getCurrentGroupId ( )

Get current group Id.

Returns
int|null|string

Definition at line 74 of file Switcher.php.

75  {
76  return $this->_storeManager->getStore()->getGroupId();
77  }

◆ getCurrentStoreCode()

getCurrentStoreCode ( )

Get current store code.

Returns
string

Definition at line 207 of file Switcher.php.

208  {
209  return $this->_storeManager->getStore()->getCode();
210  }

◆ getCurrentStoreId()

getCurrentStoreId ( )

Get current Store Id.

Returns
int

Definition at line 84 of file Switcher.php.

85  {
86  return $this->_storeManager->getStore()->getId();
87  }

◆ getCurrentWebsiteId()

getCurrentWebsiteId ( )

Get current website Id.

Returns
int|null|string

Definition at line 64 of file Switcher.php.

65  {
66  return $this->_storeManager->getStore()->getWebsiteId();
67  }

◆ getGroups()

getGroups ( )

Retrieve list of store groups with default urls set

Returns
Group[]

Definition at line 148 of file Switcher.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
$group
Definition: sections.phtml:16
setData($key, $value=null)
Definition: DataObject.php:72

◆ getRawGroups()

getRawGroups ( )

Get raw groups.

Returns
array

Definition at line 94 of file Switcher.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
$group
Definition: sections.phtml:16
setData($key, $value=null)
Definition: DataObject.php:72

◆ getRawStores()

getRawStores ( )

Get raw stores.

Returns
array

Definition at line 113 of file Switcher.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getStoreCode()

getStoreCode ( )

Get store code.

Returns
string

Definition at line 230 of file Switcher.php.

231  {
232  return $this->_storeManager->getStore()->getCode();
233  }

◆ getStoreName()

getStoreName ( )

Get store name.

Returns
null|string

Definition at line 240 of file Switcher.php.

241  {
242  return $this->_storeManager->getStore()->getName();
243  }

◆ getStores()

getStores ( )

Get stores.

Returns
\Magento\Store\Model\Store[]

Definition at line 186 of file Switcher.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
setData($key, $value=null)
Definition: DataObject.php:72

◆ getTargetStorePostData()

getTargetStorePostData ( Store  $store,
  $data = [] 
)

Returns target store post data.

Parameters
Store$store
array$data
Returns
string
Exceptions

Definition at line 253 of file Switcher.php.

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  }

◆ isStoreInUrl()

isStoreInUrl ( )

Is store in url.

Returns
bool

Definition at line 217 of file Switcher.php.

218  {
219  if ($this->_storeInUrl === null) {
220  $this->_storeInUrl = $this->_storeManager->getStore()->isUseStoreInUrl();
221  }
222  return $this->_storeInUrl;
223  }

Field Documentation

◆ $_postDataHelper

$_postDataHelper
protected

Definition at line 35 of file Switcher.php.

◆ $_storeInUrl

$_storeInUrl
protected

Definition at line 30 of file Switcher.php.


The documentation for this class was generated from the following file: