|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Driver\Fields\Drupal7; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Entityreference field handler for Drupal 7. |
|
7
|
|
|
*/ |
|
8
|
|
|
class EntityreferenceHandler extends AbstractHandler { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* {@inheritdoc} |
|
12
|
|
|
*/ |
|
13
|
|
|
public function expand($values) { |
|
14
|
|
|
$entity_type = $this->getEntityType(); |
|
15
|
|
|
$entity_info = entity_get_info($entity_type); |
|
16
|
|
|
// For users set label to username. |
|
17
|
|
|
if ($entity_type == 'user') { |
|
18
|
|
|
$entity_info['entity keys']['label'] = 'name'; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$return = array(); |
|
22
|
|
|
foreach ($values as $value) { |
|
23
|
|
|
// Extract target id by step. Otherwise, try get entity searching |
|
24
|
|
|
// by entity label. |
|
25
|
|
|
if (is_array($value) && !empty($value['target_id']) && $this->entityExists($value['target_id'])) { |
|
26
|
|
|
$target_id = $value['target_id']; |
|
27
|
|
|
} |
|
28
|
|
|
else { |
|
29
|
|
|
$target_id = db_select($entity_info['base table'], 't') |
|
30
|
|
|
->fields('t', array($entity_info['entity keys']['id'])) |
|
31
|
|
|
->condition('t.' . $entity_info['entity keys']['label'], $value) |
|
32
|
|
|
->execute()->fetchField(); |
|
33
|
|
|
} |
|
34
|
|
|
if ($target_id) { |
|
35
|
|
|
$return[$this->language][] = array('target_id' => $target_id); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
return $return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get entity type. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
* Entity type (node, user, taxonomy, etc). |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getEntityType() { |
|
48
|
|
|
return $this->fieldInfo['settings']['target_type']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Check target id belongs to a real entity. |
|
53
|
|
|
* |
|
54
|
|
|
* @param int $target_id |
|
55
|
|
|
* Target id. |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
* Entity exists? |
|
59
|
|
|
*/ |
|
60
|
|
|
public function entityExists($target_id) { |
|
61
|
|
|
$entity_type = $this->getEntityType(); |
|
62
|
|
|
$entity = entity_load_single($entity_type, $target_id); |
|
63
|
|
|
return !empty($entity); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|