1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_article\Plugin\Block; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Block\BlockBase; |
6
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides a 'ArticleNodeEndpointBlock' block. |
12
|
|
|
* |
13
|
|
|
* @Block( |
14
|
|
|
* id = "article_node_endpoint_block", |
15
|
|
|
* admin_label = @Translation("Article node endpoint block"), |
16
|
|
|
* category = "Lists", |
17
|
|
|
* ) |
18
|
|
|
*/ |
19
|
|
|
class ArticleNodeEndpointBlock extends BlockBase implements ContainerFactoryPluginInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* GuzzleHttp\Client definition. |
23
|
|
|
* |
24
|
|
|
* @var \GuzzleHttp\Client |
25
|
|
|
*/ |
26
|
|
|
protected $httpClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructs a new ArticleNodeEndpointBlock instance. |
30
|
|
|
* |
31
|
|
|
* @param array $configuration |
32
|
|
|
* A configuration array containing information about the plugin instance. |
33
|
|
|
* @param string $plugin_id |
34
|
|
|
* The plugin ID for the plugin instance. |
35
|
|
|
* @param mixed $plugin_definition |
36
|
|
|
* The plugin implementation definition. |
37
|
|
|
* @param \GuzzleHttp\Client $http_client |
38
|
|
|
* The HTTP client service. |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, Client $http_client) { |
41
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
42
|
|
|
$this->httpClient = $http_client; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
49
|
|
|
return new static( |
50
|
|
|
$configuration, |
51
|
|
|
$plugin_id, |
52
|
|
|
$plugin_definition, |
53
|
|
|
$container->get('http_client') |
|
|
|
|
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function build() { |
61
|
|
|
$build = []; |
62
|
|
|
global $base_url; |
63
|
|
|
|
64
|
|
|
header('Content-Type: application/json'); |
65
|
|
|
$json_output = (string) $this->httpClient->get($base_url . '/api/node/article')->getBody(); |
66
|
|
|
$json_pretty = json_encode(json_decode($json_output), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
67
|
|
|
$json_indented_by_2 = preg_replace('/^( +?)\\1(?=[^ ])/m', '$1', $json_pretty); |
68
|
|
|
|
69
|
|
|
$build['article_node_endpoint_block'] = [ |
70
|
|
|
'#markup' => '<div id="api-demo"> |
71
|
|
|
<button class="btn btn-primary button button--primary coh-style-link-button |
72
|
|
|
coh-style-link-button-color open-apiModal">Expand API Response</button> |
73
|
|
|
<div class="apiResponse"> |
74
|
|
|
<pre><code class="language-json">' . $json_indented_by_2 . '</code></pre> |
75
|
|
|
</div> |
76
|
|
|
<div class="apiResponseModal"> |
77
|
|
|
<pre><code class="language-json">' . $json_indented_by_2 . '</code></pre> |
78
|
|
|
</div> |
79
|
|
|
</div>', |
80
|
|
|
'#attached' => ['library' => ['df_tools_article/main']], |
81
|
|
|
'#allowed_tags' => ['button', 'code', 'div', 'pre'], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
return $build; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|