1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\Deriver\Fields; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\Derivative\DeriverBase; |
6
|
|
|
use Drupal\Core\Entity\ContentEntityTypeInterface; |
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
8
|
|
|
use Drupal\Core\Field\BaseFieldDefinition; |
9
|
|
|
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; |
10
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
11
|
|
|
use Drupal\Core\TypedData\TypedDataManager; |
12
|
|
|
use Drupal\graphql\Utility\StringHelper; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
class EntityQueryDeriver extends DeriverBase implements ContainerDeriverInterface { |
16
|
|
|
use StringTranslationTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The entity type manager service. |
20
|
|
|
* |
21
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
22
|
|
|
*/ |
23
|
|
|
protected $entityTypeManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The typed data manager service. |
27
|
|
|
* |
28
|
|
|
* @var \Drupal\Core\TypedData\TypedDataManager |
29
|
|
|
*/ |
30
|
|
|
protected $typedDataManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public static function create(ContainerInterface $container, $basePluginId) { |
36
|
|
|
return new static( |
37
|
|
|
$container->get('entity_type.manager'), |
38
|
|
|
$container->get('typed_data_manager') |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
47
|
|
|
TypedDataManager $typedDataManager |
48
|
|
|
) { |
49
|
|
|
$this->entityTypeManager = $entityTypeManager; |
50
|
|
|
$this->typedDataManager = $typedDataManager; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function getDerivativeDefinitions($basePluginDefinition) { |
|
|
|
|
57
|
|
|
foreach ($this->entityTypeManager->getDefinitions() as $id => $type) { |
58
|
|
|
if ($type instanceof ContentEntityTypeInterface) { |
|
|
|
|
59
|
|
|
$derivative = [ |
60
|
|
|
'name' => StringHelper::propCase($id, 'query'), |
61
|
|
|
'description' => $this->t("Loads '@type' entities.", ['@type' => $type->getLabel()]), |
62
|
|
|
'entity_type' => $id, |
63
|
|
|
] + $basePluginDefinition; |
64
|
|
|
|
65
|
|
|
$this->derivatives[$id] = $derivative; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return parent::getDerivativeDefinitions($basePluginDefinition); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.