EntityreferenceHandler::expand()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 6
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 = [];
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