\Magento\Store\Model\Website * * @var array */ protected $_websiteCollection = []; /** * Group collection * groupId => \Magento\Store\Model\Group * * @var array */ protected $_groupCollection = []; /** * Store collection * storeId => \Magento\Store\Model\Store * * @var array */ protected $_storeCollection; /** * @var bool */ private $_isAdminScopeAllowed = true; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $_storeManager; /** * @var string */ private $nonEscapableNbspChar; /** * Init model * Load Website, Group and Store collections * * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager) { $this->_storeManager = $storeManager; // phpcs:ignore Magento2.Functions.DiscouragedFunction $this->nonEscapableNbspChar = html_entity_decode(' ', ENT_NOQUOTES, 'UTF-8'); return $this->reload(); } /** * Load/Reload Website collection * * @return $this */ protected function _loadWebsiteCollection() { $this->_websiteCollection = $this->_storeManager->getWebsites(); return $this; } /** * Load/Reload Group collection * * @return $this */ protected function _loadGroupCollection() { $this->_groupCollection = []; foreach ($this->_storeManager->getWebsites() as $website) { foreach ($website->getGroups() as $group) { $this->_groupCollection[$group->getId()] = $group; } } return $this; } /** * Load/Reload Store collection * * @return $this */ protected function _loadStoreCollection() { $this->_storeCollection = $this->_storeManager->getStores(); return $this; } /** * Retrieve store values for form * * @param bool $empty * @param bool $all * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getStoreValuesForForm($empty = false, $all = false) { $options = []; if ($empty) { $options[] = ['label' => '', 'value' => '']; } if ($all && $this->_isAdminScopeAllowed) { $options[] = ['label' => __('All Store Views'), 'value' => 0]; } foreach ($this->_websiteCollection as $website) { $websiteShow = false; foreach ($this->_groupCollection as $group) { if ($website->getId() != $group->getWebsiteId()) { continue; } $values = []; foreach ($this->_storeCollection as $store) { if ($group->getId() != $store->getGroupId()) { continue; } if (!$websiteShow) { $options[] = ['label' => $website->getName(), 'value' => []]; $websiteShow = true; } $values[] = [ 'label' => str_repeat($this->nonEscapableNbspChar, 4) . $store->getName(), 'value' => $store->getId(), ]; } if (!empty($values)) { $options[] = [ 'label' => str_repeat($this->nonEscapableNbspChar, 4) . $group->getName(), 'value' => $values, ]; } } } array_walk( $options, function (&$item) { $item['__disableTmpl'] = true; } ); return $options; } /** * Retrieve stores structure * * @param bool $isAll * @param array $storeIds * @param array $groupIds * @param array $websiteIds * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getStoresStructure($isAll = false, $storeIds = [], $groupIds = [], $websiteIds = []) { $out = []; $websites = $this->getWebsiteCollection(); if ($isAll) { $out[] = ['value' => 0, 'label' => __('All Store Views')]; } foreach ($websites as $website) { $websiteId = $website->getId(); if ($websiteIds && !in_array($websiteId, $websiteIds)) { continue; } $out[$websiteId] = ['value' => $websiteId, 'label' => $website->getName()]; foreach ($website->getGroups() as $group) { $groupId = $group->getId(); if ($groupIds && !in_array($groupId, $groupIds)) { continue; } $out[$websiteId]['children'][$groupId] = ['value' => $groupId, 'label' => $group->getName()]; foreach ($group->getStores() as $store) { $storeId = $store->getId(); if ($storeIds && !in_array($storeId, $storeIds)) { continue; } $out[$websiteId]['children'][$groupId]['children'][$storeId] = [ 'value' => $storeId, 'label' => $store->getName(), ]; } if (empty($out[$websiteId]['children'][$groupId]['children'])) { unset($out[$websiteId]['children'][$groupId]); } } if (empty($out[$websiteId]['children'])) { unset($out[$websiteId]); } } return $out; } /** * Get store options in tree view * * @param bool $isAll * @param array $storeIds * @param array $groupIds * @param array $websiteIds * @return array Format: array(array('value' => '', 'label' => '