Completed
Push — 8.x-1.x ( a78f8f...f9121f )
by Philipp
01:16
created

GraphQLEntityRow::getEntityRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $entityTypeManager of type object<Drupal\Core\Entit...tyTypeManagerInterface> is incompatible with the declared type object<Drupal\Core\Entity\EntityTypeManager> of property $entityTypeManager.

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..

Loading history...
86
    $this->entityRepository = $entityRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $entityRepository of type object<Drupal\Core\Entit...ityRepositoryInterface> is incompatible with the declared type object<Drupal\Core\Entity\EntityRepository> of property $entityRepository.

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..

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\EntityInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
176
      return $row->_entity;
177
    }
178
179
    if (isset($row->_object) && $row->_object instanceof EntityAdapter) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\Plugin\DataType\EntityAdapter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to 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 require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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