1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Internationalization (i18n) submodule: Multilingual meta-blocks
|
6
|
*
|
7
|
* @author Jose A. Reyero, 2005
|
8
|
*
|
9
|
* @ TODO Add strings on block update.
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Implements hook_menu().
|
14
|
*
|
15
|
* Add translate tab to blocks.
|
16
|
*/
|
17
|
function i18n_block_menu() {
|
18
|
$items['admin/structure/block/manage/%/%/translate'] = array(
|
19
|
'title' => 'Translate',
|
20
|
'access callback' => 'i18n_block_translate_tab_access',
|
21
|
'access arguments' => array(4, 5),
|
22
|
'page callback' => 'i18n_block_translate_tab_page',
|
23
|
'page arguments' => array(4, 5),
|
24
|
'type' => MENU_LOCAL_TASK,
|
25
|
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
|
26
|
'weight' => 10,
|
27
|
);
|
28
|
$items['admin/structure/block/manage/%/%/translate/%i18n_language'] = array(
|
29
|
'title' => 'Translate',
|
30
|
'access callback' => 'i18n_block_translate_tab_access',
|
31
|
'access arguments' => array(4, 5),
|
32
|
'page callback' => 'i18n_block_translate_tab_page',
|
33
|
'page arguments' => array(4, 5, 7),
|
34
|
'type' => MENU_CALLBACK,
|
35
|
'weight' => 10,
|
36
|
);
|
37
|
return $items;
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Implement hook_menu_alter().
|
42
|
*
|
43
|
* Reorganize block tabs so that they make sense.
|
44
|
*/
|
45
|
function i18n_block_menu_alter(&$items) {
|
46
|
// Give the configure tab a short name and make it display.
|
47
|
$items['admin/structure/block/manage/%/%/configure']['weight'] = -100;
|
48
|
$items['admin/structure/block/manage/%/%/configure']['title'] = 'Configure';
|
49
|
$items['admin/structure/block/manage/%/%/configure']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
|
50
|
// Hide the delete tab. Not sure why this was even set a local task then
|
51
|
// set to not show in any context...
|
52
|
$items['admin/structure/block/manage/%/%/delete']['type'] = MENU_CALLBACK;
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* Menu access callback function.
|
57
|
*
|
58
|
* Only let blocks translated which are configured to be translatable.
|
59
|
*/
|
60
|
function i18n_block_translate_tab_access($module, $delta) {
|
61
|
$block = block_load($module, $delta);
|
62
|
return user_access('translate interface') && $block && isset($block->i18n_mode) && ($block->i18n_mode == I18N_MODE_LOCALIZE);
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Build a translation page for the given block.
|
67
|
*/
|
68
|
function i18n_block_translate_tab_page($module, $delta, $language = NULL) {
|
69
|
$block = block_load($module, $delta);
|
70
|
return i18n_string_object_translate_page('block', $block, $language);
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Implements hook_block_list_alter().
|
75
|
*
|
76
|
* Translate localizable blocks.
|
77
|
*
|
78
|
* To be run after all block visibility modules have run, just translate the blocks to be displayed
|
79
|
*/
|
80
|
function i18n_block_block_list_alter(&$blocks) {
|
81
|
global $theme_key, $language;
|
82
|
|
83
|
// Build an array of node types for each block.
|
84
|
$block_languages = array();
|
85
|
$result = db_query('SELECT module, delta, language FROM {i18n_block_language}');
|
86
|
foreach ($result as $record) {
|
87
|
$block_languages[$record->module][$record->delta][$record->language] = TRUE;
|
88
|
}
|
89
|
|
90
|
foreach ($blocks as $key => $block) {
|
91
|
if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
|
92
|
// This block was added by a contrib module, leave it in the list.
|
93
|
continue;
|
94
|
}
|
95
|
if (isset($block_languages[$block->module][$block->delta]) && !isset($block_languages[$block->module][$block->delta][$language->language])) {
|
96
|
// Block not visible for this language
|
97
|
unset($blocks[$key]);
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Implements hook_block_view().
|
104
|
*/
|
105
|
function i18n_block_block_view_alter(&$data, $block) {
|
106
|
if (!empty($block->i18n_mode)) {
|
107
|
if (!empty($block->title) && $block->title != '<none>') {
|
108
|
// Unfiltered, as $block->subject will be created later from the title.
|
109
|
$data['title'] = i18n_string(array('blocks', $block->module, $block->delta, 'title'), $block->title, array('sanitize' => FALSE));
|
110
|
}
|
111
|
if ($block->module == 'block' && isset($data['content'])) {
|
112
|
$data['content'] = i18n_string(array('blocks', $block->module, $block->delta, 'body'), $data['content']);
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Implements hook_context_block_info_alter().
|
119
|
*/
|
120
|
function i18n_block_context_block_info_alter(&$block_info) {
|
121
|
$theme_key = variable_get('theme_default', 'garland');
|
122
|
$result = db_select('block')
|
123
|
->fields('block', array('module', 'delta', 'i18n_mode'))
|
124
|
->condition('theme', $theme_key)
|
125
|
->execute();
|
126
|
foreach ($result as $row) {
|
127
|
if (isset($block_info["{$row->module}-{$row->delta}"])) {
|
128
|
$block_info["{$row->module}-{$row->delta}"]->i18n_mode = $row->i18n_mode;
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Implements hook_help().
|
135
|
*/
|
136
|
function i18n_block_help($path, $arg) {
|
137
|
switch ($path) {
|
138
|
case 'admin/help#i18n_block':
|
139
|
$output = '<p>' . t('This module provides support for multilingual blocks.') . '</p>';
|
140
|
$output .= '<p>' . t('You can set up a language for a block or define it as translatable:') . '</p>';
|
141
|
$output .= '<ul>';
|
142
|
$output .= '<li>' . t('Blocks with a language will be displayed only in pages with that language.') . '</li>';
|
143
|
$output .= '<li>' . t('Translatable blocks can be translated using the localization interface.') . '</li>';
|
144
|
$output .= '</ul>';
|
145
|
$output .= '<p>' . t('To search and translate strings, use the <a href="@translate-interface">translation interface</a> pages.', array('@translate-interface' => url('admin/config/regional/translate'))) . '</p>';
|
146
|
return $output;
|
147
|
case 'admin/config/regional/i18n':
|
148
|
$output = '<p>'. t('To set up multilingual options for blocks go to the <a href="@configure_blocks">Blocks administration page</a>.', array('@configure_blocks' => url('admin/structure/block'))) .'</p>';
|
149
|
return $output;
|
150
|
}
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Remove strings for deleted custom blocks.
|
155
|
*/
|
156
|
function i18n_block_block_delete_submit(&$form, $form_state) {
|
157
|
$delta = $form_state['values']['delta'];
|
158
|
// Delete stored strings for the title and content fields.
|
159
|
i18n_string_remove("blocks:block:$delta:title");
|
160
|
i18n_string_remove("blocks:block:$delta:body");
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Implements block hook_form_FORM_ID_alter().
|
165
|
*
|
166
|
* Remove block title for multilingual blocks.
|
167
|
*/
|
168
|
function i18n_block_form_block_add_block_form_alter(&$form, &$form_state, $form_id) {
|
169
|
//i18n_block_alter_forms($form, $form_state, $form_id);
|
170
|
i18n_block_form_block_admin_configure_alter($form, $form_state, $form_id);
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Implements block hook_form_FORM_ID_alter().
|
175
|
*
|
176
|
* Remove block title for multilingual blocks.
|
177
|
*/
|
178
|
function i18n_block_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
|
179
|
$form['i18n_block']['languages'] = array(
|
180
|
'#type' => 'fieldset',
|
181
|
'#title' => t('Languages'),
|
182
|
'#collapsible' => TRUE,
|
183
|
'#collapsed' => TRUE,
|
184
|
'#group' => 'visibility',
|
185
|
'#weight' => 5,
|
186
|
'#attached' => array(
|
187
|
'js' => array(drupal_get_path('module', 'i18n_block') . '/i18n_block.js'),
|
188
|
),
|
189
|
);
|
190
|
|
191
|
// Add translatable option, just title for module blocks, title and content
|
192
|
// for custom blocks.
|
193
|
$description = '';
|
194
|
$module = $form['module']['#value'];
|
195
|
$delta = $form['delta']['#value'];
|
196
|
|
197
|
// User created menus are exposed by the menu module, others by system.module.
|
198
|
if ($module == 'menu' || ($module == 'system' && !in_array($delta, array('mail', 'help', 'powered-by')))) {
|
199
|
$description = t('To translate the block content itself, <a href="@menu_translate_url">translate the menu</a> that is being shown.', array('@menu_translate_url' => url('admin/structure/menu/manage/' . $form['delta']['#value'])));
|
200
|
}
|
201
|
elseif ($module == 'views' && module_exists('i18nviews')) {
|
202
|
$name = substr($delta, 0, strpos($delta, '-'));
|
203
|
$description = t('To translate the block content itself, <a href="@views_translate_url">translate the view</a> that is being shown.', array('@views_translate_url' => url('admin/structure/views/view/' . $name . '/translate')));
|
204
|
}
|
205
|
elseif ($module != 'block') {
|
206
|
$description = t('This block has generated content, only the title can be translated here.');
|
207
|
}
|
208
|
|
209
|
$block = block_load($form['module']['#value'], $form['delta']['#value']);
|
210
|
$form['i18n_block']['languages']['i18n_mode'] = array(
|
211
|
'#type' => 'checkbox',
|
212
|
'#title' => t('Make this block translatable'),
|
213
|
'#default_value' => isset($block->i18n_mode) ? $block->i18n_mode : I18N_MODE_NONE,
|
214
|
'#description' => $description,
|
215
|
);
|
216
|
|
217
|
// Add option to select which language pages to show on.
|
218
|
$default_options = db_query("SELECT language FROM {i18n_block_language} WHERE module = :module AND delta = :delta", array(
|
219
|
':module' => $form['module']['#value'],
|
220
|
':delta' => $form['delta']['#value'],
|
221
|
))->fetchCol();
|
222
|
$form['i18n_block']['languages']['languages'] = array(
|
223
|
'#type' => 'checkboxes',
|
224
|
'#title' => t('Show this block for these languages'),
|
225
|
'#default_value' => $default_options,
|
226
|
'#options' => i18n_language_list(),
|
227
|
'#description' => t('If no language is selected, block will show regardless of language.'),
|
228
|
);
|
229
|
if (user_access('translate interface')) {
|
230
|
$form['actions']['translate'] = array(
|
231
|
'#type' => 'submit',
|
232
|
'#name' => 'save_translate',
|
233
|
'#value' => t('Save and translate'),
|
234
|
'#states' => array(
|
235
|
'visible' => array(
|
236
|
// The value must be a string so that the javascript comparison works.
|
237
|
":input[name=i18n_mode]" => array('checked' => TRUE),
|
238
|
),
|
239
|
),
|
240
|
);
|
241
|
}
|
242
|
$form['#submit'][] = 'i18n_block_form_block_admin_configure_submit';
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Form submit handler for block configuration form.
|
247
|
*
|
248
|
* @see i18n_block_form_block_admin_configure_alter()
|
249
|
*/
|
250
|
function i18n_block_form_block_admin_configure_submit(&$form, &$form_state) {
|
251
|
$module = $form_state['values']['module'];
|
252
|
$delta = $form_state['values']['delta'];
|
253
|
|
254
|
// Update block languages
|
255
|
db_delete('i18n_block_language')
|
256
|
->condition('module', $module)
|
257
|
->condition('delta', $delta)
|
258
|
->execute();
|
259
|
$query = db_insert('i18n_block_language')->fields(array('language', 'module', 'delta'));
|
260
|
foreach (array_filter($form_state['values']['languages']) as $language) {
|
261
|
$query->values(array(
|
262
|
'language' => $language,
|
263
|
'module' => $module,
|
264
|
'delta' => $delta,
|
265
|
));
|
266
|
}
|
267
|
$query->execute();
|
268
|
// Update block translation options and strings
|
269
|
if (isset($form_state['values']['i18n_mode'])) {
|
270
|
db_update('block')
|
271
|
->fields(array('i18n_mode' => $form_state['values']['i18n_mode']))
|
272
|
->condition('module', $module)
|
273
|
->condition('delta', $delta)
|
274
|
->execute();
|
275
|
i18n_block_update_strings($form_state['values'], $form_state['values']['i18n_mode']);
|
276
|
|
277
|
// If the save and translate button was clicked, redirect to the translate
|
278
|
// tab instead of the block overview.
|
279
|
if ($form_state['triggering_element']['#name'] == 'save_translate') {
|
280
|
$form_state['redirect'] = 'admin/structure/block/manage/' . $module . '/' . $delta . '/translate';
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
|
285
|
/**
|
286
|
* Update block strings
|
287
|
*/
|
288
|
function i18n_block_update_strings($block, $i18n_mode = TRUE) {
|
289
|
$title = $i18n_mode && $block['title'] !== '<none>' ? $block['title'] : '';
|
290
|
i18n_string_update(array('blocks', $block['module'], $block['delta'], 'title'), $title);
|
291
|
if (isset($block['body'])) {
|
292
|
if ($i18n_mode) {
|
293
|
i18n_string_update(array('blocks', $block['module'], $block['delta'], 'body'), $block['body']['value'], array('format' => $block['body']['format']));
|
294
|
}
|
295
|
else {
|
296
|
i18n_string_remove(array('blocks', $block['module'], $block['delta'], 'body'));
|
297
|
}
|
298
|
}
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Implements hook_form_FORMID_alter().
|
303
|
*
|
304
|
* Adds node specific submit handler to delete custom block form.
|
305
|
*
|
306
|
* @see block_custom_block_delete()
|
307
|
*/
|
308
|
function i18n_block_form_block_custom_block_delete_alter(&$form, &$form_state) {
|
309
|
$form['#submit'][] = 'i18n_block_form_block_custom_block_delete_submit';
|
310
|
}
|
311
|
|
312
|
/**
|
313
|
* Form submit handler for custom block delete form.
|
314
|
*
|
315
|
* @see node_form_block_custom_block_delete_alter()
|
316
|
*/
|
317
|
function i18n_block_form_block_custom_block_delete_submit($form, &$form_state) {
|
318
|
db_delete('i18n_block_language')
|
319
|
->condition('module', 'block')
|
320
|
->condition('delta', $form_state['values']['bid'])
|
321
|
->execute();
|
322
|
// Remove related strings
|
323
|
module_invoke('i18n_strings', 'remove',
|
324
|
array('blocks', 'block', $form_state['values']['bid']),
|
325
|
array('title', 'body')
|
326
|
);
|
327
|
}
|
328
|
|
329
|
/**
|
330
|
* Translate block.
|
331
|
*
|
332
|
* @param $block
|
333
|
* Core block object
|
334
|
*/
|
335
|
function i18n_block_translate_block($block) {
|
336
|
if (!empty($block->content) && $localizable) {
|
337
|
$block->content = i18n_string_text("blocks:$block->module:$block->delta:body", $block->content);
|
338
|
}
|
339
|
// If it has a custom title, localize it
|
340
|
if (!empty($block->title) && $block->title != '<none>') {
|
341
|
// Check plain here to allow module generated titles to keep any markup.
|
342
|
$block->subject = i18n_string_plain("blocks:$block->module:$block->delta:title", $block->subject);
|
343
|
}
|
344
|
return $block;
|
345
|
}
|