|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
|
6
|
|
|
use Drupal\Core\Entity\ContentEntityInterface; |
|
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
9
|
|
|
use Drupal\Core\Render\RendererInterface; |
|
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
|
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface; |
|
12
|
|
|
use Drupal\graphql\Utility\StringHelper; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
15
|
|
|
use Youshido\GraphQL\Field\InputField; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @GraphQLField( |
|
19
|
|
|
* id = "entity_rendered", |
|
20
|
|
|
* name = "entityRendered", |
|
21
|
|
|
* type = "String", |
|
22
|
|
|
* secure = true, |
|
23
|
|
|
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRenderedDeriver", |
|
24
|
|
|
* ) |
|
25
|
|
|
*/ |
|
26
|
|
|
class EntityRendered extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
27
|
|
|
use DependencySerializationTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The entity type manager service. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $entityTypeManager; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The renderer service. |
|
38
|
|
|
* |
|
39
|
|
|
* @var \Drupal\Core\Render\RendererInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $renderer; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
array $configuration, |
|
48
|
|
|
$pluginId, |
|
49
|
|
|
$pluginDefinition, |
|
50
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
|
51
|
|
|
RendererInterface $renderer |
|
52
|
|
|
) { |
|
53
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
54
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
55
|
|
|
$this->renderer = $renderer; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
62
|
|
|
return new static( |
|
63
|
|
|
$configuration, |
|
64
|
|
|
$pluginId, |
|
65
|
|
|
$pluginDefinition, |
|
66
|
|
|
$container->get('entity_type.manager'), |
|
67
|
|
|
$container->get('renderer') |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function buildArguments(PluggableSchemaBuilderInterface $schemaBuilder) { |
|
75
|
|
|
$arguments = parent::buildArguments($schemaBuilder); |
|
76
|
|
|
|
|
77
|
|
|
if (empty($arguments['mode'])) { |
|
78
|
|
|
$definition = $this->getPluginDefinition(); |
|
79
|
|
|
$type = StringHelper::camelCase($definition['entity_type'], 'display', 'mode', 'id'); |
|
80
|
|
|
|
|
81
|
|
|
if ($type = $schemaBuilder->findByName($type, [GRAPHQL_ENUM_PLUGIN])) { |
|
82
|
|
|
$arguments['mode'] = new InputField([ |
|
83
|
|
|
'name' => 'mode', |
|
84
|
|
|
'type' => $type->getDefinition($schemaBuilder), |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $arguments; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
|
96
|
|
|
if ($value instanceof ContentEntityInterface) { |
|
|
|
|
|
|
97
|
|
|
$mode = isset($args['mode']) ? $args['mode'] : 'full'; |
|
98
|
|
|
$language = isset($args['language']) ? $args['language'] : $value->language()->getId(); |
|
99
|
|
|
|
|
100
|
|
|
$builder = $this->entityTypeManager->getViewBuilder($value->getEntityTypeId()); |
|
101
|
|
|
$rendered = $builder->view($value, $mode, $language); |
|
102
|
|
|
yield $this->renderer->render($rendered); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|