first import
This commit is contained in:
65
sites/all/modules/i18n/i18n_block/i18n_block.i18n.inc
Normal file
65
sites/all/modules/i18n/i18n_block/i18n_block.i18n.inc
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Internationalization (i18n) hooks
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_i18n_object_info().
|
||||
*/
|
||||
function i18n_block_i18n_object_info() {
|
||||
$info['block'] = array(
|
||||
'title' => t('Block'),
|
||||
'class' => 'i18n_block_object',
|
||||
'load callback' => 'block_load',
|
||||
'key' => array('module', 'delta'),
|
||||
'placeholders' => array(
|
||||
'%module' => 'module',
|
||||
'%delta' => 'delta',
|
||||
),
|
||||
'edit path' => 'admin/structure/block/manage/%module/%delta/configure',
|
||||
'string translation' => array(
|
||||
'textgroup' => 'blocks',
|
||||
'properties' => array(
|
||||
'title' => array(
|
||||
'title' => t('Title'),
|
||||
'empty' => '<none>',
|
||||
),
|
||||
'body' => array(
|
||||
'title' => t('Body'),
|
||||
'format' => 'format',
|
||||
),
|
||||
),
|
||||
'translate path' => 'admin/structure/block/manage/%module/%delta/translate/%i18n_language',
|
||||
)
|
||||
);
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_objects().
|
||||
*/
|
||||
function i18n_block_i18n_string_objects($type) {
|
||||
if ($type == 'block') {
|
||||
$query = db_select('block', 'b')
|
||||
->distinct()
|
||||
->fields('b', array('module', 'delta', 'title', 'i18n_mode'))
|
||||
->fields('bc', array('body', 'format'))
|
||||
->condition('i18n_mode', I18N_MODE_LOCALIZE);
|
||||
$query->leftJoin('block_custom', 'bc', 'b.bid = bc.bid');
|
||||
return $query->execute()->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
}
|
52
sites/all/modules/i18n/i18n_block/i18n_block.inc
Normal file
52
sites/all/modules/i18n/i18n_block/i18n_block.inc
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Blocks textgroup handler
|
||||
*/
|
||||
|
||||
/**
|
||||
* Block object
|
||||
*/
|
||||
class i18n_block_object extends i18n_string_object_wrapper {
|
||||
|
||||
/**
|
||||
* Load a block object.
|
||||
*
|
||||
* @param $object
|
||||
* An array with module and delta.
|
||||
*/
|
||||
function load_object($object) {
|
||||
$this->object = call_user_func_array($this->get_info('load callback'), $object);
|
||||
return $this->get_object();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get base keys for translating this object
|
||||
*/
|
||||
public function get_string_context() {
|
||||
return array($this->object->module, $this->object->delta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get object strings for translation
|
||||
*/
|
||||
protected function build_properties() {
|
||||
if ($this->object->module == 'block' && !isset($this->object->body)) {
|
||||
$block = (object) block_custom_block_get($this->object->delta);
|
||||
$this->object->body = $block->body;
|
||||
$this->object->format = $block->format;
|
||||
}
|
||||
$properties = parent::build_properties();
|
||||
// Body is available only for custom blocks.
|
||||
if ($this->object->module != 'block') {
|
||||
unset($properties[$this->get_textgroup()][$this->object->module][$this->object->delta]['body']);
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translation mode for object
|
||||
*/
|
||||
public function get_translate_mode() {
|
||||
return !empty($this->object->i18n_mode) ? I18N_MODE_LOCALIZE : I18N_MODE_NONE;
|
||||
}
|
||||
}
|
16
sites/all/modules/i18n/i18n_block/i18n_block.info
Normal file
16
sites/all/modules/i18n/i18n_block/i18n_block.info
Normal file
@@ -0,0 +1,16 @@
|
||||
name = Block languages
|
||||
description = Enables language selector for blocks and optional block translation.
|
||||
dependencies[] = block
|
||||
dependencies[] = i18n_string
|
||||
package = Multilingual - Internationalization
|
||||
core = 7.x
|
||||
files[] = i18n_block.inc
|
||||
files[] = i18n_block.test
|
||||
|
||||
|
||||
; Information added by drupal.org packaging script on 2013-01-13
|
||||
version = "7.x-1.8"
|
||||
core = "7.x"
|
||||
project = "i18n"
|
||||
datestamp = "1358075001"
|
||||
|
109
sites/all/modules/i18n/i18n_block/i18n_block.install
Normal file
109
sites/all/modules/i18n/i18n_block/i18n_block.install
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Installation file for i18nblocks module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function i18n_block_install() {
|
||||
module_load_install('i18n');
|
||||
i18n_install_create_fields('block', array('i18n_mode'));
|
||||
// Set module weight for it to run after all block visibility modules have run
|
||||
db_query("UPDATE {system} SET weight = 100 WHERE name = 'i18n_block' AND type = 'module'");
|
||||
// If updating from D6, module changed name
|
||||
if (variable_get('i18n_drupal6_update')) {
|
||||
i18n_block_update_7000();
|
||||
i18n_block_update_7001();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*/
|
||||
function i18n_block_uninstall() {
|
||||
db_drop_field('block', 'i18n_mode');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_schema().
|
||||
*/
|
||||
function i18n_block_schema() {
|
||||
$schema['i18n_block_language'] = array(
|
||||
'description' => 'Sets block visibility based on language',
|
||||
'fields' => array(
|
||||
'module' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 64,
|
||||
'not null' => TRUE,
|
||||
'description' => "The block's origin module, from {block}.module.",
|
||||
),
|
||||
'delta' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'description' => "The block's unique delta within module, from {block}.delta.",
|
||||
),
|
||||
'language' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 12,
|
||||
'not null' => TRUE,
|
||||
'default' => '',
|
||||
'description' => "Language code, e.g. 'de' or 'en-US'.",
|
||||
),
|
||||
),
|
||||
'primary key' => array('module', 'delta', 'language'),
|
||||
'indexes' => array(
|
||||
'language' => array('language'),
|
||||
),
|
||||
);
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_schema_alter().
|
||||
*
|
||||
* Add block table i18n_mode field
|
||||
*/
|
||||
function i18n_block_schema_alter(&$schema) {
|
||||
$schema['block']['fields']['i18n_mode'] = array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => 'Block multilingual mode.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Drupal 6 update from old i18nblocks module.
|
||||
*/
|
||||
function i18n_block_update_7000() {
|
||||
// D6-D7 updates, to be written
|
||||
// move block language from i18n_blocks into i18n_block_language
|
||||
// Move block type from i18n_blocks into block table (i18n_mode)
|
||||
if (db_table_exists('i18n_blocks')) {
|
||||
foreach (db_query("SELECT * FROM {i18n_blocks}")->fetchAll() as $block) {
|
||||
if ($block->language) {
|
||||
// Set language for block
|
||||
db_merge('i18n_block_language')
|
||||
->key(array('module' => $block->module, 'delta' => $block->delta))
|
||||
->fields(array('language' => $block->language))
|
||||
->execute();
|
||||
}
|
||||
else {
|
||||
// Mark block as translatable
|
||||
db_update('block')
|
||||
->fields(array('i18n_mode' => 1))
|
||||
->condition('module', $block->module)
|
||||
->condition('delta', $block->delta)
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop Drupal 6 {i18n_blocks} table after migration.
|
||||
*/
|
||||
function i18n_block_update_7001() {
|
||||
if (db_table_exists('i18n_blocks')) {
|
||||
db_drop_table('i18n_blocks');
|
||||
}
|
||||
}
|
30
sites/all/modules/i18n/i18n_block/i18n_block.js
Normal file
30
sites/all/modules/i18n/i18n_block/i18n_block.js
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
(function ($) {
|
||||
|
||||
/**
|
||||
* Provide the summary information for the block settings vertical tab.
|
||||
*/
|
||||
Drupal.behaviors.i18nSettingsSummary = {
|
||||
attach: function (context) {
|
||||
|
||||
$('fieldset#edit-languages', context).drupalSetSummary(function (context) {
|
||||
var summary = '';
|
||||
if ($('.form-item-i18n-mode input[type=checkbox]:checked', context).val()) {
|
||||
summary += Drupal.t('Translatable');
|
||||
}
|
||||
else {
|
||||
summary += Drupal.t('Not translatable');
|
||||
}
|
||||
summary += ', ';
|
||||
if ($('.form-item-languages input[type=checkbox]:checked', context).val()) {
|
||||
summary += Drupal.t('Restricted to certain languages');
|
||||
}
|
||||
else {
|
||||
summary += Drupal.t('Not restricted');
|
||||
}
|
||||
return summary;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
})(jQuery);
|
345
sites/all/modules/i18n/i18n_block/i18n_block.module
Normal file
345
sites/all/modules/i18n/i18n_block/i18n_block.module
Normal file
@@ -0,0 +1,345 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Internationalization (i18n) submodule: Multilingual meta-blocks
|
||||
*
|
||||
* @author Jose A. Reyero, 2005
|
||||
*
|
||||
* @ TODO Add strings on block update.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*
|
||||
* Add translate tab to blocks.
|
||||
*/
|
||||
function i18n_block_menu() {
|
||||
$items['admin/structure/block/manage/%/%/translate'] = array(
|
||||
'title' => 'Translate',
|
||||
'access callback' => 'i18n_block_translate_tab_access',
|
||||
'access arguments' => array(4, 5),
|
||||
'page callback' => 'i18n_block_translate_tab_page',
|
||||
'page arguments' => array(4, 5),
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
|
||||
'weight' => 10,
|
||||
);
|
||||
$items['admin/structure/block/manage/%/%/translate/%i18n_language'] = array(
|
||||
'title' => 'Translate',
|
||||
'access callback' => 'i18n_block_translate_tab_access',
|
||||
'access arguments' => array(4, 5),
|
||||
'page callback' => 'i18n_block_translate_tab_page',
|
||||
'page arguments' => array(4, 5, 7),
|
||||
'type' => MENU_CALLBACK,
|
||||
'weight' => 10,
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement hook_menu_alter().
|
||||
*
|
||||
* Reorganize block tabs so that they make sense.
|
||||
*/
|
||||
function i18n_block_menu_alter(&$items) {
|
||||
// Give the configure tab a short name and make it display.
|
||||
$items['admin/structure/block/manage/%/%/configure']['weight'] = -100;
|
||||
$items['admin/structure/block/manage/%/%/configure']['title'] = 'Configure';
|
||||
$items['admin/structure/block/manage/%/%/configure']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
|
||||
// Hide the delete tab. Not sure why this was even set a local task then
|
||||
// set to not show in any context...
|
||||
$items['admin/structure/block/manage/%/%/delete']['type'] = MENU_CALLBACK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Menu access callback function.
|
||||
*
|
||||
* Only let blocks translated which are configured to be translatable.
|
||||
*/
|
||||
function i18n_block_translate_tab_access($module, $delta) {
|
||||
$block = block_load($module, $delta);
|
||||
return user_access('translate interface') && isset($block) && ($block->i18n_mode == I18N_MODE_LOCALIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a translation page for the given block.
|
||||
*/
|
||||
function i18n_block_translate_tab_page($module, $delta, $language = NULL) {
|
||||
$block = block_load($module, $delta);
|
||||
return i18n_string_object_translate_page('block', $block, $language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_block_list_alter().
|
||||
*
|
||||
* Translate localizable blocks.
|
||||
*
|
||||
* To be run after all block visibility modules have run, just translate the blocks to be displayed
|
||||
*/
|
||||
function i18n_block_block_list_alter(&$blocks) {
|
||||
global $theme_key, $language;
|
||||
|
||||
// Build an array of node types for each block.
|
||||
$block_languages = array();
|
||||
$result = db_query('SELECT module, delta, language FROM {i18n_block_language}');
|
||||
foreach ($result as $record) {
|
||||
$block_languages[$record->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 (!empty($block->title) && $block->title != '<none>') {
|
||||
// Unfiltered, as $block->subject will be created later from the title.
|
||||
$data['title'] = i18n_string(array('blocks', $block->module, $block->delta, 'title'), $block->title, array('sanitize' => FALSE));
|
||||
}
|
||||
if ($block->module == 'block' && isset($data['content'])) {
|
||||
$data['content'] = i18n_string(array('blocks', $block->module, $block->delta, 'body'), $data['content']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_context_block_info_alter().
|
||||
*/
|
||||
function i18n_block_context_block_info_alter(&$block_info) {
|
||||
$theme_key = variable_get('theme_default', 'garland');
|
||||
$result = db_select('block')
|
||||
->fields('block', array('module', 'delta', 'i18n_mode'))
|
||||
->condition('theme', $theme_key)
|
||||
->execute();
|
||||
foreach ($result as $row) {
|
||||
if (isset($block_info["{$row->module}-{$row->delta}"])) {
|
||||
$block_info["{$row->module}-{$row->delta}"]->i18n_mode = $row->i18n_mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function i18n_block_help($path, $arg) {
|
||||
switch ($path) {
|
||||
case 'admin/help#i18n_block':
|
||||
$output = '<p>' . t('This module provides support for multilingual blocks.') . '</p>';
|
||||
$output .= '<p>' . t('You can set up a language for a block or define it as translatable:') . '</p>';
|
||||
$output .= '<ul>';
|
||||
$output .= '<li>' . t('Blocks with a language will be displayed only in pages with that language.') . '</li>';
|
||||
$output .= '<li>' . t('Translatable blocks can be translated using the localization interface.') . '</li>';
|
||||
$output .= '</ul>';
|
||||
$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>';
|
||||
return $output;
|
||||
case 'admin/config/regional/i18n':
|
||||
$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>';
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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("blocks:block:$delta:title");
|
||||
i18n_string_remove("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.
|
||||
$description = '';
|
||||
$module = $form['module']['#value'];
|
||||
$delta = $form['delta']['#value'];
|
||||
|
||||
// User created menus are exposed by the menu module, others by system.module.
|
||||
if ($module == 'menu' || ($module == 'system' && !in_array($delta, array('mail', 'help', 'powered-by')))) {
|
||||
$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'])));
|
||||
}
|
||||
elseif ($module == 'views' && module_exists('i18nviews')) {
|
||||
$name = substr($delta, 0, strpos($delta, '-'));
|
||||
$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')));
|
||||
}
|
||||
elseif ($module != 'block') {
|
||||
$description = t('This block has generated content, only the title can be translated here.');
|
||||
}
|
||||
|
||||
$block = block_load($form['module']['#value'], $form['delta']['#value']);
|
||||
$form['i18n_block']['languages']['i18n_mode'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Make this block translatable'),
|
||||
'#default_value' => isset($block->i18n_mode) ? $block->i18n_mode : I18N_MODE_NONE,
|
||||
'#description' => $description,
|
||||
);
|
||||
|
||||
// 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('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.'),
|
||||
);
|
||||
if (user_access('translate interface')) {
|
||||
$form['actions']['translate'] = array(
|
||||
'#type' => 'submit',
|
||||
'#name' => 'save_translate',
|
||||
'#value' => t('Save and translate'),
|
||||
'#states' => array(
|
||||
'visible' => array(
|
||||
// The value must be a string so that the javascript comparison works.
|
||||
":input[name=i18n_mode]" => array('checked' => TRUE),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
$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']);
|
||||
|
||||
// If the save and translate button was clicked, redirect to the translate
|
||||
// tab instead of the block overview.
|
||||
if ($form_state['triggering_element']['#name'] == 'save_translate') {
|
||||
$form_state['redirect'] = 'admin/structure/block/manage/' . $module . '/' . $delta . '/translate';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update block strings
|
||||
*/
|
||||
function i18n_block_update_strings($block, $i18n_mode = TRUE) {
|
||||
$title = $i18n_mode && $block['title'] !== '<none>' ? $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 != '<none>') {
|
||||
// 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;
|
||||
}
|
197
sites/all/modules/i18n/i18n_block/i18n_block.test
Normal file
197
sites/all/modules/i18n/i18n_block/i18n_block.test
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Test case for multilingual blocks
|
||||
*/
|
||||
|
||||
class i18nBlocksTestCase extends Drupali18nTestCase {
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Block translation',
|
||||
'group' => 'Internationalization',
|
||||
'description' => 'Block translation functions'
|
||||
);
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
parent::setUp('i18n_block');
|
||||
parent::setUpLanguages();
|
||||
$this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
|
||||
|
||||
$format = filter_default_format();
|
||||
variable_set('i18n_string_allowed_formats', array($format => $format));
|
||||
$this->drupalLogin($this->admin_user);
|
||||
}
|
||||
|
||||
function testBlockTranslation() {
|
||||
|
||||
$block_translater = $this->drupalCreateUser(array('administer blocks', 'translate interface', 'translate user-defined strings'));
|
||||
|
||||
// Display Language switcher block
|
||||
$switcher = array('module' => 'locale', 'delta' => 'language', 'title' => t('Languages'));
|
||||
$this->moveBlockToRegion($switcher);
|
||||
// Add a custom title to language switcher block and check it displays translated
|
||||
$title = $this->randomName(10);
|
||||
$this->updateBlock($switcher, array('title' => $title, 'i18n_mode' => I18N_MODE_LOCALIZE));
|
||||
$this->assertText($title, "The new custom title is displayed on the home page.");
|
||||
$translations = $this->createStringTranslation('blocks', $title);
|
||||
$this->i18nAssertTranslations($translations);
|
||||
|
||||
// Create a translatable block and test block visibility per language.
|
||||
$block = $this->i18nCreateBlock();
|
||||
// Now set a language for the block and confirm it shows just for that one (without translation)
|
||||
$languages = $this->getEnabledLanguages();
|
||||
$setlanguage = array_shift($languages);
|
||||
$otherlanguage = array_shift($languages);
|
||||
$this->setBlockLanguages($block, array($setlanguage->language));
|
||||
// Show in block's language but not translated
|
||||
$this->i18nGet($setlanguage);
|
||||
$this->assertText($block['title']);
|
||||
// Do not show in the other language
|
||||
$this->i18nGet($otherlanguage);
|
||||
$this->assertNoText($block['title']);
|
||||
|
||||
// Create a new block, translate it and check the right translations are displayed for title and body
|
||||
$box2 = $this->i18nCreateBlock();
|
||||
// Create translations for title and body, source strings should be already there
|
||||
$translations = $this->i18nTranslateBlock($box2);
|
||||
$this->i18nAssertTranslations($translations['title'], '', 'Custom block title translation displayed.');
|
||||
$this->i18nAssertTranslations($translations['body'], '', 'Custom block body translation displayed.');
|
||||
|
||||
// Test the translate tab.
|
||||
$this->drupalLogin($this->admin_user);
|
||||
$this->drupalGet('admin/structure/block/manage/' . $box2['module'] . '/' . $box2['delta'] . '/configure');
|
||||
$this->assertNoFieldByName('save_and_translate');
|
||||
|
||||
$this->drupalLogin($block_translater);
|
||||
$this->drupalPost('admin/structure/block/manage/' . $box2['module'] . '/' . $box2['delta'] . '/configure', array(), t('Save and translate'));
|
||||
// @todo Improve these checks.
|
||||
$this->assertText(t('Spanish'));
|
||||
$this->assertText(t('translated'));
|
||||
|
||||
$this->clickLink(t('translate'));
|
||||
|
||||
$this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':title]', $translations['title']['es']);
|
||||
$this->assertFieldByName('strings[blocks:block:' . $box2['delta'] . ':body]', $translations['body']['es']);
|
||||
|
||||
// Update the translation.
|
||||
$translations['title']['es'] = $this->randomName(10);
|
||||
$translations['body']['es'] = $this->randomName(20);
|
||||
$edit = array(
|
||||
'strings[blocks:block:' . $box2['delta'] . ':title]' => $translations['title']['es'],
|
||||
'strings[blocks:block:' . $box2['delta'] . ':body]' => $translations['body']['es'],
|
||||
);
|
||||
$this->drupalPost(NULL, $edit, t('Save translation'));
|
||||
$this->i18nAssertTranslations($translations['title'], '', 'Updated block title translation displayed.');
|
||||
$this->i18nAssertTranslations($translations['body'], '', 'Updated block body translation displayed.');
|
||||
|
||||
// Test a block translation with filtering and text formats
|
||||
$box3 = $this->i18nCreateBlock(array(
|
||||
'title' => '<div><script>alert(0)</script>Title</script>',
|
||||
'body' => "Dangerous text\nOne line\nTwo lines<script>alert(1)</script>",
|
||||
));
|
||||
// This should be the actual HTML displayed
|
||||
$title = check_plain($box3['title']);
|
||||
$body = check_markup($box3['body'], $box3['format']);
|
||||
$this->drupalGet('');
|
||||
$this->assertRaw($title, "Title being displayed for default language: " . $title);
|
||||
$this->assertRaw($body, "Body being displayed for default language: " . $body);
|
||||
|
||||
// We add language name to the body just to make sure we get the right translation later
|
||||
// This won't work for block titles as they don't have input format thus scripts will be blocked by locale
|
||||
$translations = array();
|
||||
foreach ($this->getOtherLanguages() as $langcode => $language) {
|
||||
$translations[$langcode] = $box3['body'] . "\n" . $language->name;
|
||||
$filtered[$langcode] = check_markup($translations[$langcode], $box3['format']);
|
||||
}
|
||||
// We need to find the string by this part alone, the rest will be filtered
|
||||
$this->createStringTranslation('blocks', 'Dangerous text', $translations);
|
||||
// Check the right filtered strings are displayed
|
||||
$this->i18nAssertTranslations($filtered);
|
||||
|
||||
// Assert translatable descriptions.
|
||||
$this->drupalLogin($this->admin_user);
|
||||
$this->drupalGet('admin/structure/block/manage/system/powered-by/configure');
|
||||
$this->assertText(t('This block has generated content, only the title can be translated here.'));
|
||||
|
||||
$this->drupalGet('admin/structure/block/manage/system/navigation/configure');
|
||||
$this->assertText(t('To translate the block content itself, translate the menu that is being shown.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate block fields to all languages
|
||||
*/
|
||||
function i18nTranslateBlock($block) {
|
||||
$translations['title'] = $this->createStringTranslation('blocks', $block['title']);
|
||||
$translations['body'] = $this->createStringTranslation('blocks', $block['body']);
|
||||
return $translations;
|
||||
}
|
||||
/**
|
||||
* Test creating custom block (i.e. box), moving it to a specific region and then deleting it.
|
||||
*/
|
||||
function i18nCreateBlock($block = array(), $region = 'sidebar_first', $check_display = TRUE) {
|
||||
$this->drupalLogin($this->admin_user);
|
||||
// Add a new custom block by filling out the input form on the admin/structure/block/add page.
|
||||
$block += array(
|
||||
'info' => $this->randomName(8),
|
||||
'title' => $this->randomName(8),
|
||||
'i18n_mode' => I18N_MODE_LOCALIZE,
|
||||
'body' => $this->randomName(16),
|
||||
);
|
||||
$custom_block = array(
|
||||
'info' => $block['info'],
|
||||
'title' => $block['title'],
|
||||
'i18n_mode' => $block['i18n_mode'],
|
||||
'body[value]' => $block['body'],
|
||||
);
|
||||
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
|
||||
|
||||
// Confirm that the custom block has been created, and then query the created bid.
|
||||
$this->assertText(t('The block has been created.'), t('Custom block successfully created.'));
|
||||
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $block['info']))->fetchField();
|
||||
|
||||
// Check to see if the custom block was created by checking that it's in the database.
|
||||
$this->assertNotNull($bid, t('Custom block found in database'));
|
||||
|
||||
// Check that block_block_view() returns the correct title and content.
|
||||
$data = block_block_view($bid);
|
||||
$format = db_query("SELECT format FROM {block_custom} WHERE bid = :bid", array(':bid' => $bid))->fetchField();
|
||||
$this->assertTrue(array_key_exists('subject', $data) && empty($data['subject']), t('block_block_view() provides an empty block subject, since custom blocks do not have default titles.'));
|
||||
$this->assertEqual(check_markup($block['body'], $format), $data['content'], t('block_block_view() provides correct block content.'));
|
||||
|
||||
// Check if the block can be moved to all available regions.
|
||||
$block['module'] = 'block';
|
||||
$block['delta'] = $bid;
|
||||
$block['format'] = $format;
|
||||
$this->moveBlockToRegion($block, $region);
|
||||
|
||||
return $block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update block i18n mode
|
||||
*/
|
||||
function setBlockMode($block, $mode = I18N_MODE_LOCALIZE) {
|
||||
$edit['i18n_mode'] = $mode;
|
||||
$this->updateBlock($block, $edit);
|
||||
}
|
||||
/**
|
||||
* Update block visibility for languages
|
||||
*/
|
||||
function setBlockLanguages($block, $languages = array()) {
|
||||
$edit = array();
|
||||
foreach ($this->getEnabledLanguages() as $langcode => $language) {
|
||||
$edit["languages[$langcode]"] = in_array($langcode, $languages) ? TRUE : FALSE;
|
||||
}
|
||||
$this->updateBlock($block, $edit);
|
||||
}
|
||||
/**
|
||||
* Update block
|
||||
*/
|
||||
function updateBlock($block, $edit) {
|
||||
$this->drupalLogin($this->admin_user);
|
||||
$this->drupalPost('admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure', $edit, t('Save block'));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user