TaxonomyTermReferenceHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A expand() 0 11 3
A getVocab() 0 5 2
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal7;
4
5
/**
6
 * Taxonomy term reference field handler for Drupal 7.
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 = taxonomy_get_term_by_name($name, $this->getVocab());
17
      if (!$terms) {
18
        throw new \Exception(sprintf("No term '%s' exists.", $name));
19
      }
20
      $return[$this->language][] = ['tid' => array_shift($terms)->tid];
21
    }
22
    return $return;
23
  }
24
25
  /**
26
   * Attempt to determine the vocabulary for which the field is configured.
27
   *
28
   * @return mixed
29
   *   Returns a string containing the vocabulary in which the term must be
30
   *   found or NULL if unable to determine.
31
   */
32
  protected function getVocab() {
33
    if (!empty($this->fieldInfo['settings']['allowed_values'][0]['vocabulary'])) {
34
      return $this->fieldInfo['settings']['allowed_values'][0]['vocabulary'];
35
    }
36
  }
37
38
}
39