module][$record->delta][$record->language] = TRUE; } foreach ($blocks as $key => $block) { if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) { // This block was added by a contrib module, leave it in the list. continue; } if (isset($block_languages[$block->module][$block->delta]) && !isset($block_languages[$block->module][$block->delta][$language->language])) { // Block not visible for this language unset($blocks[$key]); } } } /** * Implements hook_block_view() */ function i18n_block_block_view_alter(&$data, $block) { if (!empty($block->i18n_mode)) { if ($block->title && $block->title != '') { $block->title = i18n_string(array('blocks', $block->module, $block->delta, 'title'), $block->title); } if ($block->module == 'block' && isset($data['content'])) { $data['content'] = i18n_string(array('blocks', $block->module, $block->delta, 'body'), $data['content']); } } } /** * Implements hook_help(). */ function i18n_block_help($path, $arg) { switch ($path) { case 'admin/help#i18n_block': $output = '

' . t('This module provides support for multilingual blocks.') . '

'; $output .= '

' . t('You can set up a language for a block or define it as translatable:') . '

'; $output .= ''; $output .= '

' . t('To search and translate strings, use the translation interface pages.', array('@translate-interface' => url('admin/build/translate'))) . '

'; return $output; } } /** * Refresh all strings. */ function i18n_block_locale_refresh() { $query = db_select('block', 'b') ->distinct() ->fields('b', array('module', 'delta', 'title', 'i18n_mode')) ->fields('bc', array('body', 'format')) ->condition('i18n_mode', 1); $query->leftJoin('block_custom', 'bc', 'b.bid = bc.bid'); $result = $query->execute() ->fetchAll(PDO::FETCH_OBJ); foreach ($result as $block) { // If the block has a custom title and no language it must be translated if ($block->title && $block->title != '') { i18n_string_update("blocks:$block->module:$block->delta:title", $block->title); } // If the block has body and no language, must be a custom block (box) if ($block->body) { i18n_string_update("blocks:$block->module:$block->delta:body", $block->body, array('format' => $block->format)); } } return TRUE; // Meaning it completed with no issues } /** * Implements hook_i18n_string_info() */ function i18n_block_i18n_string_info() { $groups['blocks'] = array( 'title' => t('Blocks'), 'description' => t('Configurable blocks titles and content.'), 'format' => TRUE, // This group has strings with format (block body) 'list' => TRUE, // This group can list all strings ); return $groups; } /** * Implements hook_i18n_string_list() */ function i18n_block_i18n_string_list($group) { if ($group == 'blocks' || $group == 'all') { $strings = array(); $query = db_select('block', 'b') ->distinct() ->fields('b', array('module', 'delta', 'title', 'i18n_mode')) ->fields('bc', array('body', 'format')) ->condition('i18n_mode', 1); $query->leftJoin('block_custom', 'bc', 'b.bid = bc.bid'); $result = $query->execute() ->fetchAll(PDO::FETCH_OBJ); foreach ($result as $block) { // If the block has a custom title and no language it must be translated if ($block->title && $block->title != '') { $strings['blocks'][$block->module][$block->delta]['title'] = $block->title; } // If the block has body and no language, must be a custom block (box) if ($block->body) { $strings['blocks'][$block->module][$block->delta]['body'] = array( 'string' => $block->body, 'format' => $block->format ); } } return $strings; } } /** * Remove strings for deleted custom blocks. */ function i18n_block_block_delete_submit(&$form, $form_state) { $delta = $form_state['values']['delta']; // Delete stored strings for the title and content fields. i18n_string_remove_string("blocks:block:$delta:title"); i18n_string_remove_string("blocks:block:$delta:body"); } /** * Implements block hook_form_FORM_ID_alter(). * * Remove block title for multilingual blocks. */ function i18n_block_form_block_add_block_form_alter(&$form, &$form_state, $form_id) { //i18n_block_alter_forms($form, $form_state, $form_id); i18n_block_form_block_admin_configure_alter($form, $form_state, $form_id); } /** * Implements block hook_form_FORM_ID_alter(). * * Remove block title for multilingual blocks. */ function i18n_block_form_block_admin_configure_alter(&$form, &$form_state, $form_id) { $form['i18n_block']['languages'] = array( '#type' => 'fieldset', '#title' => t('Languages'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#group' => 'visibility', '#weight' => 5, '#attached' => array( 'js' => array(drupal_get_path('module', 'i18n_block') . '/i18n_block.js'), ), ); // Add translatable option, just title for module blocks, title and content for custom blocks. $block = block_load($form['module']['#value'], $form['delta']['#value']); $form['i18n_block']['languages']['i18n_mode'] = array( '#type' => 'checkbox', '#title' => $form['module']['#value'] == 'block' ? t("Make this block's title and content translatable.") : t("Make this block's title translatable."), '#default_value' => isset($block->i18n_mode) ? $block->i18n_mode : 0, '#access' => module_exists('i18n_string'), ); // Add option to select which language pages to show on. $default_options = db_query("SELECT language FROM {i18n_block_language} WHERE module = :module AND delta = :delta", array( ':module' => $form['module']['#value'], ':delta' => $form['delta']['#value'], ))->fetchCol(); $form['i18n_block']['languages']['languages'] = array( '#type' => 'checkboxes', '#title' => t('Only show this block for these languages'), '#default_value' => $default_options, '#options' => i18n_language_list(), '#description' => t('If no language is selected, block will show regardless of language.'), ); $form['#submit'][] = 'i18n_block_form_block_admin_configure_submit'; } /** * Form submit handler for block configuration form. * * @see i18n_block_form_block_admin_configure_alter() */ function i18n_block_form_block_admin_configure_submit(&$form, &$form_state) { $module = $form_state['values']['module']; $delta = $form_state['values']['delta']; // Update block languages db_delete('i18n_block_language') ->condition('module', $module) ->condition('delta', $delta) ->execute(); $query = db_insert('i18n_block_language')->fields(array('language', 'module', 'delta')); foreach (array_filter($form_state['values']['languages']) as $language) { $query->values(array( 'language' => $language, 'module' => $module, 'delta' => $delta, )); } $query->execute(); // Update block translation options and strings if (isset($form_state['values']['i18n_mode'])) { db_update('block') ->fields(array('i18n_mode' => $form_state['values']['i18n_mode'])) ->condition('module', $module) ->condition('delta', $delta) ->execute(); i18n_block_update_strings($form_state['values'], $form_state['values']['i18n_mode']); } } /** * Update block strings */ function i18n_block_update_strings($block, $i18n_mode = TRUE) { if (module_exists('i18n_string')) { $title = $i18n_mode && $block['title'] !== '' ? $block['title'] : ''; i18n_string_update(array('blocks', $block['module'], $block['delta'], 'title'), $title); if (isset($block['body'])) { if ($i18n_mode) { i18n_string_update(array('blocks', $block['module'], $block['delta'], 'body'), $block['body']['value'], array('format' => $block['body']['format'])); } else { i18n_string_remove(array('blocks', $block['module'], $block['delta'], 'body')); } } } } /** * Implements hook_form_FORMID_alter(). * * Adds node specific submit handler to delete custom block form. * * @see block_custom_block_delete() */ function i18n_block_form_block_custom_block_delete_alter(&$form, &$form_state) { $form['#submit'][] = 'i18n_block_form_block_custom_block_delete_submit'; } /** * Form submit handler for custom block delete form. * * @see node_form_block_custom_block_delete_alter() */ function i18n_block_form_block_custom_block_delete_submit($form, &$form_state) { db_delete('i18n_block_language') ->condition('module', 'block') ->condition('delta', $form_state['values']['bid']) ->execute(); // Remove related strings module_invoke('i18n_strings', 'remove', array('blocks', 'block', $form_state['values']['bid']), array('title', 'body') ); } /** * Translate block. * * @param $block * Core block object */ function i18n_block_translate_block($block) { if (!empty($block->content) && $localizable) { $block->content = i18n_string_text("blocks:$block->module:$block->delta:body", $block->content); } // If it has a custom title, localize it if (!empty($block->title) && $block->title != '') { // Check plain here to allow module generated titles to keep any markup. $block->subject = i18n_string_plain("blocks:$block->module:$block->delta:title", $block->subject); } return $block; }