Hello! I am running a gaming community site for Guild Wars on Drupal 5.1. I am attempting to integrate gwbbcode, which is a script that aids in displaying skill builds. It was originally designed for phpbb, but it is adaptable. For a working example, please see this page:
If you click on the "show gwBBcode" button at the bottom of that page, you will see the text that was converted into those lovely graphical "skill bars." The function that executes this function relies on specific tags, so it easy to integrate builds along with other plaintext. The function that performs the translation from simple text code to beautiful HTML code is called "parse_gwbbcode()", and it is found in a file called gwbbcode.inc.php.
In order to use the code, I hacked a template's node.tpl.php to include these lines:
<?php
define('GWBBCODE_ROOT','/home/lennalf/public_html/test/gwbbcode');
define('GWBBCODE_URL','http://test.lennalf.com/gwbbcode');
include GWBBCODE_ROOT.'/gwbbcode.inc.php';
?>
and then replaced
<?php print $content ?>
with:
<?php $content2 = gwbbcodize($content); print $content2 ?>
(gwbbcodize points directly to parse_gwbbcode)
As a final step, I also edited page.tpl.php to include:
<?php echo
str_replace('{gwbbcode_root_path}','http://test.lennalf.com/gwbbcode',file_get_contents('/home/lennalf/public_html/test/gwbbcode/overall_header.tpl'));
?> ... which pulls in the necessary javascript and styles.
I am sure some of you are wincing right now, because this is probably the ugliest hack in the history of Drupal.
Long story short, the result is actually mostly functional. You can see http://test.lennalf.com/ for an example of the code's output. However, if you were to sign up and create your own post (please do if you care to), you would find that there is a glaring error. When editing the post, if one attempts to "Preview" the revision, the following error occurs:
Fatal error: Cannot redeclare parse_gwbbcode() (previously declared in /home/lennalf/public_html/test/gwbbcode/gwbbcode.inc.php:30) in /home/lennalf/public_html/test/gwbbcode/gwbbcode.inc.php on line 30
I am assuming that this is because node.tpl.php is being called twice in order to present the preview, which means it is also trying to include gwbbcode.inc.php twice, which means the parse_gwbbcode() function is being defined twice, which is a no-no. This is no doubt a result of trying to shove the function into the node template.
So, essentially I've gotten to the point where I could continue to hack up the script until it barely makes sense, or I can find the way to do this right. I am hoping that somebody here in the Drupal community will see what I've done here and educate me on the errors of my ways. I am not very familiar with the Drupal API and I'm not too great with php, so I would really appreciate some advice from an expert on how to properly integrate this php script. Ideally, the script would only execute against "content," as I don't want anybody trying to throw gwbbcode in titles or names or something silly like that.
(Ideally, I would make a module, but I know so little about the Drupal API that such a task seems impossible right now)
I eagerly await your replies. Thanks in advance!
Comments
Goggles!
Yes. That hack is indeed painful to look at.
The quick fix for you however is use include_once() - not include().
Beyond that, I think you would want to impliment this function as a filter. Filter modules are actually some of the easiest (after block modules) to understand.
A filter module can consist of one single function (that does three things - list, description and process) plus an info file.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks for the advice dman.
Thanks for the advice dman. I tried the include_once function, and it solved my problem. As far as setting up a filter, that is an interesting long-term project and I will be thinking about that in the future, so I appreciate the lead.
filter module works, but ...
Hi dman,
the filter modules are indee easy to understand somewhat. I've made a gwbbcode module and it works (which surprised me). But I have not solved one problem:
I need to do the same thing that lennalf already did.
Is there a nice way to alter the page.tpl.php of a theme, or can I somehow add the needed files by changing something in the gwbbcode.module?
This file "gwbbcode/overall_header.tpl" contains css AND JavaScript, so the functions drupal_add_css or drupal_add_js just don't match. Is there a similar way to include this file?
thx a lot
XPectIT
If you really need extra
If you really need extra stuff in the header, yes drupal_add_css and js are the ones. Do be careful to only include them where they are really needed, if possible.
If you have a problem editing the 'overall_header' into two logical bits (?) you can use drupal_set_header() instead.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
No luck this time
The 'overall_header' is shipped with the rest of the bundle of scripts. To split the file may be a problem when future releases change the file, because it would have to be done every time an update arrives.
I think the developer of the gwbbcode must divide this in two seperate files.
To store the content in a string, split it and call drupal_add_js/css is not possible because these functions only want the path to a file not a string as input.
The drupal_set_html_head($string) funtcion is called all the time. e.g. the frontpage has 5 contents with the gwbbcode_filter enabled, it puts the $string 5 times into the header.
drupal_set_header() I wasn't able to get working, it repeats "header may not contain more than a single header, new line detected". But I think its the same as the drupal_set_html_head(), it is calles multiple times.
Up to you.
Sorry, it was drupal_set_html_head() I was thinking of.
It's up to you to find an appropriate way to stop it adding too many times. I usually just set a
staticflag in my function where I do something like that.The proper add_js, add_css methods detect and prevent that from happening - if you want to do it by hand, you have to do the logic yourself.
If the system uses inline js and css I'd abstract them into include libs anyway, so that's up to you to manipulate if you really desire...
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Oh, I see.
Okay. I think pure-coding something around an existing filter (and the gwbbcode is nothing else) which was developed to integrate into other systems (phpbb2 in this case) and playing with some functions of drupal isn't getting me any further.
I am no programmer and don't have the time/interest to make it perfectly smooth. For example I don't know what you mean with "set a static flag in the function", but I think I should know if I want to write a good module to be of use for the community. :)
I've edited the page.tpl.php and my site works with it.
Big Thanks anyway.
XPectIT