I have been experimenting Drupal for I think a month now. One problem that I have encountered with Drupal is not allowing to have HTML tags on the Title field when creating a new content. Drupal filters HTML tags for some reason, I guess. Anyways, I wouldn't want Drupal to do this so I searched for a solution and eventually was able to find one. There was actually a posted solution in the official website of Drupal which you can find here. The first function replaces the bb codes to html tags. While the second function strips off the bb codes.
The first thing I did was create the function on the file page.tpl.php which was, of course, a file located at the themes directory. What I did was use the bb code function to the variable $head and the strip function to $head_title. I also opened the node.tpl.php, declared the strip function, and used it to the $head variables once again. Everything was fine with the page.tpl.php file except for the node.tpl.php file. It had the error message that the strip function was already declared. It was then I found out that Drupal is calling the tpl files many times and that it was not advisable to declare functions in those files. I also found out that a certain file also from the themes folder is called by Drupal automatically. And this was the template.php file. So what I did is just declared the function over that file and voila! I have my own functions. Anyways, the template.php has a php start tag (<?php) but no end tag. This was intentional and not accidental. They say all Drupal files are like this. This is how my template.php looked like:
<?php
function returnTag($text) {
$bbcode = array(
'/\[br\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
);
$htmlcode = array(
'<br //>',
"<a href=\"$1\">$2</a>",
);
return preg_replace($bbcode, $htmlcode, $text);
// else return $text;
}
function stripTag($text) {
$bbcode = array(
'/\[br\]/is',
'/\[url\=(.*?)\]/is', '/\[\/url\]/is');
return preg_replace($bbcode, '', $text);
}
If you wish to add more bb codes, you can add regular expressions in the array.
This is actually a good way to not mess with the main theme files and just add your own functions on a specific file. If you think that you can only call these functions within the themes files, sad to say you're wrong. You can actually call these functions anywhere in the Drupal files (e.g. modules). But many say (including me), it's not advisable to do this -- I'm referring to messing around with the codes of other files of Drupal (e.g. modules) and adding your functions. Better to create some modules (which I don't know how to do yet) to alter the modules' functionalities.
3 comments:
April 19, 2010 at 7:30 PM
Thank you very much. Your information is very helpful.
Drupal Website Templates
June 8, 2013 at 5:01 AM
it is very helpful. i wanted a function too. but i can't run it. could you say about how to call a custom function a template file like node--video.tpl.php. i use these functions http://forrst.com/posts/Grab_Youtube_Vimeo_Embed_Thumbnail_PHP_Functio-rTL#comment-land . but i don't know how call it from node--video.tpl.php file.
June 9, 2014 at 10:30 PM
Hi there! Outstanding post. Thanks for making reference to a very interesting and useful content, it is a big help to me and to others as well, keep it up!
Website Design Companies Bangalore
Post a Comment