sitemapCollectionFactory = $sitemapCollectionFactory; $this->sitemapHelper = $sitemapHelper; $this->storeManager = $storeManager; parent::__construct($context, $data); } /** * Prepare sitemap links to add to the robots.txt file * * Collects sitemap links for all stores of given website. * Detects if sitemap file information is required to be added to robots.txt * and adds links for this sitemap files into result data. * * @return string * @since 100.1.5 */ protected function _toHtml() { $defaultStore = $this->storeManager->getDefaultStoreView(); /** @var \Magento\Store\Model\Website $website */ $website = $this->storeManager->getWebsite($defaultStore->getWebsiteId()); $storeIds = []; foreach ($website->getStoreIds() as $storeId) { if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) { $storeIds[] = (int)$storeId; } } $links = []; if ($storeIds) { $links = array_merge($links, $this->getSitemapLinks($storeIds)); } return $links ? implode(PHP_EOL, $links) . PHP_EOL : ''; } /** * Retrieve sitemap links for given store * * Gets the names of sitemap files that linked with given store, * and adds links for this sitemap files into result array. * * @param int[] $storeIds * @return array * @since 100.1.5 */ protected function getSitemapLinks(array $storeIds) { $sitemapLinks = []; /** @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection $collection */ $collection = $this->sitemapCollectionFactory->create(); $collection->addStoreFilter($storeIds); foreach ($collection as $sitemap) { /** @var \Magento\Sitemap\Model\Sitemap $sitemap */ $sitemapFilename = $sitemap->getSitemapFilename(); $sitemapPath = $sitemap->getSitemapPath(); $sitemapUrl = $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename); $sitemapLinks[$sitemapUrl] = 'Sitemap: ' . $sitemapUrl; } return $sitemapLinks; } /** * Get unique page cache identities * * @return array * @since 100.1.5 */ public function getIdentities() { return [ Value::CACHE_TAG . '_' . $this->storeManager->getDefaultStoreView()->getId(), ]; } }