ListTextHandler::expand()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 16
nop 1
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal7;
4
5
/**
6
 * ListText field handler for Drupal 7.
7
 */
8
class ListTextHandler extends AbstractHandler {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function expand($values) {
14
    $return = [];
15
    $allowed_values = [];
16
    if (!empty($this->fieldInfo['settings']['allowed_values_function'])) {
17
      $cacheable = TRUE;
18
      $callback = $this->fieldInfo['settings']['allowed_values_function'];
19
      $options = call_user_func($callback, $this->fieldInfo, $this, $this->entityType, $this->entity, $cacheable);
20
    }
21
    else {
22
      $options = $this->fieldInfo['settings']['allowed_values'];
23
    }
24
    foreach ($values as $value) {
25
      if (array_key_exists($value, $options)) {
26
        $allowed_values[$value] = $value;
27
      }
28
      elseif (in_array($value, $options)) {
29
        $key = array_search($value, $options);
30
        $allowed_values[$value] = $key;
31
      }
32
    }
33
    foreach ($values as $value) {
34
      $return[$this->language][] = ['value' => $allowed_values[$value]];
35
    }
36
    return $return;
37
  }
38
39
}
40