Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CmsPageUrlRewriteGenerator.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
10 
12 {
16  const ENTITY_TYPE = 'cms-page';
17 
21  protected $urlRewriteFactory;
22 
27 
33  protected $storeManager;
34 
38  protected $cmsPage;
39 
45  public function __construct(
46  UrlRewriteFactory $urlRewriteFactory,
49  ) {
50  $this->urlRewriteFactory = $urlRewriteFactory;
51  $this->storeManager = $storeManager;
52  $this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
53  }
54 
59  public function generate($cmsPage)
60  {
61  $stores = $cmsPage->getStores();
62  $this->cmsPage = $cmsPage;
63  $urls = array_search('0', $stores) === false ? $this->generateForSpecificStores($stores)
64  : $this->generateForAllStores();
65  $this->cmsPage = null;
66  return $urls;
67  }
68 
74  protected function generateForAllStores()
75  {
76  $urls = [];
77  foreach ($this->storeManager->getStores() as $store) {
78  $urls[] = $this->createUrlRewrite($store->getStoreId());
79  }
80  return $urls;
81  }
82 
89  protected function generateForSpecificStores($storeIds)
90  {
91  $urls = [];
92  $existingStores = $this->storeManager->getStores();
93  foreach ($storeIds as $storeId) {
94  if (!isset($existingStores[$storeId])) {
95  continue;
96  }
97  $urls[] = $this->createUrlRewrite($storeId);
98  }
99  return $urls;
100  }
101 
109  protected function createUrlRewrite($storeId, $redirectType = 0)
110  {
111  return $this->urlRewriteFactory->create()->setStoreId($storeId)
112  ->setEntityType(self::ENTITY_TYPE)
113  ->setEntityId($this->cmsPage->getId())
114  ->setRequestPath($this->cmsPage->getIdentifier())
115  ->setTargetPath($this->cmsPageUrlPathGenerator->getCanonicalUrlPath($this->cmsPage))
116  ->setIsAutogenerated(1)
117  ->setRedirectType($redirectType);
118  }
119 }
__construct(UrlRewriteFactory $urlRewriteFactory, CmsPageUrlPathGenerator $cmsPageUrlPathGenerator, StoreManagerInterface $storeManager)