1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Breadcrumbs; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Breadcrumb\BreadcrumbManager; |
6
|
|
|
use Drupal\Core\Routing\RouteMatchInterface; |
7
|
|
|
use Drupal\graphql\GraphQL\Batching\BatchedFieldResolver; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\SubrequestFieldBase; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
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 SubrequestFieldBase { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The breadcrumb manager. |
32
|
|
|
* |
33
|
|
|
* @var \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface |
34
|
|
|
*/ |
35
|
|
|
protected $breadcrumbManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The current route match. |
39
|
|
|
* |
40
|
|
|
* @var \Drupal\Core\Routing\RouteMatchInterface |
41
|
|
|
*/ |
42
|
|
|
protected $routeMatch; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The request stack. |
46
|
|
|
* |
47
|
|
|
* @var \Symfony\Component\HttpFoundation\RequestStack |
48
|
|
|
*/ |
49
|
|
|
protected $requestStack; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* A http kernel to issue sub-requests to. |
53
|
|
|
* |
54
|
|
|
* @var \Symfony\Component\HttpKernel\HttpKernelInterface |
55
|
|
|
*/ |
56
|
|
|
protected $httpKernel; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
|
|
|
63
|
|
|
return new static( |
64
|
|
|
$configuration, |
65
|
|
|
$pluginId, |
66
|
|
|
$pluginDefinition, |
67
|
|
|
$container->get('http_kernel'), |
68
|
|
|
$container->get('request_stack'), |
69
|
|
|
$container->get('graphql.batched_resolver'), |
70
|
|
|
$container->get('breadcrumb'), |
71
|
|
|
$container->get('current_route_match') |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
array $configuration, |
80
|
|
|
$pluginId, |
81
|
|
|
$pluginDefinition, |
82
|
|
|
HttpKernelInterface $httpKernel, |
83
|
|
|
RequestStack $requestStack, |
84
|
|
|
BatchedFieldResolver $batchedFieldResolver, |
85
|
|
|
BreadcrumbManager $breadcrumbManager, |
86
|
|
|
RouteMatchInterface $routeMatch |
87
|
|
|
) { |
88
|
|
|
$this->breadcrumbManager = $breadcrumbManager; |
|
|
|
|
89
|
|
|
$this->routeMatch = $routeMatch; |
90
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition, $httpKernel, $requestStack, $batchedFieldResolver); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
protected function resolveSubrequest($value, array $args, ResolveInfo $info) { |
97
|
|
|
return $this->breadcrumbManager->build($this->routeMatch)->getLinks(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.