EntityreferenceHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A expand() 0 20 4
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 = [];
22
    foreach ($values as $value) {
23
      $target_id = db_select($entity_info['base table'], 't')
24
        ->fields('t', [$entity_info['entity keys']['id']])
25
        ->condition('t.' . $entity_info['entity keys']['label'], $value)
26
        ->execute()->fetchField();
27
      if ($target_id) {
28
        $return[$this->language][] = ['target_id' => $target_id];
29
      }
30
    }
31
    return $return;
32
  }
33
34
}
35