-
Notifications
You must be signed in to change notification settings - Fork 14
/
ethereum.module
133 lines (119 loc) · 4.02 KB
/
ethereum.module
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @file
* Contains ethereum.module.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\ethereum\Entity\EthereumServer;
use League\CommonMark\CommonMarkConverter;
/**
* Implements hook_help().
*
* @inheritdoc
*/
function ethereum_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the ethereum_user_connector module.
case 'help.page.ethereum':
return ethereum_get_readme_html('ethereum');
default:
return '';
}
}
/**
* Implements hook_help().
*/
function ethereum_get_readme_html($module) {
$path = drupal_get_path('module', $module) . '/Readme.md';
$readme = @file_get_contents(DRUPAL_ROOT . '/'. $path);
if($readme === FALSE) {
return "<div class='messages messages--warning'>Missing Readme.md for $path.</div>";
}
else {
$converter = new CommonMarkConverter();
return $converter->convertToHtml($readme);
}
}
/**
* Implements hook_field_formatter_info_alter().
*/
function ethereum_field_formatter_info_alter(array &$info) {
// Allow the 'Ethereum address' field type to use the existing 'Basic string'
// formatter.
$info['basic_string']['field_types'][] = 'ethereum_address';
}
/**
* Implements hook_field_views_data().
*/
function ethereum_field_views_data(\Drupal\field\FieldStorageConfigInterface $field_storage) {
$data = views_field_default_views_data($field_storage);
foreach ($data as $table_name => $table_data) {
// Add the relationship only on the target_id field.
$data[$table_name][$field_storage->getName() . '_network']['filter']['id'] = 'in_operator';
$data[$table_name][$field_storage->getName() . '_network']['filter']['options callback'] = '_ethereum_get_networks_options';
}
return $data;
}
/**
* Returns the Ethereum networks as an array suitable for an options element.
*
* @return array
* An array of Ethereum Networks containing the network name, ID, and
* description, keyed by the network ID.
*/
function _ethereum_get_networks_options() {
return \Drupal::service('ethereum.manager')->getNetworksAsOptions();
}
/**
* Implements hook_js_settings_alter().
*
* @param $settings
* @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
*/
function ethereum_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
// Provide the URL of the current server in drupalSettings to allow JavaScript
// code to rely on an instantiated Web3 object.
$cfg = \Drupal::config('ethereum.settings');
if ($cfg->get('frontend_server') !== 'disabled') {
$frontend_server = $cfg->get('frontend_server') ? $cfg->get('frontend_server') : $cfg->get('current_server');
$settings['ethereum']['frontendServerHost'] = EthereumServer::load($frontend_server)->getUrl();
}
}
/**
* Implements hook_toolbar().
*/
function ethereum_toolbar() {
/** @var \Drupal\ethereum\EthereumManagerInterface $ethereum_manager */
$ethereum_manager = \Drupal::service('ethereum.manager');
$current_server = $ethereum_manager->getCurrentServer();
$networks = $ethereum_manager->getAllNetworks();
$items['ethereum'] = [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'link',
'#title' => t('Network: <span class="network-label">@network_label</span>', ['@network_label' => $networks[$current_server->getNetworkId()]['label']]),
'#url' => Url::fromRoute('ethereum.status'),
'#attributes' => [
'title' => t('Current Ethereum network'),
'class' => ['toolbar-icon', 'toolbar-icon-ethereum'],
],
],
'#wrapper_attributes' => [
'class' => ['ethereum-toolbar-tab'],
],
'#weight' => 1500,
'#attached' => [
'library' => [
'ethereum/ethereum-toolbar',
],
],
];
// Add a special class to the wrapper if we are on the main network so we
// can highlight it with a different color.
if ($current_server->getNetworkId() === '1') {
$items['ethereum']['#wrapper_attributes']['class'][] = 'ethereum-toolbar-tab--is-mainnet';
}
return $items;
}