1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Render\MarkupInterface; |
6
|
|
|
use Drupal\Core\Entity\ContentEntityInterface; |
7
|
|
|
use Drupal\Core\Entity\Entity; |
8
|
|
|
use Drupal\Core\Entity\EntityInterface; |
9
|
|
|
use Drupal\Core\Field\FieldItemInterface; |
10
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
12
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
13
|
|
|
use GraphQL\Type\Definition\ScalarType; |
14
|
|
|
use GraphQL\Type\Definition\WrappingType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class for entity field plugins. |
18
|
|
|
*/ |
19
|
|
|
class EntityFieldBase extends FieldPluginBase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
protected function resolveItem($item, array $args, ResolveContext $context, ResolveInfo $info) { |
25
|
|
|
if ($item instanceof FieldItemInterface) { |
|
|
|
|
26
|
|
|
$definition = $this->getPluginDefinition(); |
27
|
|
|
$property = $definition['property']; |
28
|
|
|
$result = $item->get($property)->getValue(); |
29
|
|
|
$result = $result instanceof MarkupInterface ? $result->__toString() : $result; |
|
|
|
|
30
|
|
|
|
31
|
|
|
$type = $info->returnType; |
32
|
|
|
$type = $type instanceof WrappingType ? $type->getWrappedType(TRUE) : $type; |
33
|
|
|
if ($type instanceof ScalarType) { |
34
|
|
|
$result = is_null($result) ? NULL : $type->serialize($result); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($result instanceof ContentEntityInterface && $result->isTranslatable() && $language = $context->getContext('language', $info)) { |
|
|
|
|
38
|
|
|
if ($result->hasTranslation($language)) { |
39
|
|
|
$result = $result->getTranslation($language); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($result instanceof ContentEntityInterface) { |
|
|
|
|
44
|
|
|
$access = $entity->access('view', NULL, TRUE); |
|
|
|
|
45
|
|
|
$context->addCacheableDependency($access); |
46
|
|
|
|
47
|
|
|
if (!$access->isAllowed()) { |
48
|
|
|
$result = NULL; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.