Skip to content

Commit c2187b2

Browse files
merge magento/2.3-develop into magento-honey-badgers/MC-18402
2 parents e9b1a05 + 5860041 commit c2187b2

File tree

8 files changed

+664
-374
lines changed

8 files changed

+664
-374
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\CatalogUrlRewriteGraphQl\Model\Resolver;
9+
10+
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
17+
/**
18+
* Returns the url suffix for category
19+
*/
20+
class CategoryUrlSuffix implements ResolverInterface
21+
{
22+
/**
23+
* System setting for the url suffix for categories
24+
*
25+
* @var string
26+
*/
27+
private static $xml_path_category_url_suffix = 'catalog/seo/category_url_suffix';
28+
29+
/**
30+
* Cache for product rewrite suffix
31+
*
32+
* @var array
33+
*/
34+
private $categoryUrlSuffix = [];
35+
36+
/**
37+
* @var ScopeConfigInterface
38+
*/
39+
private $scopeConfig;
40+
41+
/**
42+
* @param ScopeConfigInterface $scopeConfig
43+
*/
44+
public function __construct(ScopeConfigInterface $scopeConfig)
45+
{
46+
$this->scopeConfig = $scopeConfig;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function resolve(
53+
Field $field,
54+
$context,
55+
ResolveInfo $info,
56+
array $value = null,
57+
array $args = null
58+
): string {
59+
/** @var StoreInterface $store */
60+
$store = $context->getExtensionAttributes()->getStore();
61+
$storeId = (int)$store->getId();
62+
return $this->getCategoryUrlSuffix($storeId);
63+
}
64+
65+
/**
66+
* Retrieve category url suffix by store
67+
*
68+
* @param int $storeId
69+
* @return string
70+
*/
71+
private function getCategoryUrlSuffix(int $storeId): string
72+
{
73+
if (!isset($this->categoryUrlSuffix[$storeId])) {
74+
$this->categoryUrlSuffix[$storeId] = $this->scopeConfig->getValue(
75+
self::$xml_path_category_url_suffix,
76+
ScopeInterface::SCOPE_STORE,
77+
$storeId
78+
);
79+
}
80+
return $this->categoryUrlSuffix[$storeId];
81+
}
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\CatalogUrlRewriteGraphQl\Model\Resolver;
9+
10+
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
17+
/**
18+
* Returns the url suffix for product
19+
*/
20+
class ProductUrlSuffix implements ResolverInterface
21+
{
22+
/**
23+
* System setting for the url suffix for products
24+
*
25+
* @var string
26+
*/
27+
private static $xml_path_product_url_suffix = 'catalog/seo/product_url_suffix';
28+
29+
/**
30+
* Cache for product rewrite suffix
31+
*
32+
* @var array
33+
*/
34+
private $productUrlSuffix = [];
35+
36+
/**
37+
* @var ScopeConfigInterface
38+
*/
39+
private $scopeConfig;
40+
41+
/**
42+
* @param ScopeConfigInterface $scopeConfig
43+
*/
44+
public function __construct(ScopeConfigInterface $scopeConfig)
45+
{
46+
$this->scopeConfig = $scopeConfig;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function resolve(
53+
Field $field,
54+
$context,
55+
ResolveInfo $info,
56+
array $value = null,
57+
array $args = null
58+
): string {
59+
/** @var StoreInterface $store */
60+
$store = $context->getExtensionAttributes()->getStore();
61+
$storeId = (int)$store->getId();
62+
return $this->getProductUrlSuffix($storeId);
63+
}
64+
65+
/**
66+
* Retrieve product url suffix by store
67+
*
68+
* @param int $storeId
69+
* @return string
70+
*/
71+
private function getProductUrlSuffix(int $storeId): string
72+
{
73+
if (!isset($this->productUrlSuffix[$storeId])) {
74+
$this->productUrlSuffix[$storeId] = $this->scopeConfig->getValue(
75+
self::$xml_path_product_url_suffix,
76+
ScopeInterface::SCOPE_STORE,
77+
$storeId
78+
);
79+
}
80+
return $this->productUrlSuffix[$storeId];
81+
}
82+
}

app/code/Magento/CatalogUrlRewriteGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.1.3||~7.2.0||~7.3.0",
7+
"magento/module-store": "*",
78
"magento/module-catalog": "*",
89
"magento/framework": "*"
910
},

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
interface ProductInterface {
55
url_key: String @doc(description: "The part of the URL that identifies the product")
6+
url_suffix: String @doc(description: "The part of the product URL that is appended after the url key") @resolver(class: "Magento\\CatalogUrlRewriteGraphQl\\Model\\Resolver\\ProductUrlSuffix")
67
url_path: String @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")
78
url_rewrites: [UrlRewrite] @doc(description: "URL rewrites list") @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite")
89
}
910

11+
interface CategoryInterface {
12+
url_suffix: String @doc(description: "The part of the category URL that is appended after the url key") @resolver(class: "Magento\\CatalogUrlRewriteGraphQl\\Model\\Resolver\\CategoryUrlSuffix")
13+
}
14+
1015
input ProductFilterInput {
1116
url_key: FilterTypeInput @doc(description: "The part of the URL that identifies the product")
1217
url_path: FilterTypeInput @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See COPYING.txt for license details.
33

44
type Query {
5-
urlResolver(url: String!): EntityUrl @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\EntityUrl") @doc(description: "The urlResolver query returns the relative URL for a specified product, category or CMS page") @cache(cacheIdentity: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite\\UrlResolverIdentity")
5+
urlResolver(url: String!): EntityUrl @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\EntityUrl") @doc(description: "The urlResolver query returns the relative URL for a specified product, category or CMS page, using as input a url_key appended by the url_suffix, if one exists") @cache(cacheIdentity: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite\\UrlResolverIdentity")
66
}
77

88
type EntityUrl @doc(description: "EntityUrl is an output object containing the `id`, `relative_url`, and `type` attributes") {

0 commit comments

Comments
 (0)