FileHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 40
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A expand() 0 31 3
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal7;
4
5
/**
6
 * File field handler for Drupal 7.
7
 */
8
class FileHandler extends AbstractHandler {
9
10
  /**
11
   * {@inheritdoc}
12
   *
13
   * Specify files in file fields by their filename.
14
   */
15
  public function expand($values) {
16
    $return = [];
17
18
    foreach ($values as $value) {
19
      $query = new \EntityFieldQuery();
20
21
      $query->entityCondition('entity_type', 'file')
22
        ->propertyCondition('filename', $value)
23
        ->propertyOrderBy('timestamp', 'DESC')
24
        ->range(0, 1);
25
26
      $result = $query->execute();
27
28
      if (!empty($result['file'])) {
29
        $files = entity_load('file', array_keys($result['file']));
30
        $file = current($files);
31
32
        $return[$this->language][] = [
33
          'filename' => $file->filename,
34
          'uri' => $file->uri,
35
          'fid' => $file->fid,
36
          'display' => 1,
37
        ];
38
      }
39
      else {
40
        throw new \Exception(sprintf('File with filename "%s" not found.', $value));
41
      }
42
    }
43
44
    return $return;
45
  }
46
47
}
48