TaxonomyTermReferenceHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A expand() 0 15 3
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
/**
6
 * Field handler for taxonomy term references in Drupal 8.
7
 */
8
class TaxonomyTermReferenceHandler extends AbstractHandler {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function expand($values) {
14
    $return = [];
15
    foreach ($values as $name) {
16
      $terms = \Drupal::entityTypeManager()
17
        ->getStorage('taxonomy_term')
18
        ->loadByProperties(['name' => $name]);
19
      if ($terms) {
20
        $return[] = array_shift($terms)->id();
21
      }
22
      else {
23
        throw new \Exception(sprintf("No term '%s' exists.", $name));
24
      }
25
    }
26
    return $return;
27
  }
28
29
}
30