|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Drupal\TqExtension\Utils; |
|
6
|
|
|
|
|
7
|
|
|
use Drupal\TqExtension\Cores\DrupalKernelPlaceholder; |
|
8
|
|
|
|
|
9
|
|
|
final class EntityDrupalWrapper |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Entity type. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $type = ''; |
|
17
|
|
|
/** |
|
18
|
|
|
* Entity bundle. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $bundle = ''; |
|
23
|
|
|
/** |
|
24
|
|
|
* Entity object. |
|
25
|
|
|
* |
|
26
|
|
|
* @var object |
|
27
|
|
|
*/ |
|
28
|
|
|
private $entity; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $fields = [ |
|
33
|
|
|
'locators' => [], |
|
34
|
|
|
'required' => [], |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $entityType |
|
39
|
|
|
* @param string $bundle |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($entityType, $bundle = '') |
|
42
|
|
|
{ |
|
43
|
|
|
$this->type = $entityType; |
|
44
|
|
|
$this->bundle = $bundle ?: $this->type; |
|
45
|
|
|
|
|
46
|
|
|
// The fields in "locators" array stored by machine name of a field and duplicated by field label. |
|
47
|
|
|
foreach (DrupalKernelPlaceholder::getFieldDefinitions($this->type, $this->bundle) as $name => $definition) { |
|
48
|
|
|
$this->fields['locators'][$definition['label']] = $this->fields['locators'][$name] = $name; |
|
49
|
|
|
|
|
50
|
|
|
if ($definition['required']) { |
|
51
|
|
|
$this->fields['required'][$name] = $definition['label']; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function load($id) |
|
57
|
|
|
{ |
|
58
|
|
|
if (null === $this->entity) { |
|
59
|
|
|
$this->entity = DrupalKernelPlaceholder::entityLoad($this->type, $id); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Metadata wrapper needed since placeholder for Drupal 7 requires |
|
63
|
|
|
* \EntityDrupalWrapper as an argument. |
|
64
|
|
|
* |
|
65
|
|
|
* @see \Drupal\TqExtension\Cores\Drupal7Placeholder::entityFieldValue() |
|
66
|
|
|
*/ |
|
67
|
|
|
if (DRUPAL_CORE < 8) { |
|
68
|
|
|
$this->entity = entity_metadata_wrapper($this->type, $this->entity); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->entity; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function hasField($fieldName) |
|
76
|
|
|
{ |
|
77
|
|
|
return DrupalKernelPlaceholder::entityHasField( |
|
78
|
|
|
$this->getEntity(), |
|
79
|
|
|
$this->getFieldNameByLocator($fieldName) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getFieldValue($fieldName) |
|
84
|
|
|
{ |
|
85
|
|
|
return DrupalKernelPlaceholder::entityFieldValue( |
|
86
|
|
|
$this->getEntity(), |
|
87
|
|
|
$this->getFieldNameByLocator($fieldName) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $fieldName |
|
93
|
|
|
* Machine name or label of a field. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getFieldNameByLocator($fieldName) |
|
98
|
|
|
{ |
|
99
|
|
|
return isset($this->fields['locators'][$fieldName]) ? $this->fields['locators'][$fieldName] : ''; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return array[] |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getRequiredFields() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->fields['required']; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return object |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getEntity() |
|
114
|
|
|
{ |
|
115
|
|
|
if (null === $this->entity) { |
|
116
|
|
|
throw new \RuntimeException('You have to load an entity before getting it.'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->entity; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|