TaxonomyHandler::expand()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 8
nop 1
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal6;
4
5
use Drupal\Driver\Fields\FieldHandlerInterface;
6
7
/**
8
 * Provides a custom field handler to make it easier to include taxonomy terms.
9
 */
10
class TaxonomyHandler implements FieldHandlerInterface {
11
12
  /**
13
   * {@inheritdoc}
14
   */
15
  public function expand($values) {
16
    $result = [];
17
    $values = (array) $values;
18
    foreach ($values as $entry) {
19
      $terms = explode(',', $entry);
20
      foreach ($terms as $term) {
21
        // Try to split things out in order to find optional specified vocabs.
22
        $term_name_or_tid = '';
23
        $parts = explode(':', $term);
24
        if (count($parts) == 1) {
25
          $term_name_or_tid = $term;
26
        }
27
        elseif (count($parts) == 2) {
28
          $term_name_or_tid = $term;
29
        }
30
        if ($term_list = taxonomy_get_term_by_name($term_name_or_tid)) {
31
          $term = reset($term_list);
32
          $result[] = $term;
33
        }
34
      }
35
    }
36
37
    return $result;
38
  }
39
40
}
41