View source
<?php
function language_sections_filter_tips($delta, $format, $long = false) {
$short_help = "Mark language-dependent sections with <strong>== lc ==</strong> where <em>lc</em> (or <em>lc-xx</em>) is a language code, <em>other</em> or <em>all</em>.";
$long_help = $short_help;
return t($long ? $long_help : $short_help);
}
function _language_sections_get_language() {
$versions = explode('.', VERSION, 2);
switch ($versions[0]) {
case 6:
case 7:
global $language;
return $language->language;
default:
global $locale;
return $locale;
}
}
function language_sections_filter($op, $delta = 0, $format = -1, $text = '') {
$mod_id = 'language_sections';
$mod_name = 'Language Sections';
$prefix = $mod_id . '_' . $format . '_';
$def_pattern = '/(=+ *([a-z]{2}|[a-z]{2}-[a-z]{2,5}|all|other) *=+\\s?)(.*?)/i';
switch ($op) {
case 'process':
$use_alt = variable_get($prefix . 'alt', FALSE);
$pattern = $use_alt ? variable_get($prefix . 'pattern', $def_pattern) : $def_pattern;
$n1 = 2;
$n2 = 2;
$n3 = 4;
$matches = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$current_language = strtolower(_language_sections_get_language());
$out = $matches[0];
$default = true;
for ($i = $n1; $i < count($matches); $i += $n3) {
switch (strtolower($matches[$i])) {
case $current_language:
$out .= $matches[$i + $n2];
$default = false;
break;
case 'qq':
case 'all':
$out .= $matches[$i + $n2];
break;
case 'qz':
case 'other':
if ($default) {
$out .= $matches[$i + $n2];
}
else {
$default = true;
}
break;
}
}
return $out;
case 'no cache':
return variable_get($prefix . 'cache', FALSE) == FALSE;
case 'list':
return array(
0 => t($mod_name),
);
case 'description':
return t('Allows you to define content for several languages in a single text area.');
case 'settings':
return _language_sections_settings($mod_id, $mod_name, $prefix, $def_pattern);
default:
return $text;
}
}
function _language_sections_settings($mod_id, $mod_name, $prefix, $def_pattern) {
$form[$mod_id] = array(
'#type' => 'fieldset',
'#title' => t($mod_name),
'#collapsible' => TRUE,
);
$field = $prefix . 'cache';
$form[$mod_id][$field] = array(
'#type' => 'checkbox',
'#title' => t("Allow caching"),
'#default_value' => variable_get($field, FALSE),
'#description' => t("Allow the filtered text to be cached by the Drupal core, which may improve site performance. NOTE: Depending on your site configuration, this may cause text for the wrong language to be displayed. Even if this is set, other modules might still prevent the text from being cached."),
);
$field = $prefix . 'alt';
$use_alt = variable_get($field, FALSE);
$form[$mod_id][$field] = array(
'#type' => 'checkbox',
'#title' => t("Use alternative pattern"),
'#default_value' => $use_alt,
'#description' => t("If set, {$mod_name} can be defined using the pattern given below. Otherwise, the default pattern will be used."),
);
$field = $prefix . 'pattern';
$form[$mod_id][$field] = array(
'#type' => 'textfield',
'#title' => t("Alternative pattern"),
'#size' => 64,
'#maxlength' => 128,
'#default_value' => $use_alt ? variable_get($field, $def_pattern) : $def_pattern,
'#description' => t("If enabled above, this pattern will be used for finding the {$mod_name} in the text. Initially, this is set to the {$mod_name} module's internal default. NOTE: You should not change the number of parenthesised groups in the expression."),
);
return $form;
}