-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessibletargetblankfilter.module
More file actions
89 lines (72 loc) · 3.28 KB
/
Copy pathaccessibletargetblankfilter.module
File metadata and controls
89 lines (72 loc) · 3.28 KB
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
<?php
/**
* @file
* Provides the ability for a link tags with target="_blank" to be
* automatically converted to a more accessible format, by alerting users via
* the use of text that the link will open in a new window.
*
* The text to be displayed can be changed via the filter settings form
*/
function accessibletargetblankfilter_init() {
drupal_add_css(drupal_get_path('module', 'accessibletargetblankfilter') . '/accessibletargetblankfilter.css');
drupal_add_js(drupal_get_path('module', 'accessibletargetblankfilter') . '/accessibletargetblankfilter.js', 'file');
}
/*
* sets the default text that the filter will insert
*
*/
function _accessibletargetblank_getDefaultText() {
return 'This link will open in a new window';
}
/**
* Implements hook_filter_info().
*/
function accessibletargetblankfilter_filter_info() {
$filters['accessibletargetblankfilter'] = array(
'title' => t('Accessible Target Blank Filter'),
'description' => t('Converts links that open in a new window and that have no title attribute to a more accessible format'),
'process callback' => '_accessibletargetblankfilter_process',
'default settings' => array(
'accessibletargetblankfilter_texttoadd' => _accessibletargetblank_getDefaultText(),
),
'settings callback' => '_accessibletargetblankfilter_settings',
'tips callback' => 'accessibletargetblankfilter_filter_tips',
);
return $filters;
}
/**
* Implements hook_filter_tips().
*/
function accessibletargetblankfilter_filter_tips($filter, $format, $long = FALSE) {
$textToAdd = isset($filter->settings['accessibletargetblankfilter_texttoadd']) ? $filter->settings['accessibletargetblankfilter_texttoadd'] : accessibletargetblank_getDefaultText();
if ($long) {
return t('Text to be added to the link tag to denote that it will open in a new window. The current text is ' . $textToAdd);
}
else {
return t('Text to be added to the link tag to denote that it will open in a new window. The current text is ' . $textToAdd);
}
}
/**
* Process function for the highlight filter.
*/
function _accessibletargetblankfilter_process($text, $filter) {
$textToAdd = isset($filter->settings['accessibletargetblankfilter_texttoadd']) ? $filter->settings['accessibletargetblankfilter_texttoadd'] : accessibletargetblank_getDefaultText();
$pattern = '~<a\s++[^>]*?\btarget\s*+=\s*+([\'"])_blank\1[^>]*+>\K((?:[^<]++|<++(?!/a>))*+)~is';
return preg_replace($pattern, "$2<span class=\"linkTargetBlankChild\">" . $textToAdd . "</span>", $text);
}
/**
* The settings form for filter.
*
* Default text is set above in the function... something like "This link will open in a new window"
*/
function _accessibletargetblankfilter_settings($form, &$form_state, $filter, $format, $defaults) {
$filter->settings += $defaults;
$settings['accessibletargetblankfilter_texttoadd'] = array(
'#title' => t('Text to add'),
'#description' => t('Text to be added to the link tag to denote that it will open in a new window'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => isset($filter->settings['accessibletargetblankfilter_texttoadd']) ? $filter->settings['accessibletargetblankfilter_texttoadd'] : $defaults['accessibletargetblankfilter_texttoadd'],
);
return $settings;
}