1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\Deriver\Fields; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Field\FieldConfigInterface; |
6
|
|
|
use Drupal\Core\Field\FieldStorageDefinitionInterface; |
7
|
|
|
use Drupal\field\FieldStorageConfigInterface; |
8
|
|
|
use Drupal\graphql\Utility\StringHelper; |
9
|
|
|
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverWithTypeMapping; |
10
|
|
|
use Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\EntityField; |
11
|
|
|
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityFieldType; |
12
|
|
|
|
13
|
|
|
// TODO Write tests for entity reference graph traversal. |
14
|
|
|
|
15
|
|
|
// TODO Should we expose config entities? |
16
|
|
|
|
17
|
|
|
// TODO Convert timestamps to strings? |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Deriver for RawValue fields. |
21
|
|
|
*/ |
22
|
|
|
class EntityFieldDeriver extends EntityFieldDeriverWithTypeMapping { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition) { |
28
|
|
|
$fieldName = $fieldDefinition->getName(); |
29
|
|
|
if (!$parents = $this->getParentsForField($entityTypeId, $fieldDefinition)) { |
30
|
|
|
return []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$derivative = [ |
34
|
|
|
'parents' => $parents, |
35
|
|
|
'name' => EntityField::getId($fieldName), |
36
|
|
|
'multi' => $fieldDefinition->isMultiple(), |
37
|
|
|
'field' => $fieldName, |
38
|
|
|
'schema_cache_tags' => array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']), |
39
|
|
|
'schema_cache_contexts' => $fieldDefinition->getCacheContexts(), |
40
|
|
|
'schema_cache_max_age' => $fieldDefinition->getCacheMaxAge(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$properties = $fieldDefinition->getPropertyDefinitions(); |
44
|
|
|
if (count($properties) === 1) { |
45
|
|
|
// Flatten the structure for single-property fields. |
46
|
|
|
/** @var \Drupal\Core\TypedData\DataDefinitionInterface $property */ |
47
|
|
|
$property = reset($properties); |
48
|
|
|
$keys = array_keys($properties); |
49
|
|
|
|
50
|
|
|
$derivative['type'] = $this->typeMapper->typedDataToGraphQLFieldType($property); |
51
|
|
|
$derivative['property'] = reset($keys); |
52
|
|
|
} |
53
|
|
|
else { |
54
|
|
|
$derivative['type'] = EntityFieldType::getId($entityTypeId, $fieldName); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return [ |
58
|
|
|
"$entityTypeId-$fieldName" => $derivative + $basePluginDefinition, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getParentsForField($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition) { |
63
|
|
|
if ($fieldDefinition->isBaseField()) { |
64
|
|
|
return [StringHelper::camelCase([$entityTypeId])]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($fieldDefinition instanceof FieldStorageConfigInterface) { |
|
|
|
|
68
|
|
|
return array_values(array_map(function ($bundleId) use ($entityTypeId) { |
69
|
|
|
return [StringHelper::camelCase([$entityTypeId, $bundleId])]; |
70
|
|
|
}, $fieldDefinition->getBundles())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return []; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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.