-
Notifications
You must be signed in to change notification settings - Fork 611
/
Entry.php
87 lines (77 loc) · 2.7 KB
/
Entry.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
82
83
84
85
86
87
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\gql\resolvers\elements;
use Craft;
use craft\elements\db\ElementQuery;
use craft\elements\ElementCollection;
use craft\elements\Entry as EntryElement;
use craft\gql\base\ElementResolver;
use craft\helpers\Gql as GqlHelper;
use yii\base\UnknownMethodException;
/**
* Class Entry
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.3.0
*/
class Entry extends ElementResolver
{
/**
* @inheritdoc
*/
public static function prepareQuery(mixed $source, array $arguments, ?string $fieldName = null): mixed
{
// If this is the beginning of a resolver chain, start fresh
if ($source === null) {
$query = EntryElement::find();
$pairs = GqlHelper::extractAllowedEntitiesFromSchema('read');
$condition = [];
if (isset($pairs['sections'])) {
$entriesService = Craft::$app->getEntries();
$sectionIds = array_filter(array_map(
fn(string $uid) => $entriesService->getSectionByUid($uid)?->id,
$pairs['sections'],
));
if (!empty($sectionIds)) {
$condition[] = ['in', 'entries.sectionId', $sectionIds];
}
}
if (isset($pairs['nestedentryfields'])) {
$fieldsService = Craft::$app->getFields();
$types = array_flip($fieldsService->getNestedEntryFieldTypes());
$fieldIds = array_filter(array_map(function(string $uid) use ($fieldsService, $types) {
$field = $fieldsService->getFieldByUid($uid);
return $field && isset($types[$field::class]) ? $field->id : null;
}, $pairs['nestedentryfields']));
if (!empty($fieldIds)) {
$condition[] = ['in', 'entries.fieldId', $fieldIds];
}
}
if (empty($condition)) {
return ElementCollection::empty();
}
$query->andWhere(['or', ...$condition]);
// If not, get the prepared element query
} else {
$query = $source->$fieldName;
}
// If it's preloaded, it's preloaded.
if (!$query instanceof ElementQuery) {
return $query;
}
foreach ($arguments as $key => $value) {
try {
$query->$key($value);
} catch (UnknownMethodException $e) {
if ($value !== null) {
throw $e;
}
}
}
return $query;
}
}