-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage_id_filter.module
executable file
·156 lines (135 loc) · 4.38 KB
/
language_id_filter.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @file language_id_filter.module
*
* Text filter for title and identified text fields to auto expand language
* shorthand to full span text to satsify MVC rules
*
* [fr]TEST[/fr]
*
* becomes
*
* <span xml:lang="fr" lang="fr">TEST</span>
*
*/
/**
* Implements hook_help().
*/
function language_id_filter_help($path, $arg) {
switch ($path) {
case 'admin/help#language_id_filter':
return t("Text filter for title and identified text fields to auto expand language shorthand to full span text to satsify MVC rules.<br/>This filter should be at the very end since the html corrector filter can double up on the xml:lang attribute.");
}
}
/**
* Implements hook_element_info_alter().
*/
function language_id_filter_element_info_alter(&$types) {
foreach ($types as $type => &$info) {
$info += array('#pre_render' => array());
array_unshift($info['#pre_render'], 'language_id_filter_pre_render');
}
}
/**
* #pre_render callback for any element.
*/
function language_id_filter_pre_render($element) {
foreach (array('#title', '#description', '#markup') as $property) {
if (isset($element[$property])) {
$element[$property] = language_id_filter_filter_process($element[$property]);
}
}
return $element;
}
/**
* Implements hook_filter_info().
*/
function language_id_filter_filter_info() {
$filters['pirate'] = array(
'title' => t('MVC language filter'),
'description' => t('Text filter for title and identified text fields to auto expand language shorthand to full span text to satsify MVC rules.'),
'process callback' => 'language_id_filter_filter_process',
/*'settings callback' => 'language_id_filter_filter_settings',
'default settings' => array(
'language_id_filter_display_tip' => 1,
),*/
'tips callback' => 'language_id_filter_filter_tips',
);
return $filters;
}
/**
* Filter process callback for Pirate filter.
*/
function language_id_filter_filter_process($text) {
//return $text;
// Most of the following code is taken from Drupal core's Filter module
// in order to exclude text within tags, such as URLs that might get
// modified using the replacement patterns.
$ignore_tags = 'a|script|style|code|pre';
$open_tag = '';
$pattern_search = '/\[([a-zA-Z]{2,3})\](.*?)\[\/\1\]/';
$pattern_replace = '<span class="alt-lang lang-${1}" xml:lang="${1}" lang="${1}">${2}</span>';
return preg_replace($pattern_search, $pattern_replace, language_id_filter_fix_quotes($text));
}
function language_id_filter_filter_quick_process($text, $clean = FALSE) {
$pattern_search = '/\[([a-zA-Z]{2,3})\](.*?)\[\/\1\]/';
if ($clean) {
$pattern_replace = '${2}';
} else {
$pattern_replace = '<span class="alt-lang lang-${1}" xml:lang="${1}" lang="${1}">${2}</span>';
}
$text = preg_replace($pattern_search, $pattern_replace, $text);
$text = language_id_filter_fix_quotes($text);
if ($clean) {
return check_plain($text);
} else {
return $text;
}
}
function language_id_filter_fix_quotes($text) {
$text = preg_replace('/\*\*(.*?)\*\*/', '<strong>${1}</strong>', $text, 1);
$text = preg_replace('/\*(.*?)\*/', '<em>${1}</em>', $text, 1);
return strtr($text, array(
' :' => ' :',
'« ' => '« ',
' »' => ' »',
));
}
/**
* Filter tips callback for Pirate filter.
*/
/*function language_id_filter_filter_tips($filter, $format, $long = FALSE) {
if ($filter->settings['language_id_filter_display_tip']) {
return t('Avast! This website be taken over by pirates on September 19th. Yarr!');
}
}*/
/*
* Implements hook_tokens_alter
*
* Alter title token to remove tags
*/
function language_id_filter_tokens_alter(array &$replacements, array $context) {
/*$options = $context['options'];
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
*/
if ($context['type'] == 'node') {
//kpr($context);
//kpr($replacements);
$node = $context['data']['node'];
if (!isset($context['tokens']['title'])) {
$context['tokens']['title'] = '[node:title]';
}
$title = $node->title;
if (function_exists('language_id_filter_filter_quick_process')) {
$title = language_id_filter_filter_quick_process($title, TRUE);
}
$replacements[$context['tokens']['title']] = $title;
}
}