1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_twig\Plugin\Block; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Annotation\Translation; |
6
|
|
|
use Drupal\Core\Block\Annotation\Block; |
7
|
|
|
use Drupal\Core\Block\BlockBase; |
8
|
|
|
use Drupal\Core\Form\FormStateInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class GraphQLTwigBlock |
12
|
|
|
* |
13
|
|
|
* @Block( |
14
|
|
|
* id="graphql_twig", |
15
|
|
|
* category=@Translation("GraphQL Twig"), |
16
|
|
|
* deriver="\Drupal\graphql_twig\Plugin\Deriver\GraphQLTwigBlockDeriver" |
17
|
|
|
* ) |
18
|
|
|
*/ |
19
|
|
|
class GraphQLTwigBlock extends BlockBase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function blockForm($form, FormStateInterface $form_state) { |
25
|
|
|
$form = parent::blockForm($form, $form_state); |
26
|
|
|
|
27
|
|
|
$arguments = []; |
28
|
|
|
|
29
|
|
|
if (isset($this->configuration['graphql_block'])) { |
30
|
|
|
foreach ($this->configuration['graphql_block'] as $arg) { |
31
|
|
|
$arguments[$arg['key']] = $arg['value']; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach ($this->pluginDefinition['graphql_parameters'] as $name => $el) { |
36
|
|
|
foreach ($el as $key => $value) { |
37
|
|
|
if (in_array($key, ['title', 'description'])) { |
38
|
|
|
$form['graphql_block'][$name]['#' . $key] = $this->t($value); |
39
|
|
|
} |
40
|
|
|
else { |
41
|
|
|
$form['graphql_block'][$name]['#' . $key] = $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$form['graphql_block'][$name]['#default_value'] = isset($arguments[$name]) ? $arguments[$name] : ''; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $form; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function blockSubmit($form, FormStateInterface $form_state) { |
55
|
|
|
parent::blockSubmit($form, $form_state); |
56
|
|
|
|
57
|
|
|
foreach (array_keys($this->pluginDefinition['graphql_parameters']) as $arg) { |
58
|
|
|
$values = $form_state->getValues(); |
59
|
|
|
$this->configuration['graphql_block'][] = [ |
60
|
|
|
'key' => $arg, |
61
|
|
|
'value' => $values['graphql_block'][$arg], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function build() { |
70
|
|
|
$arguments = []; |
71
|
|
|
|
72
|
|
|
foreach ($this->configuration['graphql_block'] as $arg) { |
73
|
|
|
$arguments[$arg['key']] = $arg['value']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return [ |
77
|
|
|
'#theme' => $this->pluginDefinition['graphql_theme_hook'], |
78
|
|
|
'#graphql_arguments' => $arguments, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|