|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_views\Plugin\views\row; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
6
|
|
|
use Drupal\Core\Entity\EntityRepositoryInterface; |
|
7
|
|
|
use Drupal\Core\Entity\EntityTypeBundleInfo; |
|
8
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
9
|
|
|
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter; |
|
10
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
|
11
|
|
|
use Drupal\views\Entity\Render\EntityTranslationRenderTrait; |
|
12
|
|
|
use Drupal\views\Plugin\views\row\RowPluginBase; |
|
13
|
|
|
use Drupal\views\ResultRow; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Plugin which displays entities as raw data. |
|
18
|
|
|
* |
|
19
|
|
|
* @ViewsRow( |
|
20
|
|
|
* id = "graphql_entity", |
|
21
|
|
|
* title = @Translation("Entity"), |
|
22
|
|
|
* help = @Translation("Use entities as row data."), |
|
23
|
|
|
* display_types = {"graphql"} |
|
24
|
|
|
* ) |
|
25
|
|
|
*/ |
|
26
|
|
|
class GraphQLEntityRow extends RowPluginBase { |
|
27
|
|
|
|
|
28
|
|
|
use EntityTranslationRenderTrait { |
|
29
|
|
|
getEntityTranslationRenderer as getEntityTranslationRendererBase; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $usesOptions = FALSE; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Contains the entity type of this row plugin instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Drupal\Core\Entity\EntityTypeInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $entityType; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The entity type bundle info. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \Drupal\Core\Entity\EntityTypeBundleInfo |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $entityTypeBundleInfo; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The entity type manager. |
|
53
|
|
|
* |
|
54
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManager |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $entityTypeManager; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The entity type bundle info. |
|
60
|
|
|
* |
|
61
|
|
|
* @var \Drupal\Core\Entity\EntityRepository |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $entityRepository; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The language manager. |
|
67
|
|
|
* |
|
68
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $languageManager; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Drupal\Core\Entity\EntityTypeBundleInfo $entityTypeBundleInfo |
|
76
|
|
|
* The entity type manager. |
|
77
|
|
|
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager |
|
78
|
|
|
* The language manager. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeBundleInfo $entityTypeBundleInfo, LanguageManagerInterface $languageManager, EntityTypeManagerInterface $entityTypeManager, EntityRepositoryInterface $entityRepository) { |
|
81
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
82
|
|
|
|
|
83
|
|
|
$this->entityTypeBundleInfo = $entityTypeBundleInfo; |
|
84
|
|
|
$this->languageManager = $languageManager; |
|
85
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
|
|
|
|
|
86
|
|
|
$this->entityRepository = $entityRepository; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
93
|
|
|
return new static( |
|
94
|
|
|
$configuration, |
|
95
|
|
|
$pluginId, |
|
96
|
|
|
$pluginDefinition, |
|
97
|
|
|
$container->get('entity_type.bundle.info'), |
|
98
|
|
|
$container->get('language_manager'), |
|
99
|
|
|
$container->get('entity_type.manager'), |
|
100
|
|
|
$container->get('entity.repository') |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function render($row) { |
|
108
|
|
|
if ($entity = $this->getEntityFromRow($row)) { |
|
109
|
|
|
return $this->view->getBaseEntityType() ? $this->getEntityTranslation($entity, $row) : $entity; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return NULL; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function getEntityTranslationRenderer() { |
|
119
|
|
|
if ($this->view->getBaseEntityType()) { |
|
120
|
|
|
return $this->getEntityTranslationRendererBase(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return NULL; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getEntityTypeManager() { |
|
130
|
|
|
return $this->entityTypeManager; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getEntityRepository() { |
|
137
|
|
|
return $this->entityRepository; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getEntityTypeId() { |
|
144
|
|
|
if ($entityType = $this->view->getBaseEntityType()) { |
|
145
|
|
|
return $entityType->id(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return NULL; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function getEntityTypeBundleInfo() { |
|
155
|
|
|
return $this->entityTypeBundleInfo; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getLanguageManager() { |
|
162
|
|
|
return $this->languageManager; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Retrieves the entity object from a result row. |
|
167
|
|
|
* |
|
168
|
|
|
* @param \Drupal\Views\ResultRow $row |
|
169
|
|
|
* The views result row object. |
|
170
|
|
|
* |
|
171
|
|
|
* @return null|\Drupal\Core\Entity\EntityInterface |
|
172
|
|
|
* The extracted entity object or NULL if it could not be retrieved. |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getEntityFromRow(ResultRow $row) { |
|
175
|
|
|
if (isset($row->_entity) && $row->_entity instanceof EntityInterface) { |
|
|
|
|
|
|
176
|
|
|
return $row->_entity; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (isset($row->_object) && $row->_object instanceof EntityAdapter) { |
|
|
|
|
|
|
180
|
|
|
return $row->_object->getValue(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return NULL; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function getView() { |
|
190
|
|
|
return $this->view; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritdoc} |
|
195
|
|
|
*/ |
|
196
|
|
|
public function query() { |
|
197
|
|
|
parent::query(); |
|
198
|
|
|
|
|
199
|
|
|
if ($this->view->getBaseEntityType()) { |
|
200
|
|
|
$this->getEntityTranslationRenderer()->query($this->view->getQuery()); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..