Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Robots.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sitemap\Block;
7 
12 use Magento\Sitemap\Helper\Data as SitemapHelper;
13 use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
16 
23 class Robots extends AbstractBlock implements IdentityInterface
24 {
28  private $sitemapCollectionFactory;
29 
33  private $sitemapHelper;
34 
38  private $storeManager;
39 
50  public function __construct(
51  Context $context,
52  StoreResolver $storeResolver,
53  CollectionFactory $sitemapCollectionFactory,
54  SitemapHelper $sitemapHelper,
55  StoreManagerInterface $storeManager,
56  array $data = []
57  ) {
58  $this->sitemapCollectionFactory = $sitemapCollectionFactory;
59  $this->sitemapHelper = $sitemapHelper;
60  $this->storeManager = $storeManager;
61 
62  parent::__construct($context, $data);
63  }
64 
75  protected function _toHtml()
76  {
77  $defaultStore = $this->storeManager->getDefaultStoreView();
78 
80  $website = $this->storeManager->getWebsite($defaultStore->getWebsiteId());
81 
82  $storeIds = [];
83  foreach ($website->getStoreIds() as $storeId) {
84  if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) {
85  $storeIds[] = (int)$storeId;
86  }
87  }
88 
89  $links = [];
90  if ($storeIds) {
91  $links = array_merge($links, $this->getSitemapLinks($storeIds));
92  }
93 
94  return $links ? implode(PHP_EOL, $links) . PHP_EOL : '';
95  }
96 
107  protected function getSitemapLinks(array $storeIds)
108  {
109  $sitemapLinks = [];
110 
112  $collection = $this->sitemapCollectionFactory->create();
113  $collection->addStoreFilter($storeIds);
114 
115  foreach ($collection as $sitemap) {
117  $sitemapFilename = $sitemap->getSitemapFilename();
118  $sitemapPath = $sitemap->getSitemapPath();
119 
120  $sitemapUrl = $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename);
121  $sitemapLinks[$sitemapUrl] = 'Sitemap: ' . $sitemapUrl;
122  }
123 
124  return $sitemapLinks;
125  }
126 
133  public function getIdentities()
134  {
135  return [
136  Value::CACHE_TAG . '_' . $this->storeManager->getDefaultStoreView()->getId(),
137  ];
138  }
139 }
$storeManager
__construct(Context $context, StoreResolver $storeResolver, CollectionFactory $sitemapCollectionFactory, SitemapHelper $sitemapHelper, StoreManagerInterface $storeManager, array $data=[])
Definition: Robots.php:50