|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_twig\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Controller\ControllerBase; |
|
6
|
|
|
use Drupal\Core\Template\TwigEnvironment; |
|
7
|
|
|
use Drupal\graphql\GraphQL\Execution\QueryProcessor; |
|
8
|
|
|
use GraphQL\Server\OperationParams; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Controller for configuration-generated GraphQL-Twig routes. |
|
13
|
|
|
*/ |
|
14
|
|
|
class RouteController extends ControllerBase { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Drupal\graphql\GraphQL\Execution\QueryProcessor |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $queryProcessor; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Drupal\Core\Template\TwigEnvironment |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $twig; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function create(ContainerInterface $container) { |
|
30
|
|
|
return new static( |
|
31
|
|
|
$container->get('graphql.query_processor'), |
|
32
|
|
|
$container->get('twig') |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* RouteController constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Drupal\graphql\GraphQL\Execution\QueryProcessor $processor |
|
40
|
|
|
* A GraphQL query processor. |
|
41
|
|
|
* @param \Drupal\Core\Template\TwigEnvironment $twig |
|
42
|
|
|
* A Twig environment. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(QueryProcessor $processor, TwigEnvironment $twig) { |
|
45
|
|
|
$this->queryProcessor = $processor; |
|
46
|
|
|
$this->twig = $twig; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Generic page callback. |
|
51
|
|
|
* |
|
52
|
|
|
* Accepts a theme hook and an array of theme variables. |
|
53
|
|
|
* |
|
54
|
|
|
* @param $_graphql_theme_hook |
|
55
|
|
|
* The theme hook. |
|
56
|
|
|
* @param $_graphql_arguments |
|
57
|
|
|
* Query arguments |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
* The render array build. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function page($_graphql_theme_hook, $_graphql_arguments) { |
|
63
|
|
|
return [ |
|
64
|
|
|
'#theme' => $_graphql_theme_hook, |
|
65
|
|
|
'#graphql_arguments' => $_graphql_arguments, |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Build a page title from a twig template and a GraphQL query. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $_graphql_title |
|
73
|
|
|
* @param $_graphql_title_query |
|
74
|
|
|
* @param $_graphql_arguments |
|
75
|
|
|
* |
|
76
|
|
|
* @return \Drupal\Component\Render\MarkupInterface|\Drupal\Core\StringTranslation\TranslatableMarkup|string |
|
77
|
|
|
* @throws \Drupal\Component\Plugin\Exception\PluginException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function title($_graphql_title, $_graphql_title_query, $_graphql_arguments) { |
|
80
|
|
|
if ($_graphql_title_query) { |
|
81
|
|
|
$result = $this->queryProcessor->processQuery('default:default', |
|
82
|
|
|
OperationParams::create([ |
|
83
|
|
|
'query' => $_graphql_title_query, |
|
84
|
|
|
'variables' => $_graphql_arguments, |
|
85
|
|
|
]) |
|
86
|
|
|
); |
|
87
|
|
|
$_graphql_title = $this->twig->renderInline($_graphql_title, $result->data); |
|
88
|
|
|
} |
|
89
|
|
|
else { |
|
90
|
|
|
$_graphql_title = $this->t($_graphql_title); |
|
91
|
|
|
} |
|
92
|
|
|
return $_graphql_title; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|