1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Display Suite Format
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_filter_info().
|
10
|
*/
|
11
|
function ds_format_filter_info() {
|
12
|
$filters['ds_code'] = array(
|
13
|
'title' => t('Display Suite evaluator'),
|
14
|
'description' => t('This filter will only work in the Display Suite text format, machine name is <em>ds_code</em>. No other filters can be enabled either.'),
|
15
|
'process callback' => 'ds_format_php_eval',
|
16
|
'tips callback' => 'ds_format_filter_tips',
|
17
|
'cache' => FALSE,
|
18
|
);
|
19
|
return $filters;
|
20
|
}
|
21
|
|
22
|
/**
|
23
|
* Tips callback for Display Suite php filter.
|
24
|
*/
|
25
|
function ds_format_filter_tips($filter, $format, $long = FALSE) {
|
26
|
global $base_url;
|
27
|
if ($long) {
|
28
|
$output = '<h4>' . t('Using custom code with Display Suite') . '</h4>';
|
29
|
$output .= t('Include <?php ?> tags when using PHP. The $entity object is available.');
|
30
|
return $output;
|
31
|
}
|
32
|
else {
|
33
|
return t('You may post Display Suite code. You should include <?php ?> tags when using PHP. The $entity object is available.');
|
34
|
}
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Wrapper function around PHP eval(). We don't use php_eval from
|
39
|
* the PHP module because custom fields might need properties from
|
40
|
* the current entity.
|
41
|
*
|
42
|
* @param $code
|
43
|
* The code to evaluate from the custom field.
|
44
|
* @param $object
|
45
|
* An object to use for evaluation.
|
46
|
* @return $output
|
47
|
* The output from eval.
|
48
|
*/
|
49
|
function ds_format_php_eval($code, $entity, $build = array()) {
|
50
|
global $theme_path, $theme_info, $conf;
|
51
|
|
52
|
// Store current theme path.
|
53
|
$old_theme_path = $theme_path;
|
54
|
|
55
|
// Restore theme_path to the theme, as long as ds_php_eval() executes,
|
56
|
// so code evaluted will not see the caller module as the current theme.
|
57
|
// If theme info is not initialized get the path from theme_default.
|
58
|
if (!isset($theme_info)) {
|
59
|
$theme_path = drupal_get_path('theme', $conf['theme_default']);
|
60
|
}
|
61
|
else {
|
62
|
$theme_path = dirname($theme_info->filename);
|
63
|
}
|
64
|
|
65
|
ob_start();
|
66
|
print eval('?>' . $code);
|
67
|
$output = ob_get_contents();
|
68
|
ob_end_clean();
|
69
|
|
70
|
// Recover original theme path.
|
71
|
$theme_path = $old_theme_path;
|
72
|
|
73
|
return $output;
|
74
|
}
|
75
|
|