|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Wrapper\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Driver\Plugin\DriverPluginManagerInterface; |
|
6
|
|
|
use Drupal\Core\Config\Entity\ConfigEntityInterface; |
|
7
|
|
|
use Drupal\Component\Utility\DriverNameMatcher; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A Driver field object that holds information about Drupal 8 field. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DriverFieldDrupal8 extends DriverFieldBase implements DriverFieldInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* General field definition (D7 field definition, D8: field_config). |
|
17
|
|
|
* |
|
18
|
|
|
* @var object|array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $definition; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Particular field definition (D7 field instance definition, D8: |
|
24
|
|
|
* field_storage_config). |
|
25
|
|
|
* |
|
26
|
|
|
* @var object|array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $storageDefinition; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Whether this driver field is wrapping the property of a config entity, not |
|
32
|
|
|
* the field of a content entity. |
|
33
|
|
|
* |
|
34
|
|
|
* @var boolean |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $isConfigProperty = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The config schema of this config entity property |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $configSchema; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The Drupal version being driven. |
|
47
|
|
|
* |
|
48
|
|
|
* @var integer |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $version = 8; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct( |
|
53
|
|
|
$rawValues, |
|
54
|
|
|
$fieldName, |
|
55
|
|
|
$entityType, |
|
56
|
|
|
$bundle = null, |
|
57
|
|
|
$projectPluginRoot = null, |
|
58
|
|
|
$fieldPluginManager = null |
|
59
|
|
|
) { |
|
60
|
|
|
$entityTypeDefinition = \Drupal::EntityTypeManager() |
|
61
|
|
|
->getDefinition($entityType); |
|
62
|
|
|
if ($entityTypeDefinition->entityClassImplements(ConfigEntityInterface::class)) { |
|
63
|
|
|
$this->isConfigProperty = true; |
|
64
|
|
|
$configPrefix = $entityTypeDefinition->getConfigPrefix(); |
|
65
|
|
|
$configProperties = \Drupal::service('config.typed')->getDefinition("$configPrefix.*")['mapping']; |
|
66
|
|
|
$this->configSchema = $configProperties; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Set Drupal environment variables used by default plugin manager. |
|
70
|
|
|
$this->namespaces = \Drupal::service('container.namespaces'); |
|
|
|
|
|
|
71
|
|
|
$this->cache_backend = $cache_backend = \Drupal::service('cache.discovery'); |
|
|
|
|
|
|
72
|
|
|
$this->module_handler = $module_handler = \Drupal::service('module_handler'); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
parent::__construct($rawValues, $fieldName, $entityType, $bundle, $projectPluginRoot, $fieldPluginManager); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getDefinition() |
|
81
|
|
|
{ |
|
82
|
|
|
if (is_null($this->definition) && !$this->isConfigProperty) { |
|
83
|
|
|
$entityFieldManager = \Drupal::service('entity_field.manager'); |
|
84
|
|
|
$definitions = $entityFieldManager->getFieldDefinitions($this->getEntityType(), $this->getBundle()); |
|
85
|
|
|
$this->definition = $definitions[$this->getName()]; |
|
86
|
|
|
} |
|
87
|
|
|
return $this->definition; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getStorageDefinition() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->getDefinition()->getFieldStorageDefinition(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getType() |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->isConfigProperty) { |
|
104
|
|
|
return $this->configSchema[$this->getName()]['type']; |
|
105
|
|
|
} else { |
|
106
|
|
|
return $this->getDefinition()->getType(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function isConfigProperty() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->isConfigProperty; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the machine name of the field from a human-readable identifier. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
* The machine name of a field. |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function identify($identifier) |
|
125
|
|
|
{ |
|
126
|
|
|
// Get all the candidate fields. Assemble them into an array of field |
|
127
|
|
|
// machine names and labels ready for DriverNameMatcher. Read-only fields |
|
128
|
|
|
// are not removed because DriverFields can be used for comparing as well |
|
129
|
|
|
// as writing values. |
|
130
|
|
|
$candidates = []; |
|
131
|
|
|
if ($this->isConfigProperty()) { |
|
132
|
|
|
foreach ($this->configSchema as $id => $subkeys) { |
|
133
|
|
|
$label = isset($subkeys['label']) ? $subkeys['label'] : $id; |
|
134
|
|
|
$candidates[$label] = $id; |
|
135
|
|
|
} |
|
136
|
|
|
} else { |
|
137
|
|
|
$entityManager = \Drupal::service('entity_field.manager'); |
|
138
|
|
|
$fields = $entityManager->getFieldDefinitions($this->entityType, $this->bundle); |
|
139
|
|
|
foreach ($fields as $machineName => $definition) { |
|
140
|
|
|
$label = (string) $definition->getLabel(); |
|
141
|
|
|
$label = empty($label) ? $machineName : $label; |
|
142
|
|
|
$candidates[$label] = $machineName; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$matcher = new DriverNameMatcher($candidates, "field_"); |
|
147
|
|
|
$result = $matcher->identify($identifier); |
|
148
|
|
|
if (is_null($result)) { |
|
149
|
|
|
throw new \Exception("Field or property cannot be identified. '$identifier' does not match anything on '" . $this->getEntityType(). "'."); |
|
150
|
|
|
} |
|
151
|
|
|
return $result; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: