Projet

Général

Profil

Paste
Télécharger (12,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_block / i18n_block.module @ 9faa5de0

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
 * Implements hook_permission().
42
 */
43
function i18n_block_permission() {
44
  return array(
45
    'translate blocks' => array(
46
      'title' => t('Translate Blocks'),
47
    ),
48
  );
49
}
50

    
51
/**
52
 * Implement hook_menu_alter().
53
 *
54
 * Reorganize block tabs so that they make sense.
55
 */
56
function i18n_block_menu_alter(&$items) {
57
  // Give the configure tab a short name and make it display.
58
  $items['admin/structure/block/manage/%/%/configure']['weight'] = -100;
59
  $items['admin/structure/block/manage/%/%/configure']['title'] = 'Configure';
60
  $items['admin/structure/block/manage/%/%/configure']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
61
  // Hide the delete tab. Not sure why this was even set a local task then
62
  // set to not show in any context...
63
  $items['admin/structure/block/manage/%/%/delete']['type'] = MENU_CALLBACK;
64
}
65

    
66
/**
67
 * Menu access callback function.
68
 *
69
 * Only let blocks translated which are configured to be translatable.
70
 */
71
function i18n_block_translate_tab_access($module, $delta) {
72
  $block = block_load($module, $delta);
73
  return (user_access('translate interface') || user_access('translate blocks')) && $block && isset($block->i18n_mode) && ($block->i18n_mode == I18N_MODE_LOCALIZE);
74
}
75

    
76
/**
77
 * Build a translation page for the given block.
78
 */
79
function i18n_block_translate_tab_page($module, $delta, $language = NULL) {
80
  $block = block_load($module, $delta);
81
  return i18n_string_object_translate_page('block', $block, $language);
82
}
83

    
84
/**
85
 * Implements hook_block_list_alter().
86
 *
87
 * Translate localizable blocks.
88
 *
89
 * To be run after all block visibility modules have run, just translate the blocks to be displayed
90
 */
91
function i18n_block_block_list_alter(&$blocks) {
92
  global $theme_key, $language;
93

    
94
  // Build an array of node types for each block.
95
  $block_languages = array();
96
  $result = db_query('SELECT module, delta, language FROM {i18n_block_language}');
97
  foreach ($result as $record) {
98
    $block_languages[$record->module][$record->delta][$record->language] = TRUE;
99
  }
100

    
101
  foreach ($blocks as $key => $block) {
102
    if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
103
      // This block was added by a contrib module, leave it in the list.
104
      continue;
105
    }
106
    if (isset($block_languages[$block->module][$block->delta]) && !isset($block_languages[$block->module][$block->delta][$language->language])) {
107
      // Block not visible for this language
108
      unset($blocks[$key]);
109
    }
110
  }
111
}
112

    
113
/**
114
 * Implements hook_block_view_alter().
115
 */
116
function i18n_block_block_view_alter(&$data, $block) {
117
  if (!empty($block->i18n_mode)) {
118
    if (!empty($block->title) && $block->title != '<none>') {
119
      // Unfiltered, as $block->subject will be created later from the title.
120
      $data['title'] = i18n_string(array('blocks', $block->module, $block->delta, 'title'), $block->title, array('sanitize' => FALSE));
121
    }
122
    if ($block->module == 'block' && isset($data['content'])) {
123
      $data['content'] = i18n_string(array('blocks', $block->module, $block->delta, 'body'), $data['content']);
124
    }
125
  }
126
}
127

    
128
/**
129
 * Implements hook_context_block_info_alter().
130
 */
131
function i18n_block_context_block_info_alter(&$block_info) {
132
  $theme_key = variable_get('theme_default', 'garland');
133
  $result = db_select('block')
134
    ->fields('block', array('module', 'delta', 'i18n_mode'))
135
    ->condition('theme', $theme_key)
136
    ->execute();
137
  foreach ($result as $row) {
138
    if (isset($block_info["{$row->module}-{$row->delta}"])) {
139
      $block_info["{$row->module}-{$row->delta}"]->i18n_mode = $row->i18n_mode;
140
    }
141
  }
142
}
143

    
144
/**
145
 * Implements hook_help().
146
 */
147
function i18n_block_help($path, $arg) {
148
  switch ($path) {
149
    case 'admin/help#i18n_block':
150
      $output = '<p>' . t('This module provides support for multilingual blocks.') . '</p>';
151
      $output .= '<p>' . t('You can set up a language for a block or define it as translatable:') . '</p>';
152
      $output .= '<ul>';
153
      $output .= '<li>' . t('Blocks with a language will be displayed only in pages with that language.') . '</li>';
154
      $output .= '<li>' . t('Translatable blocks can be translated using the localization interface.') . '</li>';
155
      $output .= '</ul>';
156
      $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>';
157
      return $output;
158
    case 'admin/config/regional/i18n':
159
      $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>';
160
      return $output;
161
  }
162
}
163

    
164
/**
165
 * Remove strings for deleted custom blocks.
166
 */
167
function i18n_block_block_delete_submit(&$form, $form_state) {
168
  $delta = $form_state['values']['delta'];
169
  // Delete stored strings for the title and content fields.
170
  i18n_string_remove("blocks:block:$delta:title");
171
  i18n_string_remove("blocks:block:$delta:body");
172
}
173

    
174
/**
175
 * Implements block hook_form_FORM_ID_alter().
176
 *
177
 * Remove block title for multilingual blocks.
178
 */
179
function i18n_block_form_block_add_block_form_alter(&$form, &$form_state, $form_id) {
180
  //i18n_block_alter_forms($form, $form_state, $form_id);
181
  i18n_block_form_block_admin_configure_alter($form, $form_state, $form_id);
182
}
183

    
184
/**
185
 * Implements block hook_form_FORM_ID_alter().
186
 *
187
 * Remove block title for multilingual blocks.
188
 */
189
function i18n_block_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
190
  $form['i18n_block']['languages'] = array(
191
    '#type' => 'fieldset',
192
    '#title' => t('Languages'),
193
    '#collapsible' => TRUE,
194
    '#collapsed' => TRUE,
195
    '#group' => 'visibility',
196
    '#weight' => 5,
197
    '#attached' => array(
198
      'js' => array(drupal_get_path('module', 'i18n_block') . '/i18n_block.js'),
199
    ),
200
  );
201

    
202
  // Add translatable option, just title for module blocks, title and content
203
  // for custom blocks.
204
  $description = '';
205
  $module = $form['module']['#value'];
206
  $delta = $form['delta']['#value'];
207

    
208
  // User created menus are exposed by the menu module, others by system.module.
209
  if ($module == 'menu' || ($module == 'system' && !in_array($delta, array('mail', 'help', 'powered-by')))) {
210
    $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'])));
211
  }
212
  elseif ($module == 'views' && module_exists('i18nviews')) {
213
    $name = substr($delta, 0, strpos($delta, '-'));
214
    $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')));
215
  }
216
  elseif ($module != 'block') {
217
    $description = t('This block has generated content, only the title can be translated here.');
218
  }
219

    
220
  $block = block_load($form['module']['#value'], $form['delta']['#value']);
221
  $form['i18n_block']['languages']['i18n_mode'] = array(
222
    '#type' => 'checkbox',
223
    '#title' => t('Make this block translatable'),
224
    '#default_value' => isset($block->i18n_mode) ? $block->i18n_mode : I18N_MODE_NONE,
225
    '#description' => $description,
226
  );
227

    
228
  // Add option to select which language pages to show on.
229
  $default_options = db_query("SELECT language FROM {i18n_block_language} WHERE module = :module AND delta = :delta", array(
230
    ':module' => $form['module']['#value'],
231
    ':delta' => $form['delta']['#value'],
232
  ))->fetchCol();
233
  $form['i18n_block']['languages']['languages'] = array(
234
    '#type' => 'checkboxes',
235
    '#title' => t('Show this block for these languages'),
236
    '#default_value' => $default_options,
237
    '#options' => i18n_language_list(),
238
    '#description' => t('If no language is selected, block will show regardless of language.'),
239
  );
240
  if (user_access('translate interface') || user_access('translate blocks')) {
241
    $form['actions']['translate'] = array(
242
      '#type' => 'submit',
243
      '#name'   => 'save_translate',
244
      '#value' => t('Save and translate'),
245
      '#states' => array(
246
        'visible' => array(
247
          // The value must be a string so that the javascript comparison works.
248
          ":input[name=i18n_mode]" => array('checked' => TRUE),
249
        ),
250
      ),
251
    );
252
  }
253
  $form['#submit'][] = 'i18n_block_form_block_admin_configure_submit';
254
}
255

    
256
/**
257
 * Form submit handler for block configuration form.
258
 *
259
 * @see i18n_block_form_block_admin_configure_alter()
260
 */
261
function i18n_block_form_block_admin_configure_submit(&$form, &$form_state) {
262
  $module = $form_state['values']['module'];
263
  $delta = $form_state['values']['delta'];
264

    
265
  // Update block languages
266
  db_delete('i18n_block_language')
267
    ->condition('module', $module)
268
    ->condition('delta', $delta)
269
    ->execute();
270
  $query = db_insert('i18n_block_language')->fields(array('language', 'module', 'delta'));
271
  foreach (array_filter($form_state['values']['languages']) as $language) {
272
    $query->values(array(
273
      'language' => $language,
274
      'module' => $module,
275
      'delta' => $delta,
276
    ));
277
  }
278
  $query->execute();
279
  // Update block translation options and strings
280
  if (isset($form_state['values']['i18n_mode'])) {
281
    db_update('block')
282
      ->fields(array('i18n_mode' => $form_state['values']['i18n_mode']))
283
      ->condition('module', $module)
284
      ->condition('delta', $delta)
285
      ->execute();
286
    i18n_block_update_strings($form_state['values'], $form_state['values']['i18n_mode']);
287

    
288
    // If the save and translate button was clicked, redirect to the translate
289
    // tab instead of the block overview.
290
    if ($form_state['triggering_element']['#name'] == 'save_translate') {
291
      $form_state['redirect'] = 'admin/structure/block/manage/' . $module . '/' . $delta . '/translate';
292
    }
293
  }
294
}
295

    
296
/**
297
 * Update block strings
298
 */
299
function i18n_block_update_strings($block, $i18n_mode = TRUE) {
300
  $title = $i18n_mode && $block['title'] !== '<none>' ? $block['title'] : '';
301
  i18n_string_update(array('blocks', $block['module'], $block['delta'], 'title'), $title);
302
  if (isset($block['body'])) {
303
    if ($i18n_mode) {
304
      i18n_string_update(array('blocks', $block['module'], $block['delta'], 'body'), $block['body']['value'], array('format' => $block['body']['format']));
305
    }
306
    else {
307
      i18n_string_remove(array('blocks', $block['module'], $block['delta'], 'body'));
308
    }
309
  }
310
}
311

    
312
/**
313
 * Implements hook_form_FORMID_alter().
314
 *
315
 * Adds node specific submit handler to delete custom block form.
316
 *
317
 * @see block_custom_block_delete()
318
 */
319
function i18n_block_form_block_custom_block_delete_alter(&$form, &$form_state) {
320
  $form['#submit'][] = 'i18n_block_form_block_custom_block_delete_submit';
321
}
322

    
323
/**
324
 * Form submit handler for custom block delete form.
325
 *
326
 * @see node_form_block_custom_block_delete_alter()
327
 */
328
function i18n_block_form_block_custom_block_delete_submit($form, &$form_state) {
329
  db_delete('i18n_block_language')
330
    ->condition('module', 'block')
331
    ->condition('delta', $form_state['values']['bid'])
332
    ->execute();
333
  // Remove related strings
334
  module_invoke('i18n_strings', 'remove',
335
    array('blocks', 'block', $form_state['values']['bid']),
336
    array('title', 'body')
337
  );
338
}
339

    
340
/**
341
 * Translate block.
342
 *
343
 * @param $block
344
 *   Core block object
345
 */
346
function i18n_block_translate_block($block) {
347
  if (!empty($block->content) && $localizable) {
348
    $block->content = i18n_string_text("blocks:$block->module:$block->delta:body", $block->content);
349
  }
350
  // If it has a custom title, localize it
351
  if (!empty($block->title) && $block->title != '<none>') {
352
    // Check plain here to allow module generated titles to keep any markup.
353
    $block->subject = i18n_string_plain("blocks:$block->module:$block->delta:title", $block->subject);
354
  }
355
  return $block;
356
}