-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathavoindata_hero.module
More file actions
113 lines (101 loc) · 2.81 KB
/
Copy pathavoindata_hero.module
File metadata and controls
113 lines (101 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @file
* Adds avoindata hero module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Implements hook_help().
*/
function avoindata_hero_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.avoindata_hero':
return t('
Creates header that can be exported to non-Drupal pages.
');
}
}
/**
* Implements hook_theme().
*/
function avoindata_hero_theme($existing, $type, $theme, $path) {
return [
'avoindata_hero' => [
'render element' => 'form',
'template' => 'avoindata_hero_form',
],
];
}
class AvoindataHeroHandler implements TrustedCallbackInterface
{
use StringTranslationTrait;
/**
* Queries statistics from ckan.
*
* @return array
* Returns markup.
*/
function avoindata_hero_ckan_statistics()
{
$client = \Drupal::httpClient();
try {
$response = $client->request('GET', getenv('DRUPAL_CKAN_HOST') . '/data/api/3/action/statistics');
$result = Json::decode($response->getBody());
$ckan_statistics = $result['result'];
} catch (\Exception $e) {
$ckan_statistics = [
'datasets' => '0',
'apisets' => '0',
'organizations' => '0',
'showcases' => '0'
];
}
$statistics = array(
[
'count' => $ckan_statistics['datasets'],
'url' => 'dataset',
'text' => $this->t('Datasets', [], ['context' => 'x datasets'] )
],
[
'count' => $ckan_statistics['apisets'],
'url' => 'apiset',
'text' => $this->t('Apisets', [], ['context' => 'x apisets'])
],
[
'count' => $ckan_statistics['organizations'],
'url' => 'organization',
'text' => $this->t('Organizations', [], ['context' => 'x organizations'])
],
[
'count' => $ckan_statistics['showcases'],
'url' => 'showcase',
'text' => $this->t('Applications', [], ['context' => 'x applications'])
]
);
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$statistics_markup = '';
foreach ($statistics as $statistic) {
$statistics_markup .= <<<EOT
<div class="text-center hero-stats">
<a href="/data/{$language}/{$statistic['url']}">
<div class="hero-count">{$statistic['count']}</div>
<div class="hero-text">{$statistic['text']}</div>
</a>
</div>
EOT;
}
return [
'#markup' => $statistics_markup,
'#cache' => ['max-age' => 0],
];
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['avoindata_hero_ckan_statistics'];
}
}