-
Notifications
You must be signed in to change notification settings - Fork 0
/
transclude_token.module
57 lines (45 loc) · 1.37 KB
/
transclude_token.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
<?php
/**
* @file
* The transclude_token module.
*/
function transclude_token_token_info() {
$type = array(
'name' => t('Transclude'),
'description' => t('Transclude content.'),
);
return array(
'types' => array('transclude' => $type),
);
}
function transclude_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
if ($type == 'transclude') {
foreach ($tokens as $name => $original) {
$replacements[$original] = get_transcluded_content($name);
}
}
return $replacements;
}
function get_transcluded_content($url) {
if (!strpos($url, '#')) {
drupal_set_message(t('Error: Transclusion token contains no anchor.'), 'error');
return '';
}
if (preg_match('/<\/a>/', $url)) {
@$dom = new DomDocument();
$dom->loadHTML($url);
$url = $dom->getElementsByTagName('a')->item(0)->nodeValue;
}
list($base, $anchor) = explode('#', $url);
$external_html = file_get_contents($base);
$dom = new DomDocument();
@$dom->loadHTML(file_get_contents($base));
$anchor_node = $dom->getElementById($anchor);
if ($anchor_node === NULL) {
drupal_set_message(t('Error: Anchor could not be found in external page.'), 'error');
return '';
}
return "<blockquote class='transclusion'>{$dom->saveHTML($anchor_node)}</blockquote>";
}