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\Plugin\Discovery\ContainerDeriverInterface; |
9
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
10
|
|
|
use Drupal\graphql\Utility\StringHelper; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
|
13
|
|
View Code Duplication |
class EntityRevisionByIdDeriver extends DeriverBase implements ContainerDeriverInterface { |
|
|
|
|
14
|
|
|
|
15
|
|
|
use StringTranslationTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The entity type manager service. |
19
|
|
|
* |
20
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
21
|
|
|
*/ |
22
|
|
|
protected $entityTypeManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public static function create(ContainerInterface $container, $basePluginId) { |
28
|
|
|
return new static($container->get('entity_type.manager')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function __construct(EntityTypeManagerInterface $entityTypeManager) { |
35
|
|
|
$this->entityTypeManager = $entityTypeManager; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function getDerivativeDefinitions($basePluginDefinition) { |
42
|
|
|
foreach ($this->entityTypeManager->getDefinitions() as $id => $type) { |
43
|
|
|
if ($type instanceof ContentEntityTypeInterface && $type->isRevisionable()) { |
|
|
|
|
44
|
|
|
$derivative = [ |
45
|
|
|
'name' => StringHelper::propCase($id, 'revision', 'by', 'id'), |
46
|
|
|
'type' => "entity:$id", |
47
|
|
|
'description' => $this->t("Loads '@type' entity revision by their revision id.", ['@type' => $type->getLabel()]), |
48
|
|
|
'entity_type' => $id, |
49
|
|
|
] + $basePluginDefinition; |
50
|
|
|
|
51
|
|
|
if ($type->isTranslatable()) { |
52
|
|
|
$derivative['arguments']['language'] = [ |
53
|
|
|
'type' => 'LanguageId', |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->derivatives["entity:$id"] = $derivative; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return parent::getDerivativeDefinitions($basePluginDefinition); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
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.