Completed
Push — master ( e0204a...44622c )
by Jonathan
11s
created

ListHandlerBase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A expand() 0 16 4
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
/**
6
 * Base class for List* field types.
7
 *
8
 * This allows use of allowed value labels rather than their storage value.
9
 */
10
abstract class ListHandlerBase extends AbstractHandler {
11
12
  /**
13
   * {@inheritdoc}
14
   */
15
  public function expand($values) {
16
    $return = [];
17
18
    // Load allowed values from field storage.
19
    $allowed_values = $this->fieldInfo->getSetting('allowed_values');
20
    foreach ((array) $values as $value) {
21
      // Determine if a label matching the value is found.
22
      $key = array_search($value, $allowed_values);
23
      if ($key !== FALSE) {
24
        // Set the return to use the key instead of the value.
25
        $return[] = $key;
26
      }
27
    }
28
29
    return $return ?: $values;
30
  }
31
32
}
33