Completed
Pull Request — 8.x-1.x (#39)
by
unknown
01:05
created

GraphQLEntityRow::getEntityTranslationRenderer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\EntityTypeBundleInfo;
7
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
8
use Drupal\Core\Language\LanguageManagerInterface;
9
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
10
use Drupal\views\Plugin\views\row\RowPluginBase;
11
use Drupal\views\ResultRow;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Plugin which displays entities as raw data.
16
 *
17
 * @ViewsRow(
18
 *   id = "graphql_entity",
19
 *   title = @Translation("Entity"),
20
 *   help = @Translation("Use entities as row data."),
21
 *   display_types = {"graphql"}
22
 * )
23
 */
24
class GraphQLEntityRow extends RowPluginBase {
25
26
  use EntityTranslationRenderTrait {
27
    getEntityTranslationRenderer as getEntityTranslationRendererBase;
28
  }
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  protected $usesOptions = FALSE;
34
35
  /**
36
   * Contains the entity type of this row plugin instance.
37
   *
38
   * @var \Drupal\Core\Entity\EntityTypeInterface
39
   */
40
  protected $entityType;
41
42
  /**
43
   * The entity type bundle info.
44
   *
45
   * @var \Drupal\Core\Entity\EntityTypeBundleInfo
46
   */
47
  protected $entityTypeBundleInfo;
48
49
  /**
50
   * The language manager.
51
   *
52
   * @var \Drupal\Core\Language\LanguageManagerInterface
53
   */
54
  protected $languageManager;
55
56
  /**
57
   * {@inheritdoc}
58
   *
59
   * @param \Drupal\Core\Entity\EntityTypeBundleInfo $entityTypeBundleInfo
60
   *   The entity type manager.
61
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
62
   *   The language manager.
63
   */
64
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeBundleInfo $entityTypeBundleInfo, LanguageManagerInterface $languageManager) {
65
    parent::__construct($configuration, $pluginId, $pluginDefinition);
66
67
    $this->entityTypeBundleInfo = $entityTypeBundleInfo;
68
    $this->languageManager = $languageManager;
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
75
    return new static(
76
      $configuration,
77
      $pluginId,
78
      $pluginDefinition,
79
      $container->get('entity_type.bundle.info'),
80
      $container->get('language_manager')
81
    );
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function render($row) {
88
    if ($entity = $this->getEntityFromRow($row)) {
89
      return $this->view->getBaseEntityType() ? $this->getEntityTranslation($entity, $row) : $entity;
90
    }
91
92
    return NULL;
93
  }
94
95
  /**
96
   * {@inheritdoc}
97
   */
98
  protected function getEntityTranslationRenderer() {
99
    if ($this->view->getBaseEntityType()) {
100
      return $this->getEntityTranslationRendererBase();
101
    }
102
103
    return NULL;
104
  }
105
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function getEntityTypeId() {
110
    if ($entityType = $this->view->getBaseEntityType()) {
111
      return $entityType->id();
112
    }
113
114
    return NULL;
115
  }
116
117
  /**
118
   * {@inheritdoc}
119
   */
120
  protected function getEntityTypeBundleInfo() {
121
    return $this->entityTypeBundleInfo;
122
  }
123
124
  /**
125
   * {@inheritdoc}
126
   */
127
  protected function getLanguageManager() {
128
    return $this->languageManager;
129
  }
130
131
  /**
132
   * Retrieves the entity object from a result row.
133
   *
134
   * @param \Drupal\Views\ResultRow $row
135
   *   The views result row object.
136
   *
137
   * @return null|\Drupal\Core\Entity\EntityInterface
138
   *   The extracted entity object or NULL if it could not be retrieved.
139
   */
140
  protected function getEntityFromRow(ResultRow $row) {
141
    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...
142
      return $row->_entity;
143
    }
144
145
    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...
146
      return $row->_object->getValue();
147
    }
148
149
    return NULL;
150
  }
151
152
  /**
153
   * {@inheritdoc}
154
   */
155
  protected function getView() {
156
    return $this->view;
157
  }
158
159
  /**
160
   * {@inheritdoc}
161
   */
162
  public function query() {
163
    parent::query();
164
165
    if ($this->view->getBaseEntityType()) {
166
      $this->getEntityTranslationRenderer()->query($this->view->getQuery());
167
    }
168
  }
169
170
}
171