t('Display Suite evaluator'),
'description' => t('This filter will only work in the Display Suite text format, machine name is ds_code. No other filters can be enabled either.'),
'process callback' => 'ds_format_php_eval',
'tips callback' => 'ds_format_filter_tips',
'cache' => FALSE,
);
return $filters;
}
/**
* Tips callback for Display Suite php filter.
*/
function ds_format_filter_tips($filter, $format, $long = FALSE) {
global $base_url;
if ($long) {
$output = '
' . t('Using custom code with Display Suite') . '
';
$output .= t('Include <?php ?> tags when using PHP. The $entity object is available.');
return $output;
}
else {
return t('You may post Display Suite code. You should include <?php ?> tags when using PHP. The $entity object is available.');
}
}
/**
* Wrapper function around PHP eval(). We don't use php_eval from
* the PHP module because custom fields might need properties from
* the current entity.
*
* @param $code
* The code to evaluate from the custom field.
* @param $object
* An object to use for evaluation.
* @return $output
* The output from eval.
*/
function ds_format_php_eval($code, $entity, $build = array()) {
global $theme_path, $theme_info, $conf;
// Store current theme path.
$old_theme_path = $theme_path;
// Restore theme_path to the theme, as long as ds_php_eval() executes,
// so code evaluted will not see the caller module as the current theme.
// If theme info is not initialized get the path from theme_default.
if (!isset($theme_info)) {
$theme_path = drupal_get_path('theme', $conf['theme_default']);
}
else {
$theme_path = dirname($theme_info->filename);
}
ob_start();
print eval('?>' . $code);
$output = ob_get_contents();
ob_end_clean();
// Recover original theme path.
$theme_path = $old_theme_path;
return $output;
}