1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_articles\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
|
|
|
* CollectionNodeEndpointBlock constructor. |
30
|
|
|
* @param array $configuration |
31
|
|
|
* @param string $plugin_id |
32
|
|
|
* @param mixed $plugin_definition |
33
|
|
|
* @param \GuzzleHttp\Client $http_client |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, Client $http_client) { |
36
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
37
|
|
|
$this->httpClient = $http_client; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
44
|
|
|
return new static( |
45
|
|
|
$configuration, |
46
|
|
|
$plugin_id, |
47
|
|
|
$plugin_definition, |
48
|
|
|
$container->get('http_client') |
|
|
|
|
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function build() { |
56
|
|
|
$build = array(); |
57
|
|
|
global $base_url; |
58
|
|
|
header('Content-Type: application/json'); |
59
|
|
|
$json_output = (string) $this->httpClient->get($base_url . '/api/node/article')->getBody(); |
60
|
|
|
$json_pretty = json_encode(json_decode($json_output), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
61
|
|
|
$json_indented_by_2 = preg_replace('/^( +?)\\1(?=[^ ])/m', '$1', $json_pretty); |
62
|
|
|
$build['article_node_endpoint_block']['#markup'] = '<a data-toggle="language-json" class="button small">Expand Code</a> <div id="language-json" class="api-demo" data-toggler=".expanded"><pre><code class="language-json">' . $json_indented_by_2 . '</code></pre></div>'; |
63
|
|
|
$build['article_node_endpoint_block']['#attached']['library'][] = 'df_tools_articles/main'; |
64
|
|
|
return $build; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|