Completed
Pull Request — master (#84)
by
unknown
02:04
created

EntityreferenceHandler::expand()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.439
cc 5
eloc 17
nc 10
nop 1
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->fieldInfo['settings']['target_type'];
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
      $query = db_select($entity_info['base table'], 't')
24
        ->fields('t', array($entity_info['entity keys']['id']));
25
      if(is_numeric($value)){
26
        $query->condition('t.' . $entity_info['entity keys']['id'], $value);
27
      }
28
      else{
29
        $query->condition('t.' . $entity_info['entity keys']['label'], $value);
30
      }
31
      $target_id = $query->execute()->fetchField();
32
      if ($target_id) {
33
        $return[$this->language][] = array('target_id' => $target_id);
34
      }
35
    }
36
    return $return;
37
  }
38
39
}
40