Skip to content

Commit 2a54601

Browse files
committed
MC-21811: Canonical_url displays the backend domain instead of relative
- add category implementation
1 parent 90b8b3a commit 2a54601

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Category;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Store\Api\Data\StoreInterface;
16+
use Magento\Catalog\Helper\Category as CategoryHelper;
17+
18+
/**
19+
* Resolve data for category canonical URL
20+
*/
21+
class CanonicalUrl implements ResolverInterface
22+
{
23+
/** @var CategoryHelper */
24+
private $categoryHelper;
25+
26+
/**
27+
* CanonicalUrl constructor.
28+
* @param CategoryHelper $categoryHelper
29+
*/
30+
public function __construct(CategoryHelper $categoryHelper)
31+
{
32+
$this->categoryHelper = $categoryHelper;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(
39+
Field $field,
40+
$context,
41+
ResolveInfo $info,
42+
array $value = null,
43+
array $args = null
44+
) {
45+
if (!isset($value['model'])) {
46+
throw new LocalizedException(__('"model" value should be specified'));
47+
}
48+
49+
/* @var Category $category */
50+
$category = $value['model'];
51+
/** @var StoreInterface $store */
52+
$store = $context->getExtensionAttributes()->getStore();
53+
if ($this->categoryHelper->canUseCanonicalTag($store)) {
54+
$baseUrl = $category->getUrlInstance()->getBaseUrl();
55+
return str_replace($baseUrl, '', $category->getUrl());
56+
}
57+
}
58+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function resolve(
4545
throw new LocalizedException(__('"model" value should be specified'));
4646
}
4747

48-
/* @var $product Product */
48+
/* @var Product $product */
4949
$product = $value['model'];
5050
/** @var StoreInterface $store */
5151
$store = $context->getExtensionAttributes()->getStore();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
109109
gift_message_available: String @doc(description: "Indicates whether a gift message is available.")
110110
manufacturer: Int @doc(description: "A number representing the product's manufacturer.")
111111
categories: [CategoryInterface] @doc(description: "The categories assigned to a product.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Categories") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CategoriesIdentity")
112-
canonical_url: String @doc(description: "Canonical URL.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CanonicalUrl")
112+
canonical_url: String @doc(description: "Relative canonical URL. This value is returned only if the system setting 'Use Canonical Link Meta Tag For Products' is enabled") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CanonicalUrl")
113113
media_gallery: [MediaGalleryInterface] @doc(description: "An array of Media Gallery objects.") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\MediaGallery")
114114
}
115115

@@ -223,6 +223,7 @@ interface CategoryInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model
223223
path_in_store: String @doc(description: "Category path in store.")
224224
url_key: String @doc(description: "The url key assigned to the category.")
225225
url_path: String @doc(description: "The url path assigned to the category.")
226+
canonical_url: String @doc(description: "Relative canonical URL. This value is returned only if the system setting 'Use Canonical Link Meta Tag For Categories' is enabled") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category\\CanonicalUrl")
226227
position: Int @doc(description: "The position of the category relative to other categories at the same level in tree.")
227228
level: Int @doc(description: "Indicates the depth of the category within the tree.")
228229
created_at: String @doc(description: "Timestamp indicating when the category was created.")

0 commit comments

Comments
 (0)