ViewDeriverBase::getEntityTypeByTable()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.1111
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\Deriver;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Component\Plugin\PluginManagerInterface;
7
use Drupal\Component\Utility\NestedArray;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
10
use Drupal\graphql_views\Plugin\views\row\GraphQLEntityRow;
11
use Drupal\graphql_views\Plugin\views\row\GraphQLFieldRow;
12
use Drupal\graphql\Utility\StringHelper;
13
use Drupal\graphql_views\ViewDeriverHelperTrait;
14
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
15
use Drupal\views\ViewEntityInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
/**
19
 * Base class for graphql view derivers.
20
 */
21
abstract class ViewDeriverBase extends DeriverBase implements ContainerDeriverInterface {
22
  use ViewDeriverHelperTrait {
23
    getRowResolveType as private traitGetRowResolveType;
24
  }
25
  /**
26
   * The entity type manager.
27
   *
28
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
29
   */
30
  protected $entityTypeManager;
31
32
  /**
33
   * The interface plugin manager to search for return type candidates.
34
   *
35
   * @var \Drupal\Component\Plugin\PluginManagerInterface
36
   */
37
  protected $interfacePluginManager;
38
39
  /**
40
   * An key value pair of data tables and the entities they belong to.
41
   *
42
   * @var string[]
43
   */
44
  protected $dataTables;
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public static function create(ContainerInterface $container, $basePluginId) {
50
    return new static(
51
      $container->get('entity_type.manager'),
52
      $container->get('plugin.manager.graphql.interface')
53
    );
54
  }
55
56
  /**
57
   * Creates a ViewDeriver object.
58
   *
59
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
60
   *   An entity type manager instance.
61
   * @param \Drupal\Component\Plugin\PluginManagerInterface $interfacePluginManager
62
   *   The plugin manager for graphql interfaces.
63
   */
64
  public function __construct(
65
    EntityTypeManagerInterface $entityTypeManager,
66
    PluginManagerInterface $interfacePluginManager
67
  ) {
68
    $this->interfacePluginManager = $interfacePluginManager;
69
    $this->entityTypeManager = $entityTypeManager;
70
  }
71
72
  /**
73
   * Retrieves the entity type id of an entity by its base or data table.
74
   *
75
   * @param string $table
76
   *   The base or data table of an entity.
77
   *
78
   * @return string
79
   *   The id of the entity type that the given base table belongs to.
80
   */
81
  protected function getEntityTypeByTable($table) {
82
    if (!isset($this->dataTables)) {
83
      $this->dataTables = [];
84
85
      foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
86
        if ($dataTable = $entityType->getDataTable()) {
87
          $this->dataTables[$dataTable] = $entityType->id();
88
        }
89
        if ($baseTable = $entityType->getBaseTable()) {
90
          $this->dataTables[$baseTable] = $entityType->id();
91
        }
92
      }
93
    }
94
95
    return !empty($this->dataTables[$table]) ? $this->dataTables[$table] : NULL;
96
  }
97
98
  /**
99
   * Retrieves the type the view's rows resolve to.
100
   *
101
   * @param \Drupal\views\ViewEntityInterface $view
102
   *   The view entity.
103
   * @param string $displayId
104
   *   Interface plugin manager.
105
   *
106
   * @return null|string
107
   *   The name of the type or NULL if the type could not be derived.
108
   */
109
  protected function getRowResolveType(ViewEntityInterface $view, $displayId) {
110
    return $this->traitGetRowResolveType($view, $displayId, $this->interfacePluginManager);
111
  }
112
113
}
114