This module provides support for multilingual blocks
These are not real blocks, but metablocks that group together a number of normal blocks and display the right one depending on language
In the block administration pages you will find a new tab for creating "Multilingual blocks". Set them up as usual (region, visibility, etc...) and then click on configuration, to define which one of the other blocks will be shown for each language.
' ); case 'admin/build/block/i18n': return t('These are not real blocks, but metablocks that group together a number of normal blocks and display the right one depending on language
'); } } /** * Implementation of hook_menu() */ function i18nblocks_menu($may_cache) { $items = array(); if($may_cache){ $items[] = array('path' => 'admin/build/block/i18n', 'title' => t('Add multilingual block'), 'access' => user_access('administer blocks'), 'callback' => 'i18nblocks_add', 'type' => MENU_LOCAL_TASK); } return $items; } /** * Implementation of hook_block() */ function i18nblocks_block($op = 'list', $delta = 0, $edit = array()) { switch($op) { case 'list': for($i = 1; $i<= variable_get('i18nblocks_number', 0);$i++){ $block = i18nblocks_load($i); $blocks[$i]['info'] = isset($block['name']) ? $block['name'] : t('Multilingual block %number', array('%number' => $i)); } return $blocks; case 'view': $language = i18n_get_lang(); $meta = i18nblocks_load($delta); $module = $meta[$language]['module']; $gamma = $meta[$language]['delta']; $block = module_invoke($module, 'block', 'view', $gamma); return $block; case 'configure': return i18nblocks_form(i18nblocks_load($delta)); case 'save': i18nblocks_save($edit, $delta); break; } } /** * Implementation of block form_alter(). * * Remove block title for multilingual blocks. */ function i18nblocks_form_alter($form_id, &$form) { if ($form_id == 'block_admin_configure' && isset($form['block_settings']['i18nblocks'])) { unset($form['block_settings']['title']); $form['block_settings']['title'] = array('#type' => 'value', '#value' => ''); } } /** * Form for multilingual blocks */ function i18nblocks_form($i18nblock){ $languages = i18n_supported_languages(); $modules = array_intersect(module_list(), module_implements('block')); // Compile list of available blocks $blocklist = array('' => t(' -- ')); foreach (module_implements('block') as $module) { if($module != 'i18nblocks') { // Avoid this module's blocks, could be funny :-) if (is_array($module_blocks = module_invoke($module, 'block', 'list'))) { foreach($module_blocks as $delta => $block) { $blocklist[$module.':'.$delta] = $block['info']."($module)"; }; } } } $form['name'] = array('#type' => 'textfield', '#title' => t('Block description'), '#default_value' => !is_array($i18nblock['name']) ? $i18nblock['name'] : t('Multilingual block !number', array('!number' => variable_get('i18nblocks_number', 0) +1)), '#size' => 40, '#maxlength' => 40 ); $form['i18nblocks'] = array('#type' => 'fieldset', '#tree' => TRUE, '#title' => t('Select the block to be displayed for each language')); foreach($languages as $lang => $langname){ $form['i18nblocks'][$lang] = array( '#type' => 'select', '#title' => $langname, '#default_value' => isset($i18nblock[$lang]) ? $i18nblock[$lang]['module'] .':'. $i18nblock[$lang]['delta'] : '', '#options' => $blocklist ); } $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); return $form; } /** * Add a new metablock and go to settings page */ function i18nblocks_add(){ $delta = variable_get('i18nblocks_number', 0) +1; $block['name'] = t('Multilingual block ').$delta; $form = i18nblocks_form($block); $output = drupal_get_form('i18nblocks_form', $form); print theme('page', $output); } function i18nblocks_form_submit($form_id, $form_values){ $delta = variable_get('i18nblocks_number', 0) +1; variable_set('i18nblocks_number', $delta); i18nblocks_save($form_values, $delta); drupal_set_message(t('The new multilingual block has been created.')); drupal_goto('admin/build/block'); } /** * Db layer: for now it stores each block as a variable */ function i18nblocks_load($delta){ return variable_get('i18nblocks_'.$delta, array()); } function i18nblocks_save($edit, $delta){ $block = array('name' => $edit['name']); foreach($edit['i18nblocks'] as $lang => $path){ list($module, $gamma) = explode(':', $path); $block[$lang]['module'] = $module; $block[$lang]['delta'] = $gamma; } variable_set('i18nblocks_'.$delta, $block); }