Skip to content

Commit 07c8f54

Browse files
committed
MC-20158: Redirects are not supported in urlResolver
- support for custom urls - return the proper canonical url
1 parent 68c48d5 commit 07c8f54

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CanonicalUrl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function resolve(
3434

3535
/* @var $product Product */
3636
$product = $value['model'];
37-
$url = $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
37+
$product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
3838

39-
return $url;
39+
return $product->getRequestPath();
4040
}
4141
}

app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,49 +57,79 @@ public function resolve(
5757
throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
5858
}
5959

60+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
6061
$result = null;
6162
$url = $args['url'];
6263
if (substr($url, 0, 1) === '/' && $url !== '/') {
6364
$url = ltrim($url, '/');
6465
}
6566
$customUrl = $this->customUrlLocator->locateUrl($url);
6667
$url = $customUrl ?: $url;
67-
$urlRewrite = $this->findCanonicalUrl($url, (int)$context->getExtensionAttributes()->getStore()->getId());
68-
if ($urlRewrite) {
69-
if (!$urlRewrite->getEntityId()) {
68+
$finalUrlRewrite = $this->findFinalUrl($url, $storeId);
69+
if ($finalUrlRewrite) {
70+
$relativeUrl = $finalUrlRewrite->getRequestPath();
71+
$resultArray = $this->rewriteCustomUrls($finalUrlRewrite, $storeId) ?? [
72+
'id' => $finalUrlRewrite->getEntityId(),
73+
'canonical_url' => $relativeUrl,
74+
'relative_url' => $relativeUrl,
75+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
76+
];
77+
78+
79+
if (empty($resultArray['id'])) {
7080
throw new GraphQlNoSuchEntityException(
7181
__('No such entity found with matching URL key: %url', ['url' => $url])
7282
);
7383
}
74-
$result = [
75-
'id' => $urlRewrite->getEntityId(),
76-
'canonical_url' => $urlRewrite->getTargetPath(),
77-
'relative_url' => $urlRewrite->getTargetPath(),
78-
'type' => $this->sanitizeType($urlRewrite->getEntityType())
79-
];
84+
85+
$result = $resultArray;
8086
}
8187
return $result;
8288
}
8389

8490
/**
85-
* Find the canonical url passing through all redirects if any
91+
* Handle custom urls with and without redirects
92+
*
93+
* @param UrlRewrite $finalUrlRewrite
94+
* @param int $storeId
95+
* @return array|null
96+
*/
97+
private function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId): ?array
98+
{
99+
if ($finalUrlRewrite->getEntityType() === 'custom' || !($finalUrlRewrite->getEntityId() > 0)) {
100+
$finalCustomUrlRewrite = clone $finalUrlRewrite;
101+
$finalUrlRewrite = $this->findFinalUrl($finalCustomUrlRewrite->getTargetPath(), $storeId, true);
102+
$relativeUrl =
103+
$finalCustomUrlRewrite->getRedirectType() == 0
104+
? $finalCustomUrlRewrite->getRequestPath() : $finalUrlRewrite->getRequestPath();
105+
return [
106+
'id' => $finalUrlRewrite->getEntityId(),
107+
'canonical_url' => $relativeUrl,
108+
'relative_url' => $relativeUrl,
109+
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
110+
];
111+
}
112+
return null;
113+
}
114+
115+
/**
116+
* Find the final url passing through all redirects if any
86117
*
87118
* @param string $requestPath
88119
* @param int $storeId
120+
* @param bool $findCustom
89121
* @return UrlRewrite|null
90122
*/
91-
private function findCanonicalUrl(string $requestPath, int $storeId) : ?UrlRewrite
123+
private function findFinalUrl(string $requestPath, int $storeId, bool $findCustom = false): ?UrlRewrite
92124
{
93125
$urlRewrite = $this->findUrlFromRequestPath($requestPath, $storeId);
94-
if ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
95-
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
96-
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
97-
}
126+
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
127+
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
98128
}
99129
if (!$urlRewrite) {
100130
$urlRewrite = $this->findUrlFromTargetPath($requestPath, $storeId);
101131
}
102-
if ($urlRewrite && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated()) {
132+
if ($urlRewrite && ($findCustom && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated())) {
103133
$urlRewrite = $this->findUrlFromTargetPath($urlRewrite->getTargetPath(), $storeId);
104134
}
105135

@@ -113,7 +143,7 @@ private function findCanonicalUrl(string $requestPath, int $storeId) : ?UrlRewri
113143
* @param int $storeId
114144
* @return UrlRewrite|null
115145
*/
116-
private function findUrlFromRequestPath(string $requestPath, int $storeId) : ?UrlRewrite
146+
private function findUrlFromRequestPath(string $requestPath, int $storeId): ?UrlRewrite
117147
{
118148
return $this->urlFinder->findOneByData(
119149
[
@@ -130,7 +160,7 @@ private function findUrlFromRequestPath(string $requestPath, int $storeId) : ?Ur
130160
* @param int $storeId
131161
* @return UrlRewrite|null
132162
*/
133-
private function findUrlFromTargetPath(string $targetPath, int $storeId) : ?UrlRewrite
163+
private function findUrlFromTargetPath(string $targetPath, int $storeId): ?UrlRewrite
134164
{
135165
return $this->urlFinder->findOneByData(
136166
[

0 commit comments

Comments
 (0)