Skip to content

Commit fa04a64

Browse files
committed
MC-20158: Redirects are not supported in urlResolver
- add redirect code
1 parent 4aace8c commit fa04a64

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class EntityUrl implements ResolverInterface
3131
*/
3232
private $customUrlLocator;
3333

34+
/**
35+
* @var int
36+
*/
37+
private $redirectType;
38+
3439
/**
3540
* @param UrlFinderInterface $urlFinder
3641
* @param CustomUrlLocatorInterface $customUrlLocator
@@ -63,6 +68,7 @@ public function resolve(
6368
if (substr($url, 0, 1) === '/' && $url !== '/') {
6469
$url = ltrim($url, '/');
6570
}
71+
$this->redirectType = 0;
6672
$customUrl = $this->customUrlLocator->locateUrl($url);
6773
$url = $customUrl ?: $url;
6874
$finalUrlRewrite = $this->findFinalUrl($url, $storeId);
@@ -72,6 +78,7 @@ public function resolve(
7278
'id' => $finalUrlRewrite->getEntityId(),
7379
'canonical_url' => $relativeUrl,
7480
'relative_url' => $relativeUrl,
81+
'redirectCode' => $this->redirectType,
7582
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
7683
];
7784

@@ -105,6 +112,7 @@ private function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId): ?
105112
'id' => $finalUrlRewrite->getEntityId(),
106113
'canonical_url' => $relativeUrl,
107114
'relative_url' => $relativeUrl,
115+
'redirectCode' => $finalCustomUrlRewrite->getRedirectType(),
108116
'type' => $this->sanitizeType($finalUrlRewrite->getEntityType())
109117
];
110118
}
@@ -122,10 +130,12 @@ private function rewriteCustomUrls(UrlRewrite $finalUrlRewrite, int $storeId): ?
122130
private function findFinalUrl(string $requestPath, int $storeId, bool $findCustom = false): ?UrlRewrite
123131
{
124132
$urlRewrite = $this->findUrlFromRequestPath($requestPath, $storeId);
125-
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
126-
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
127-
}
128-
if (!$urlRewrite) {
133+
if ($urlRewrite) {
134+
$this->redirectType = $urlRewrite->getRedirectType();
135+
while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
136+
$urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath(), $storeId);
137+
}
138+
} else {
129139
$urlRewrite = $this->findUrlFromTargetPath($requestPath, $storeId);
130140
}
131141
if ($urlRewrite && ($findCustom && !$urlRewrite->getEntityId() && !$urlRewrite->getIsAutogenerated())) {

app/code/Magento/UrlRewriteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type EntityUrl @doc(description: "EntityUrl is an output object containing the `
99
id: Int @doc(description: "The ID assigned to the object associated with the specified url. This could be a product ID, category ID, or page ID.")
1010
canonical_url: String @deprecated(reason: "The canonical_url field is deprecated, use relative_url instead.")
1111
relative_url: String @doc(description: "The internal relative URL. If the specified url is a redirect, the query returns the redirected URL, not the original.")
12+
redirectCode: Int @doc(description: "301 or 302 HTTP code for url permanent or temporary redirect or 0 for the 200 no redirect")
1213
type: UrlRewriteEntityTypeEnum @doc(description: "One of PRODUCT, CATEGORY, or CMS_PAGE.")
1314
}
1415

0 commit comments

Comments
 (0)