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
|
|
|
foreach ($this->pluginDefinition['graphql_parameters'] as $name => $el) { |
28
|
|
|
foreach ($el as $key => $value) { |
29
|
|
|
if (in_array($key, ['title', 'description'])) { |
30
|
|
|
$form['graphql_block'][$name]['#' . $key] = $this->t($value); |
31
|
|
|
} |
32
|
|
|
else { |
33
|
|
|
$form['graphql_block'][$name]['#' . $key] = $value; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $form; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function blockSubmit($form, FormStateInterface $form_state) { |
45
|
|
|
parent::blockSubmit($form, $form_state); |
46
|
|
|
|
47
|
|
View Code Duplication |
foreach (array_keys($this->pluginDefinition['graphql_parameters']) as $arg) { |
|
|
|
|
48
|
|
|
$values = $form_state->getValues(); |
49
|
|
|
$this->configuration['graphql_block'][$arg] = $values['graphql_block'][$arg]; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function build() { |
57
|
|
|
$arguments = []; |
58
|
|
|
|
59
|
|
View Code Duplication |
foreach (array_keys($this->pluginDefinition['graphql_parameters']) as $arg) { |
|
|
|
|
60
|
|
|
$arguments[$arg] = $this->configuration['graphql_block'][$arg]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return [ |
64
|
|
|
'#theme' => $this->pluginDefinition['graphql_theme_hook'], |
65
|
|
|
'#graphql_arguments' => $arguments, |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
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.