This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
forked from paolomainardi/twinbit-drupal-metadata-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwinbitFrontendMetadata.php
331 lines (303 loc) · 9.51 KB
/
TwinbitFrontendMetadata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* @file
* Provides an abstract metadata wrapper allowing easy usage of the entity metadata.
*/
class TwinbitFrontendMetadata {
private $_wrapper;
private $_type;
private $_entity;
/**
* Construct a new wrapper object.
*
* @param $type
* The type of the passed data.
* @param $entity
* Optional. Drupal entity object
*
* @param $wrapper
* Optional. EntityDrupalWrapper instance.
*/
public function __construct($type, $entity = null, $wrapper = null) {
$this->setType($type);
if (!$wrapper) {
$this->setWrapper(entity_metadata_wrapper($type, $entity));
}
else {
$this->setWrapper($wrapper);
}
$wrapper = $this->getWrapper();
if ($entity) {
if (is_numeric($entity)) {
$entity = entity_load($type, array($entity));
$entity = array_pop($entity);
}
$this->setEntity($entity);
}
else {
$entity = entity_load($type, array($wrapper->getIdentifier()));
if ($entity) {
$entity = array_pop($entity);
$entity->type = $type;
$this->setEntity($entity);
}
}
}
/**
* Gets the info about the given property
*
* @param $field_name
* Optional. Field name of the entity.
*
* @return
* An array of info about the property.
*/
public function getProperties($field_name = '') {
$wrapper = $this->getWrapper();
if ($field_name) {
$info = $wrapper->{$field_name}->getPropertyInfo();
}
$info = $wrapper->getPropertyInfo();
return $info;
}
/**
* Example:
*
* Get all the titles of the referenced nodes:
*
* $frontend = new TwinbitFrontend('node', $entity);
*
* (a more generic version is $frontend = new TwinbitFrontend($entity['entity_type'], $entity) )
*
* $files = $frontend->get_wrapper_field('field_product_reference_file');
* foreach ($files as $file) {
* $titles[] = $file->get_field_value('title'));
* }
*
* Access to referenced nodes and get all taxonomies of type "field_file_type"
*
* $terms = array();
* $referenced_entity_files = $object->get_referenced_wrappers('field_product_reference_file', TRUE);
*
* Recupero l'identificativo del termine del logo
*
* foreach ($referenced_entity_files as $file) {
* $terms[] = $file->metadata_get_field_value('field_file_type'); // load taxonomy object attached to
* $file_ref = $file->getEntity(); // proxy method to get the raw entity (in this case node(@type=file) referenced)
* }
*
* @param $field_name
* Field name to load.
*
* @param $full
* False: return just the existing entity_metadata_wrapper object (this is useful if you need just plain values, examples "$node->title" or $term->tid)
* True: return a full TwinbitFrontendMetadata object
*
* @return Collection of TwinbitFrontend instances
*/
public function get_referenced_wrappers($field_name, $full = false) {
$wrapper = $this->getWrapper();
if (!$wrapper->{$field_name}->value()) {
return array();
}
$instances = array();
foreach ($wrapper->{$field_name} as $wrapper) {
$type = $wrapper->type();
$id = $wrapper->getIdentifier();
if ($full) {
$instances[] = new TwinbitFrontendMetadata($type, $wrapper->getIdentifier());
}
else {
$instances[] = new TwinbitFrontendMetadata($type, null, $wrapper);
}
}
return $instances;
}
/**
* This a wrapper around the EntityDrupalWrapper->value() method
* Examples:
* // get non sanitized value
* $value = $object->get_field_value_single('field_name', $attr, array('decode' => true));
* // When $attr is null
* $value = $object->get_field_value_single('field_name', null, array('decode' => true));
*
* @param $field_name
* Entity field name
* @param $attr
* Collection attribute, return element attribute if exists
* @param $options
* An array of options. Known keys:
* - identifier: If set to TRUE for a list of entities, it won't be returned
* as list of fully loaded entity objects, but as a list of entity ids.
* Note that this list may contain ids of stale entity references.
*/
public function get_field_value_single($field_name, $attr = false, $options = array()) {
$res = $this->get_field_value($field_name, 0, $attr, $options);
return $res;
}
/**
* Set a field value.
* References:
* http://drupal.stackexchange.com/questions/50358/save-a-new-value-with-entity-metadata-wrapper-to-an-entity-field-which-is-an-arr
* http://drupal.stackexchange.com/questions/66963/how-do-you-clear-a-field-value-with-entity-metadata-wrapper
* @param $field_name
* Entity field name
*
* @param $value
* Value
*/
public function set_field_value($field_name, $value = null) {}
/**
* Examples:
* // get non sanitized value
* $value = $object->get_field_value('field_name', $index, $attr, array('decode' => true));
* // When $index and $attr are null
* $value = $object->get_field_value('field_name', null, null, array('decode' => true));
*
*
* This a wrapper around the EntityDrupalWrapper->value() method
*
* @param $field_name
* Entity field name
* @param $index
* Collection index
* @param $attr
* Collection attribute, this can be used just when $index is active
* @param $options
* An array of options. Known keys:
* - identifier: If set to TRUE for a list of entities, it won't be returned
* as list of fully loaded entity objects, but as a list of entity ids.
* Note that this list may contain ids of stale entity references.
* - sanitize: If set to TRUE you will get the sanitezed version (please note that some values are sanitized by default) (default to TRUE)
* - decode: Get the "non sanitized by default" value representation.
*
*/
public function get_field_value($field_name, $index = null, $attr = null, $options = array()) {
$wrapper = $this->getWrapper();
$properties = $this->getProperties();
$entity = $this->getEntity();
$collection = array();
// standard option, sanitize by default for all fields
$default_options = array('sanitize' => TRUE);
$options = $default_options + $options;
// some "bad" fields could not to be accesible from metadata_wrapper try to lookup within the entity
if (!array_key_exists($field_name, $properties)) {
if (isset($entity->{$field_name})) {
$field = $entity->{$field_name};
if (isset($field[LANGUAGE_NONE])) {
$values = $field[LANGUAGE_NONE];
// we can't make any more assumption here, just return raw values.
return $values;
}
}
}
else {
// check if the count method exists (cardinality > 1)
if (method_exists($wrapper->{$field_name}, 'count')) {
if ($index) {
$object = $wrapper->{$field_name}->get($index)->value($options);
// put the single value in array
if ($object) {
$collection[] = $object;
}
}
else {
// this is already a collection, overwrite the array
if ($value = $wrapper->{$field_name}->value($options)) {
$collection = $value;
}
}
}
else {
$object = $wrapper->{$field_name}->value($options);
if ($object) {
$collection[] = $wrapper->{$field_name}->value($options);
}
}
// check index and attributes
if (is_numeric($index) && (count($collection))) {
if (isset($collection[$index]) && is_array($collection)) {
$collection = $collection[$index];
if ($attr) {
if (isset($collection[$attr])) {
$collection = $collection[$attr];
}
}
}
}
}
// if we don't have elements, return just false
if (!count($collection)) {
$collection = false;
}
return $collection;
}
/**
* Get a field property
*
* Only for field that are not a reference to another entity
* (i.e. not for field collections, files, images, taxonomy terms)
*
* Example:
* $node_metadata = new TwinbitFrontendMetadata('node', $node);
* $link_title = $node_metadata->get_field_property('field_link', 'title');
* $link_url = $node_metadata->get_field_property('field_link', 'url');
*
* @param $fieldname
* Entity field name
* @param $property
* Field property name
*
* @return Mixed Property value
*/
public function get_field_property($field_name, $property) {
$wrapper = $this->getWrapper();
$propertyInfo = $this->getProperties($field_name);
if (!$wrapper->{$field_name}->value()) {
return false;
}
if (!array_key_exists($property, $propertyInfo)) {
return false;
}
$output = $wrapper->{$field_name}->{$property}->value();
return $output;
}
private function setType($type) {
$this->_type = $type;
}
public function getType() {
return $this->_type;
}
public function getEntity() {
return $this->_entity;
}
private function setEntity($entity) {
$this->_entity = $entity;
}
public function getWrapper() {
return $this->_wrapper;
}
private function setWrapper($wrapper) {
$this->_wrapper = $wrapper;
}
/**
* OLD PROXY METHOD (retrocompatibility)
* @return Collection of TwinbitFrontend instances
*/
public function metadata_get_field_value($field_name, $index = false, $attr = false, $options = array()) {
return $this->get_field_value($field_name, $options);
}
/**
* OLD PROXY METHOD (retrocompatibility)
* @return Collection of TwinbitFrontend instances
*/
public function metadata_get_referenced_wrappers($field_name, $full = false) {
return $this->get_referenced_wrappers($field_name, $full);
}
/**
* OLD PROXY METHOD (retrocompatibility)
*/
public function metadata_get_field_property($field_name, $property) {
return $this->get_field_property();
}
}