-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_markup.module
113 lines (102 loc) · 3.38 KB
/
semantic_markup.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
<?php
/**
* Implements hook_process_html().
*/
function semantic_markup_process_html(&$variables) {
$rdfa_mappings = module_invoke_all('semantic_markup_mappings');
if (!empty($rdfa_mappings)) {
// Create DOM XPath object to process.
$dom = filter_dom_load($variables['page']);
$dom_xpath = new DOMXPath($dom);
foreach ($rdfa_mappings as $rdfa_mapping) {
switch ($rdfa_mapping['type']) {
case 'global':
// Process global mappings.
_semantic_markup_process_rdf_mappings($dom_xpath, $rdfa_mapping['mappings']);
break;
case 'node':
$node = menu_get_object();
if (isset($node->type) && $node->type == $rdfa_mapping['bundle']) {
// Pass node context for token replacement.
_semantic_markup_process_rdf_mappings($dom_xpath, $rdfa_mapping['mappings'], array('node' => $node));
}
break;
}
}
// Replace the page HTML with processed RDFa.
$variables['page'] = filter_dom_serialize($dom);
}
}
/**
* Process RDFa mappings.
*
* @param DOMXPath $dom_xpath
* DOMXPath to be processed.
* @param array $mappings
* rdfa xpath mappings to be processed.
* @param array $context
* context to be used for token replacement.
*/
function _semantic_markup_process_rdf_mappings($dom_xpath, $mappings, $context = array()) {
foreach ($mappings as $mapping) {
if ($elements = $dom_xpath->query($mapping['xpath'])) {
foreach ($elements as $element) {
if (!empty($mapping['element'])) {
// Inject new empty element if element name is specified.
$rdfa_element = _semantic_markup_inject_element($element, $mapping);
}
else {
// Add attributes to xpath element.
$rdfa_element = $element;
}
foreach ($mapping['attributes'] as $key => $config) {
$value = '';
if (!empty($config['value'])) {
// Use 'value' property if the configuration is an array.
$value = $config['value'];
}
elseif (is_scalar($config)) {
// If the config is a string, use as value.
$value = $config;
}
// Replace any tokens.
$value = token_replace($value, $context);
// Run through callback if set.
if (!empty($config['callback'])) {
$value = call_user_func($config['callback'], $value);
}
// Add attribute to dom element.
$rdfa_element->setAttribute($key, token_replace($value, $context));
}
}
}
}
}
/**
* Create and inject a new dom element.
*
* @param $ref_element
* Element to inject before or after.
* @param $mapping
* rdfa xpath mapping.
* @return $element
* Newly created element.
*/
function _semantic_markup_inject_element($ref_element, $mapping) {
$element = $ref_element->ownerDocument->createElement($mapping['element']);
// Inject new element before or after xpath element based on position config.
if (!empty($mapping['position']) && $mapping['position'] == 'after') {
// Position after.
if($ref_element->nextSibling) {
$ref_element->parentNode->insertBefore($element, $ref_element->nextSibling);
}
else {
$ref_element->parentNode->appendChild($element);
}
}
else{
// Position before.
$ref_element->parentNode->insertBefore($element, $ref_element);
}
return $element;
}