Completed
Pull Request — master (#96)
by
unknown
02:05
created

LinkHandler::expand()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.439
cc 6
eloc 19
nc 7
nop 1
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
/**
6
 * Link field handler for Drupal 8.
7
 */
8
class LinkHandler extends AbstractHandler {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function expand($values) {
14
    $return = array();
15
    foreach ($values as $value) {
16
      $uri = $value[1];
17
18
      // If the uri has no scheme (and is not protocol relative) attempt to find
19
      // a node with that label.
20
      if (empty($uri_parts['scheme']) && strpos($uri, '//') !== 0) {
21
        $entity_type_id = 'node';
22
        $query = \Drupal::entityQuery($entity_type_id)->condition('title', $uri);
23
        $entities = $query->execute();
24
        if (!empty($entities)) {
25
          $nid = array_shift($entities);
26
          $uri = 'entity:' . $entity_type_id . '/' . $nid;
27
        }
28
      }
29
30
      // 'options' is required to be an array, otherwise the utility class
31
      // Drupal\Core\Utility\UnroutedUrlAssembler::assemble() will complain.
32
      $options = array();
33
      if (!empty($value[2])) {
34
        parse_str($value[2], $options);
35
      }
36
37
      $return[] = array(
38
        'options' => $options,
39
        'title' => $value[0],
40
        'uri' => $uri,
41
      );
42
    }
43
44
    return $return;
45
  }
46
47
}
48