-
Notifications
You must be signed in to change notification settings - Fork 0
/
onovas_web_speech.module
executable file
·158 lines (139 loc) · 4.36 KB
/
onovas_web_speech.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @file
* onovas_web_speech.module.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\onovas_web_speech\lib\general\MarkdownParser;
/**
* Implements hook_help().
*/
function onovas_web_speech_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.onovas_web_speech':
/* Añado el contenido del archivo README.md a la ayuda del módulo */
$parser = new MarkdownParser();
$module_path = \Drupal::service('extension.path.resolver')
->getPath('module', "onovas_web_speech");
$readme_ruta = $module_path . "/README.md";
$contenido = '';
if (file_exists($readme_ruta)) {
$contenido = file_get_contents($readme_ruta);
$contenido = Markup::create($parser->text($contenido));
if ($contenido) {
$template_path = $module_path . "/templates/custom/help.html.twig";
$template = file_get_contents($template_path);
$build = [
'description' => [
'#type' => 'inline_template',
'#template' => $template,
'#context' => [
'readme' => $contenido,
],
],
];
return $build;
}
}
default:
}
}
/**
* Implements hook_modules_installed().
*/
function onovas_web_speech_modules_installed($modules) {
if (in_array('onovas_web_speech', $modules)) {
// Be friendly to your users: what to do after install?
$url = Url::fromRoute('onovas_web_speech.settings');
if (PHP_SAPI != 'cli') {
\Drupal::messenger()->addMessage(t('You can now <a href="@url_settings">configure the onovas: Web Speech module</a> for your site.',
['@url_settings' => $url->toString()]), 'status');
}
}
}
/**
* Implements hook_toolbar_alter().
*/
function onovas_web_speech_toolbar_alter(&$items) {
/* Añado iconos a la toolbar de administración */
$items['administration']['#attached']['library'][] = 'onovas_web_speech/toolbar';
}
/**
* Implements hook_page_attachments().
*/
function onovas_web_speech_page_attachments(array &$attachments) {
/* Añadir el CSS y JS a toda la web */
$attachments['#attached']['library'][] = 'onovas_web_speech/global_libraries';
}
/**
* Implements hook_theme().
*/
function onovas_web_speech_theme() {
return [
'onovas_web_speech' => [
'variables' => [],
],
'onovas_web_speech_block' => [
'variables' => [],
],
];
}
/**
* Implements hook_entity_extra_field_info().
*/
function onovas_web_speech_entity_extra_field_info() {
$extra = [];
$config = \Drupal::config('onovas_web_speech.settings');
$content_types = $config->get('content_types');
foreach ($content_types as $content_type) {
if ($content_type) {
$extra['node'][$content_type]['display']['node_onovas_web_speech'] = [
'label' => t('Web Speech'),
'description' => t('Show web speech'),
'weight' => 5,
'visible' => FALSE,
];
}
}
return $extra;
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function onovas_web_speech_entity_view(array &$build,
EntityInterface $entity,
EntityViewDisplayInterface $display,
$view_mode) {
// Prevent error on preview of an unpublished node.
if ($entity->id() === NULL) {
return;
}
if ($display->getComponent('node_onovas_web_speech')) {
$config = \Drupal::config('onovas_web_speech.settings');
$content_types = $config->get('content_types');
/* TODO Poner container como configurable */
$isAllowed = in_array($entity->bundle(), $content_types);
if ($isAllowed) {
$build['node_onovas_web_speech'] = [
'#theme' => 'onovas_web_speech',
'#attached' => [
'library' => [
'onovas_web_speech/speech',
],
'drupalSettings' => [
'onovas_web_speech' => [
'voice' => $config->get('voice'),
'pitch' => $config->get('pitch'),
'rate' => $config->get('rate'),
'container' => $config->get('container'),
],
],
],
];
}
}
}