/
Products.php
81 lines (73 loc) · 2.19 KB
/
Products.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\fields;
use Craft;
use craft\base\ElementInterface;
use craft\commerce\elements\Product;
use craft\commerce\gql\arguments\elements\Product as ProductArguments;
use craft\commerce\gql\interfaces\elements\Product as ProductInterface;
use craft\commerce\gql\resolvers\elements\Product as ProductResolver;
use craft\commerce\web\assets\editproduct\EditProductAsset;
use craft\fields\BaseRelationField;
use craft\helpers\Gql as GqlHelper;
use craft\services\Gql as GqlService;
use GraphQL\Type\Definition\Type;
/**
* Class Product Field
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 2.0
*/
class Products extends BaseRelationField
{
public function __construct(array $config = [])
{
// Never needed and allows us to instantiate the field while ignoring old setting until the Product field migration has run.
unset($config['targetLocale']);
parent::__construct($config);
}
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('commerce', 'Commerce Products');
}
/**
* @inheritdoc
*/
public static function defaultSelectionLabel(): string
{
return Craft::t('commerce', 'Add a product');
}
public function getInputHtml($value, ElementInterface $element = null): string
{
Craft::$app->getView()->registerAssetBundle(EditProductAsset::class);
return parent::getInputHtml($value, $element);
}
/**
* @inheritdoc
* @since 3.1.4
*/
public function getContentGqlType()
{
return [
'name' => $this->handle,
'type' => Type::listOf(ProductInterface::getType()),
'args' => ProductArguments::getArguments(),
'resolve' => ProductResolver::class . '::resolve',
'complexity' => GqlHelper::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
];
}
/**
* @inheritdoc
*/
protected static function elementType(): string
{
return Product::class;
}
}