TaxonomyTermReferenceHandler::expand()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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