i18n_block.module 12 KB

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