diff --git a/Model/CustomRedirectDataProvider.php b/Model/CustomRedirectDataProvider.php index 834c0dc..3ffc502 100644 --- a/Model/CustomRedirectDataProvider.php +++ b/Model/CustomRedirectDataProvider.php @@ -8,10 +8,11 @@ namespace MageWorx\SeoRedirectsGraphQl\Model; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Query\Uid; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\UrlRewrite\Model\UrlFinderInterface; -use Magento\UrlRewrite\Model\UrlRewriteFactory; +use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\UrlRewriteGraphQl\Model\DataProvider\EntityDataProviderComposite; use MageWorx\SeoRedirects\Api\Data\CustomRedirectInterface; @@ -98,28 +99,7 @@ public function getEntityUrlDataByCustomRedirectEntity(CustomRedirectInterface $ */ public function getUrlRewriteDataByCustomRedirectEntity(CustomRedirectInterface $customRedirect): ?array { - $urlRewrite = $this->getUrlRewrite($customRedirect); - - if (!$urlRewrite) { - // Trying to get rewrite to custom url by target path - $urlRewrite = $this->getRewriteByTargetPath($customRedirect->getTargetEntityIdentifier(), (int)$customRedirect->getStoreId()); - if (!$urlRewrite) { - // Trying to get rewrite to custom url by request path - $urlRewrite = $this->getRewriteByRequestPath($customRedirect->getTargetEntityIdentifier(), (int)$customRedirect->getStoreId()); - if (!$urlRewrite) { - // Create new URL rewrite dummy entity to return the data about our custom redirect - /** @var \Magento\UrlRewrite\Model\UrlRewrite $urlRewrite */ - $urlRewrite = $this->urlRewriteFactory->create(); - $urlRewrite->setEntityId(0); - $urlRewrite->setRequestPath($customRedirect->getTargetEntityIdentifier()); - $urlRewrite->setEntityType('CUSTOM'); - $urlRewrite->setStoreId((int)$customRedirect->getStoreId()); - $urlRewrite->setMetadata(['redirect_code' => $customRedirect->getRedirectCode()]); - $urlRewrite->setRedirectType($customRedirect->getRedirectCode()); - $urlRewrite->setTargetPath($customRedirect->getTargetEntityIdentifier()); - } - } - } + $urlRewrite = $this->searchUrlRewriteByCustomRedirect($customRedirect); return [ 'id' => $urlRewrite->getEntityId(), @@ -152,11 +132,7 @@ public function getRouteData(int $storeId, int $objectId, string $objectType, Re return null; } - $urlRewrite = $this->getUrlRewrite($customRedirect); - - if (!$urlRewrite) { - return null; - } + $urlRewrite = $this->searchUrlRewriteByCustomRedirect($customRedirect); $type = $this->sanitizeType($urlRewrite->getEntityType()); $result = $this->entityDataProviderComposite->getData($type, (int)$urlRewrite->getEntityId(), $info, $storeId); @@ -195,9 +171,43 @@ protected function getCustomRedirect(int $storeId, int $objectId, string $object return $customRedirectCollection->getFirstItem(); } + /** + * @param CustomRedirect $customRedirect + * @return UrlRewrite + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function searchUrlRewriteByCustomRedirect(CustomRedirect $customRedirect): UrlRewrite + { + $urlRewrite = $this->getUrlRewrite($customRedirect); + + if (!$urlRewrite) { + // Trying to get rewrite to custom url by target path + $urlRewrite = $this->getRewriteByTargetPath($customRedirect->getTargetEntityIdentifier(), (int)$customRedirect->getStoreId()); + if (!$urlRewrite) { + // Trying to get rewrite to custom url by request path + $urlRewrite = $this->getRewriteByRequestPath($customRedirect->getTargetEntityIdentifier(), (int)$customRedirect->getStoreId()); + if (!$urlRewrite) { + // Create new URL rewrite dummy entity to return the data about our custom redirect + /** @var \Magento\UrlRewrite\Model\UrlRewrite $urlRewrite */ + $urlRewrite = $this->urlRewriteFactory->create(); + $urlRewrite->setEntityId(0); + $urlRewrite->setRequestPath($customRedirect->getTargetEntityIdentifier()); + $urlRewrite->setEntityType(CustomRedirectTypeOptions::TYPE_CUSTOM); + $urlRewrite->setStoreId((int)$customRedirect->getStoreId()); + $urlRewrite->setMetadata(['redirect_code' => $customRedirect->getRedirectCode()]); + $urlRewrite->setRedirectType($customRedirect->getRedirectCode()); + $urlRewrite->setTargetPath($customRedirect->getTargetEntityIdentifier()); + } + } + } + + return $urlRewrite; + } + /** * @param CustomRedirect $customRedirect * @return UrlRewrite|null + * @throws LocalizedException */ protected function getUrlRewrite(CustomRedirect $customRedirect): ?UrlRewrite { diff --git a/Model/CustomUrlTypeResolver.php b/Model/CustomUrlTypeResolver.php new file mode 100644 index 0000000..51adf98 --- /dev/null +++ b/Model/CustomUrlTypeResolver.php @@ -0,0 +1,25 @@ + CustomRedirect::REDIRECT_TYPE_PRODUCT, self::TYPE_CATEGORY => CustomRedirect::REDIRECT_TYPE_CATEGORY, - self::TYPE_CMS_PAGE => CustomRedirect::REDIRECT_TYPE_PAGE + self::TYPE_CMS_PAGE => CustomRedirect::REDIRECT_TYPE_PAGE, + self::TYPE_CUSTOM => CustomRedirect::REDIRECT_TYPE_CUSTOM ]; } } diff --git a/etc/graphql/di.xml b/etc/graphql/di.xml new file mode 100755 index 0000000..17e4bc3 --- /dev/null +++ b/etc/graphql/di.xml @@ -0,0 +1,16 @@ + + + + + + + MageWorx\SeoRedirectsGraphQl\Model\CustomUrlTypeResolver + + + + diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 342b70e..167963d 100644 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -1,3 +1,5 @@ enum UrlRewriteEntityTypeEnum @doc(description: "This enumeration adds the CUSTOM entity type.") { CUSTOM } +type CustomUrlRedirect implements RoutableInterface @doc(description: "Defines properties of a custom redirect entity. In Progress.") { +}