EntityToJsonDeriver::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_json\Plugin\Deriver;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Extension\ModuleHandlerInterface;
7
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
10
/**
11
 * Only add the field if the serializer module is enabled.
12
 */
13
class EntityToJsonDeriver extends DeriverBase implements ContainerDeriverInterface {
14
15
  /**
16
   *  The module handler service.
17
   *
18
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
19
   */
20
  protected $moduleHandler;
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public static function create(ContainerInterface $container, $basePluginId) {
26
    return new static(
27
      $container->get('module_handler')
28
    );
29
  }
30
31
  /**
32
   * Creates a EntityToJsonDeriver object.
33
   *
34
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
35
   *   The module handler service.
36
   */
37
  public function __construct(ModuleHandlerInterface $moduleHandler) {
38
    $this->moduleHandler = $moduleHandler;
39
  }
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public function getDerivativeDefinitions($basePluginDefinition) {
45
    if ($this->moduleHandler->moduleExists('serialization')) {
46
      $this->derivatives['entity_to_json'] = $basePluginDefinition;
47
    }
48
49
    return parent::getDerivativeDefinitions($basePluginDefinition);
50
  }
51
52
}
53