Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
- use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
- use Magento\UrlRewrite\Model\OptionProvider;
- class CategoryRedirect implements \Magento\Framework\Event\ObserverInterface
- {
- const EN_STORE = 1;
- const GR_STORE = 6;
- /**
- * @var \Magento\Framework\UrlInterface
- */
- protected $_url;
- /** var \Magento\Framework\App\ActionFactory */
- protected $actionFactory;
- /** @var \Magento\Framework\UrlInterface */
- protected $url;
- /** @var \Magento\Store\Model\StoreManagerInterface */
- protected $storeManager;
- /** @var \Magento\Framework\App\ResponseInterface */
- protected $response;
- /**
- * @var \Magento\UrlRewrite\Model\UrlFinderInterface
- */
- protected $urlFinder;
- /**
- * @var \Greecs\Museums\Helper\Data
- */
- protected $museumsHelper;
- /**
- * CategoryRedirect constructor.
- *
- * @param \Magento\Framework\UrlInterface $url
- * @param \Magento\Framework\App\ActionFactory $actionFactory
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\App\ResponseInterface $response
- * @param UrlFinderInterface $urlFinder
- */
- public function __construct(
- \Magento\Framework\UrlInterface $url,
- \Magento\Framework\App\ActionFactory $actionFactory,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Framework\App\ResponseInterface $response,
- \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder,
- )
- {
- $this->actionFactory = $actionFactory;
- $this->url = $url;
- $this->storeManager = $storeManager;
- $this->response = $response;
- $this->urlFinder = $urlFinder;
- }
- public function execute(\Magento\Framework\Event\Observer $observer)
- {
- $request = $observer->getEvent()->getRequest();
- $requestPath = $request->getRequestString();
- $currentStoreId = $this->storeManager->getStore()->getId();
- $rewrite = $this->getRewrite($requestPath, $currentStoreId);
- if ($rewrite === null){
- if($currentStoreId == self::GR_STORE){
- $newRewrite = $this->getRewrite($requestPath, self::EN_STORE);
- $lookupStore = self::GR_STORE;
- }else{
- $newRewrite = $this->getRewrite($requestPath, self::GR_STORE);
- $lookupStore = self::EN_STORE;
- }
- if($newRewrite != null){
- $newRewrite = $this->getRewriteByTargetPath($newRewrite->getTargetPath(), $lookupStore);
- return $this->redirect($request, $this->url->getUrl($newRewrite->getRequestPath()), OptionProvider::TEMPORARY);
- }
- }
- }
- /**
- * @param \Magento\Framework\App\RequestInterface $request
- * @param UrlRewrite $rewrite
- *
- * @return \Magento\Framework\App\ActionInterface|null
- */
- protected function processRedirect($request, $rewrite)
- {
- $target = $rewrite->getTargetPath();
- if ($rewrite->getEntityType() !== Rewrite::ENTITY_TYPE_CUSTOM
- || ($prefix = substr($target, 0, 6)) !== 'http:/' && $prefix !== 'https:'
- )
- {
- $target = $this->url->getUrl('', ['_direct' => $target]);
- }
- return $this->redirect($request, $target, $rewrite->getRedirectType());
- }
- /**
- * @param \Magento\Framework\App\RequestInterface $request
- * @param string $url
- * @param int $code
- *
- * @return \Magento\Framework\App\ActionInterface
- */
- protected function redirect($request, $url, $code)
- {
- $this->response->setRedirect($url, $code);
- $request->setDispatched(true);
- return $this->actionFactory->create('Magento\Framework\App\Action\Redirect');
- }
- /**
- * @param string $requestPath
- * @param int $storeId
- *
- * @return UrlRewrite|null
- */
- protected function getRewrite($requestPath, $storeId)
- {
- return $this->urlFinder->findOneByData([
- UrlRewrite::REQUEST_PATH => trim($requestPath, '/'),
- UrlRewrite::STORE_ID => $storeId,
- ]);
- }
- /**
- * @param $targetPath
- * @param int $storeId
- *
- * @return UrlRewrite|null
- * @internal param string $requestPath
- */
- protected function getRewriteByTargetPath($targetPath, $storeId)
- {
- return $this->urlFinder->findOneByData([
- UrlRewrite::TARGET_PATH => trim($targetPath, '/'),
- UrlRewrite::STORE_ID => $storeId,
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement