|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\Deriver\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\Derivative\DeriverBase; |
|
6
|
|
|
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface; |
|
7
|
|
|
use Drupal\Core\Config\TypedConfigManagerInterface; |
|
8
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
9
|
|
|
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; |
|
10
|
|
|
use Drupal\graphql\Utility\StringHelper; |
|
11
|
|
|
use Drupal\graphql_core\Plugin\GraphQL\Interfaces\Entity\EntityType; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ConfigEntityPropertyDeriver extends DeriverBase implements ContainerDeriverInterface { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The typed config manager service. |
|
18
|
|
|
* |
|
19
|
|
|
* @var \Drupal\Core\Config\TypedConfigManagerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $typedConfigManager; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The entity type manager service. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $entityTypeManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ConfigFieldDeriver constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
|
34
|
|
|
* The entity type manager service. |
|
35
|
|
|
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager |
|
36
|
|
|
* The typed config manager service. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(EntityTypeManagerInterface $entityTypeManager, TypedConfigManagerInterface $typedConfigManager) { |
|
39
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
40
|
|
|
$this->typedConfigManager = $typedConfigManager; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function create(ContainerInterface $container, $basePluginId) { |
|
47
|
|
|
return new static( |
|
48
|
|
|
$container->get('entity_type.manager'), |
|
49
|
|
|
$container->get('config.typed') |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getDerivativeDefinitions($basePluginDefinition) { |
|
57
|
|
|
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) { |
|
58
|
|
|
if (!$entityType instanceof ConfigEntityTypeInterface) { |
|
|
|
|
|
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$prefix = $entityType->getConfigPrefix(); |
|
63
|
|
|
if (!$this->typedConfigManager->hasDefinition("$prefix.*")) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$export = array_diff_key($entityType->getPropertiesToExport() ?: [], ['_core' => TRUE]); |
|
68
|
|
|
$definition = $this->typedConfigManager->getDefinition("$prefix.*"); |
|
69
|
|
|
if (empty($definition['mapping'])) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$properties = array_intersect_key($definition['mapping'], $export); |
|
74
|
|
|
foreach ($properties as $propertyName => $propertyDefinition) { |
|
75
|
|
|
if ($propertyDefinition['type'] !== 'string') { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->derivatives["$entityTypeId:$propertyName"] = [ |
|
80
|
|
|
'name' => StringHelper::propCase($propertyName), |
|
81
|
|
|
'type' => 'String', |
|
82
|
|
|
'parents' => [EntityType::getId($entityTypeId)], |
|
83
|
|
|
'property' => $propertyName, |
|
84
|
|
|
] + $basePluginDefinition; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return parent::getDerivativeDefinitions($basePluginDefinition); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto 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
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.