Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Suffix.php
Go to the documentation of this file.
1 <?php
11 
20 
29 class Suffix extends \Magento\Framework\App\Config\Value
30 {
34  protected $urlRewriteHelper;
35 
39  protected $storeManager;
40 
44  protected $urlFinder;
45 
49  protected $connection;
50 
55  protected $resource;
56 
60  private $appConfig;
61 
76  public function __construct(
77  \Magento\Framework\Model\Context $context,
78  \Magento\Framework\Registry $registry,
80  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
83  \Magento\Framework\App\ResourceConnection $appResource,
85  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
86  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
87  array $data = []
88  ) {
89  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
90  $this->urlRewriteHelper = $urlRewriteHelper;
91  $this->connection = $appResource->getConnection();
92  $this->urlFinder = $urlFinder;
93  $this->storeManager = $storeManager;
94  $this->resource = $appResource;
95  }
96 
103  private function getAppConfig()
104  {
105  if ($this->appConfig === null) {
106  $this->appConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(
107  \Magento\Framework\App\Config::class
108  );
109  }
110  return $this->appConfig;
111  }
112 
118  public function beforeSave()
119  {
120  $this->urlRewriteHelper->validateSuffix($this->getValue());
121  return $this;
122  }
123 
127  public function afterSave()
128  {
129  if ($this->isValueChanged()) {
131  if ($this->isCategorySuffixChanged()) {
132  $this->cacheTypeList->invalidate([
133  \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
134  \Magento\Framework\App\Cache\Type\Collection::TYPE_IDENTIFIER
135  ]);
136  }
137  }
138  return parent::afterSave();
139  }
140 
145  public function afterDeleteCommit()
146  {
147  if ($this->isValueChanged()) {
149  if ($this->isCategorySuffixChanged()) {
150  $this->cacheTypeList->invalidate([
151  \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
152  \Magento\Framework\App\Cache\Type\Collection::TYPE_IDENTIFIER
153  ]);
154  }
155  }
156 
157  return parent::afterDeleteCommit();
158  }
159 
165  private function isCategorySuffixChanged()
166  {
167  return $this->isValueChanged()
169  }
170 
176  protected function updateSuffixForUrlRewrites()
177  {
178  $map = [
181  ];
182  if (!isset($map[$this->getPath()])) {
183  return $this;
184  }
185  $dataFilter = [UrlRewrite::ENTITY_TYPE => $map[$this->getPath()]];
186  $storesIds = $this->getStoreIds();
187  if ($storesIds) {
188  $dataFilter[UrlRewrite::STORE_ID] = $storesIds;
189  }
190  $entities = $this->urlFinder->findAllByData($dataFilter);
191  $oldSuffixPattern = '~' . preg_quote($this->getOldValue()) . '$~';
192  if ($this->getValue() !== null) {
193  $suffix = $this->getValue();
194  } else {
195  $this->getAppConfig()->clean();
196  $suffix = $this->_config->getValue($this->getPath());
197  }
198  foreach ($entities as $urlRewrite) {
199  $bind = $urlRewrite->getIsAutogenerated()
200  ? [UrlRewrite::REQUEST_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getRequestPath())]
201  : [UrlRewrite::TARGET_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getTargetPath())];
202  $this->connection->update(
203  $this->resource->getTableName(DbStorage::TABLE_NAME),
204  $bind,
205  $this->connection->quoteIdentifier(UrlRewrite::URL_REWRITE_ID) . ' = ' . $urlRewrite->getUrlRewriteId()
206  );
207  }
208  return $this;
209  }
210 
214  protected function getStoreIds()
215  {
216  if ($this->getScope() == 'stores') {
217  $storeIds = [$this->getScopeId()];
218  } elseif ($this->getScope() == 'websites') {
219  $website = $this->storeManager->getWebsite($this->getScopeId());
220  $storeIds = array_keys($website->getStoreIds());
221  $storeIds = array_diff($storeIds, $this->getOverrideStoreIds($storeIds));
222  } else {
223  $storeIds = array_keys($this->storeManager->getStores());
224  $storeIds = array_diff($storeIds, $this->getOverrideStoreIds($storeIds));
225  }
226  return $storeIds;
227  }
228 
233  protected function getOverrideStoreIds($storeIds)
234  {
235  $excludeIds = [];
236  foreach ($storeIds as $storeId) {
237  $suffix = $this->_config->getValue($this->getPath(), ScopeInterface::SCOPE_STORE, $storeId);
238  if ($suffix != $this->getOldValue()) {
239  $excludeIds[] = $storeId;
240  }
241  }
242  return $excludeIds;
243  }
244 }
$suffix
Definition: name.phtml:27
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\UrlRewrite\Helper\UrlRewrite $urlRewriteHelper, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\ResourceConnection $appResource, \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Suffix.php:76