Skip to content

Commit

Permalink
Merge pull request #8 from mageworx/update_support_of_route_query_bug…
Browse files Browse the repository at this point in the history
…fixes

WIP: Bugfix for Custom Redirects in GraphQL
  • Loading branch information
SiarheyUchukhlebau authored Sep 23, 2024
2 parents 21c228c + 8a2f159 commit 0e9ec5f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 30 deletions.
66 changes: 38 additions & 28 deletions Model/CustomRedirectDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down
25 changes: 25 additions & 0 deletions Model/CustomUrlTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
declare(strict_types = 1);

namespace MageWorx\SeoRedirectsGraphQl\Model;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
use MageWorx\SeoRedirectsGraphQl\Model\Source\CustomRedirectType;

class CustomUrlTypeResolver implements TypeResolverInterface
{
/**
* {@inheritdoc}
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] === CustomRedirectType::TYPE_CUSTOM) {
return 'CustomUrlRedirect';
}
return '';
}
}
6 changes: 4 additions & 2 deletions Model/Source/CustomRedirectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE.txt for license details.
*/

declare(strict_types=1);
declare(strict_types = 1);

namespace MageWorx\SeoRedirectsGraphQl\Model\Source;

Expand All @@ -15,6 +15,7 @@ class CustomRedirectType
const TYPE_PRODUCT = 'PRODUCT';
const TYPE_CATEGORY = 'CATEGORY';
const TYPE_CMS_PAGE = 'CMS_PAGE';
const TYPE_CUSTOM = 'CUSTOM';

/**
* @return array
Expand All @@ -24,7 +25,8 @@ public function getTypes(): array
return [
self::TYPE_PRODUCT => 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
];
}
}
16 changes: 16 additions & 0 deletions etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="CustomUrlRedirect" xsi:type="object">MageWorx\SeoRedirectsGraphQl\Model\CustomUrlTypeResolver</item>
</argument>
</arguments>
</type>
</config>
2 changes: 2 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -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.") {
}

0 comments on commit 0e9ec5f

Please sign in to comment.