Completed
Pull Request — 8.x-1.x (#22)
by Sebastian
02:52 queued 01:21
created

ViewDeriverBase::getPagerLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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