HOWTO: Allow PHP in primary links in XTemplate
Last modified: August 23, 2009 - 21:21
NOTE: This works for Drupal 4.6.5; it should work for 4.6.6 as well
One of the most annoying things with XTemplate is the fact that it doesn't handle PHP code when it displays primary links. However, there are two functions found on php.net's user-submitted comments (http://us2.php.net/manual/en/function.eval.php) that can be added to the engine that helps fix this issue:
- Copy the following lines and paste at the very bottom of your xtemplate.engine file (located in /themes/engines/xtemplate); right before the PHP closing tag:
function eval_mixed_helper($arr){
return ("echo stripslashes(\"".addslashes($arr[1])."\");");
}
function eval_mixed($string){
$string = "<? ?>".$string."<? ?>";
$string = preg_replace("/<\?=\s+(.*?)\s+\?>/", "<? echo $1; ?>", $string);
$string = str_replace('?>', '', str_replace( array(' <?php', '<?'), ', preg_replace_callback( "/\?> ((.|\n)*?)<\?(php)?/","eval_mixed_helper",$string) ) );
return eval($string);
}
?>
- XTemplate uses theme_get_setting() to grab primary_links html markup and place it on a page. It's possible to place that html in a variable and run the variable though said functions. To do this, simply modify the following lines (starting from line 28-ish, depending on your version of Drupal):
$xtemplate->template->assign(array(
"language" => $GLOBALS['locale'],
"head_title" => implode(' | ', $head_title),
"head" => drupal_get_html_head(),
"styles" => theme_get_styles(),
"onload_attributes" => theme_onload_attribute(),
"primary_links" => theme_get_setting('primary_links'),
"secondary_links" => theme_get_setting('secondary_links')
));
to this...// allow for php in the primary links
ob_start();
eval_mixed(theme_get_setting('primary_links'));
$primary_link_eval = ob_get_clean();
$xtemplate->template->assign(array(
"language" => $GLOBALS['locale'],
"head_title" => implode(' | ', $head_title),
"head" => drupal_get_html_head(),
"styles" => theme_get_styles(),
"onload_attributes" => theme_onload_attribute(),
"primary_links" => $primary_link_eval,
"secondary_links" => theme_get_setting('secondary_links')
));
?></li>Now any php code you put in your primary links should work.
Note
Works on Drupal 4.6.6
You need to remove the space afterpreg_replace_callback( "/\?>on the first piece of code and of course?></li>at the end of the second.
