Completed
Pull Request — master (#157)
by
unknown
12:36
created

DriverFieldDrupal8::getStorageDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Drupal\Driver\Wrapper\Field;
4
5
use Drupal\Core\Config\Entity\ConfigEntityInterface;
6
use Drupal\Driver\Plugin\DriverNameMatcher;
7
8
/**
9
 * A Driver field object that holds information about Drupal 8 field.
10
 */
11
class DriverFieldDrupal8 extends DriverFieldBase implements DriverFieldInterface {
12
13
  /**
14
   * The general field definition.
15
   *
16
   * For D7 this is the field definition, for
17
   * D8 the field_config.
18
   *
19
   * @var object|array
20
   */
21
  protected $definition;
22
23
  /**
24
   * The particular field definition.
25
   *
26
   * For D7 this is the field instance definition, for D8 the
27
   * field_storage_config.
28
   *
29
   * @var object|array
30
   */
31
  protected $storageDefinition;
32
33
  /**
34
   * Whether this driver field is wrapping the property of a config entity.
35
   *
36
   * The wrapper can hold config entity properties or content entity fields.
37
   *
38
   * @var bool
39
   */
40
  protected $isConfigProperty = FALSE;
41
42
  /**
43
   * The config schema of this config entity property.
44
   *
45
   * @var array
46
   */
47
  protected $configSchema;
48
49
  /**
50
   * The Drupal version being driven.
51
   *
52
   * @var int
53
   */
54
  protected $version = 8;
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function __construct(
60
        $rawValues,
61
        $fieldName,
62
        $entityType,
63
        $bundle = NULL,
64
        $projectPluginRoot = NULL,
65
        $fieldPluginManager = NULL
66
    ) {
67
    $entityTypeDefinition = \Drupal::EntityTypeManager()
68
      ->getDefinition($entityType);
69
    if ($entityTypeDefinition->entityClassImplements(ConfigEntityInterface::class)) {
70
      $this->isConfigProperty = TRUE;
71
      $configPrefix = $entityTypeDefinition->getConfigPrefix();
72
      $configProperties = \Drupal::service('config.typed')->getDefinition("$configPrefix.*")['mapping'];
73
      $this->configSchema = $configProperties;
74
    }
75
76
    // Set Drupal environment variables used by default plugin manager.
77
    $this->namespaces = \Drupal::service('container.namespaces');
0 ignored issues
show
Bug introduced by
The property namespaces does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
    $this->cache_backend = $cache_backend = \Drupal::service('cache.discovery');
0 ignored issues
show
Bug introduced by
The property cache_backend does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$cache_backend is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
    $this->module_handler = $module_handler = \Drupal::service('module_handler');
0 ignored issues
show
Bug introduced by
The property module_handler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$module_handler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
81
    parent::__construct($rawValues, $fieldName, $entityType, $bundle, $projectPluginRoot, $fieldPluginManager);
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function getDefinition() {
88
    if (is_null($this->definition) && !$this->isConfigProperty) {
89
      $entityFieldManager = \Drupal::service('entity_field.manager');
90
      $definitions = $entityFieldManager->getFieldDefinitions($this->getEntityType(), $this->getBundle());
91
      $this->definition = $definitions[$this->getName()];
92
    }
93
    return $this->definition;
94
  }
95
96
  /**
97
   * {@inheritdoc}
98
   */
99
  public function getStorageDefinition() {
100
    return $this->getDefinition()->getFieldStorageDefinition();
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function getType() {
107
    if ($this->isConfigProperty) {
108
      return $this->configSchema[$this->getName()]['type'];
109
    }
110
    else {
111
      return $this->getDefinition()->getType();
112
    }
113
  }
114
115
  /**
116
   * {@inheritdoc}
117
   */
118
  public function isConfigProperty() {
119
    return $this->isConfigProperty;
120
  }
121
122
  /**
123
   * Get the machine name of the field from a human-readable identifier.
124
   *
125
   * @return string
126
   *   The machine name of a field.
127
   */
128
  protected function identify($identifier) {
129
    // Get all the candidate fields. Assemble them into an array of field
130
    // machine names and labels ready for DriverNameMatcher. Read-only fields
131
    // are not removed because DriverFields can be used for comparing as well
132
    // as writing values.
133
    $candidates = [];
134
    if ($this->isConfigProperty()) {
135
      foreach ($this->configSchema as $id => $subkeys) {
136
        $label = isset($subkeys['label']) ? $subkeys['label'] : $id;
137
        $candidates[$label] = $id;
138
      }
139
    }
140
    else {
141
      $entityManager = \Drupal::service('entity_field.manager');
142
      $fields = $entityManager->getFieldDefinitions($this->entityType, $this->bundle);
143
      foreach ($fields as $machineName => $definition) {
144
        $label = (string) $definition->getLabel();
145
        $label = empty($label) ? $machineName : $label;
146
        $candidates[$label] = $machineName;
147
      }
148
    }
149
150
    $matcher = new DriverNameMatcher($candidates, "field_");
151
    $result = $matcher->identify($identifier);
152
    if (is_null($result)) {
153
      throw new \Exception("Field or property cannot be identified. '$identifier' does not match anything on '" . $this->getEntityType() . "'.");
154
    }
155
    return $result;
156
  }
157
158
}
159