|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Breadcrumbs; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Breadcrumb\BreadcrumbManager; |
|
6
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
7
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
|
8
|
|
|
use Drupal\Core\Url; |
|
9
|
|
|
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer; |
|
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
12
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Retrieve the breadcrumbs. |
|
16
|
|
|
* |
|
17
|
|
|
* TODO: Move this to `InternalUrl` (breaking change). |
|
18
|
|
|
* |
|
19
|
|
|
* @GraphQLField( |
|
20
|
|
|
* id = "breadcrumb", |
|
21
|
|
|
* secure = true, |
|
22
|
|
|
* name = "breadcrumb", |
|
23
|
|
|
* type = "Link", |
|
24
|
|
|
* multi = true, |
|
25
|
|
|
* parents = {"Url"}, |
|
26
|
|
|
* ) |
|
27
|
|
|
*/ |
|
28
|
|
|
class Breadcrumbs extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The subrequest buffer service. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $subRequestBuffer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The breadcrumb manager service. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $breadcrumbManager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The current route match. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \Drupal\Core\Routing\RouteMatchInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $routeMatch; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
55
|
|
|
return new static( |
|
56
|
|
|
$configuration, |
|
57
|
|
|
$pluginId, |
|
58
|
|
|
$pluginDefinition, |
|
59
|
|
|
$container->get('graphql.buffer.subrequest'), |
|
60
|
|
|
$container->get('breadcrumb'), |
|
61
|
|
|
$container->get('current_route_match') |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct( |
|
69
|
|
|
array $configuration, |
|
70
|
|
|
$pluginId, |
|
71
|
|
|
$pluginDefinition, |
|
72
|
|
|
SubRequestBuffer $subRequestBuffer, |
|
73
|
|
|
BreadcrumbManager $breadcrumbManager, |
|
74
|
|
|
RouteMatchInterface $routeMatch |
|
75
|
|
|
) { |
|
76
|
|
|
$this->subRequestBuffer = $subRequestBuffer; |
|
77
|
|
|
$this->breadcrumbManager = $breadcrumbManager; |
|
|
|
|
|
|
78
|
|
|
$this->routeMatch = $routeMatch; |
|
79
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
86
|
|
|
if ($value instanceof Url) { |
|
|
|
|
|
|
87
|
|
|
$resolve = $this->subRequestBuffer->add($value, function () { |
|
88
|
|
|
$links = $this->breadcrumbManager->build($this->routeMatch)->getLinks(); |
|
89
|
|
|
return $links; |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
return function ($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
|
|
93
|
|
|
$links = $resolve(); |
|
94
|
|
|
foreach ($links as $link) { |
|
95
|
|
|
yield $link; |
|
96
|
|
|
} |
|
97
|
|
|
}; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|