Module similar to Token but much simpler with custom replacement evaluating PHP function
Hi! I am new to this community.
Drupal is simply incredible I am really amazed!
Purpose: I want all the content published by users (comments, blog post, forum threads, etc) to be modified by my ParseText($content) function before being stored in the database, and to be modified by UnParseText($content) before being show to user for editing. So what I want is quite simple, is to be able to call those two functions, one on save event, and the other before show edit event.
ParseText would analize the content and do some very complex replacements using complex rules, so in this case I think that Token module is not sufficient.
Example: User enters "I like bananas as I am a monkey", however before publishing the string is modified by ParseText() which results in "I like bananas as I am a monkey". So when the content is published the special formatted text is displayed.
Now when the user goes and clicks on edit, the string "I like bananas as I am a monkey" saved in database is modified by UnParsetext() and the result is that the user gets to edit "I like bananas as I am a monkey" (original inputted string).
The problem is not about creating ParseText() nor UnParseText() but how to make them run on the appropiate events and modify the content.
Does what I need (adding calls to PHP functions on different hooks and modifying values) already exist?
Is it easy to accomplish what I am looking for? I have experience using PHP but zero experience on creating drupal modules.
Should I create a module for accomplishing my purpose?
Please point me to the right track :)
Thanks so much!
Rod

Oops... HTML tags got erased (again)
This time examples are with appropriate HTML tags.
Please read on. Thanks :)
____________________________
Hi! I am new to this community.
Drupal is simply incredible I am really amazed!
Purpose: I want all the content published by users (comments, blog post, forum threads, etc) to be modified by my ParseText($content) function before being stored in the database, and to be modified by UnParseText($content) before being show to user for editing. So what I want is quite simple, is to be able to call those two functions, one on save event, and the other before show edit event.
ParseText would analize the content and do some very complex replacements using complex rules, so in this case I think that Token module is not sufficient.
Example: User enters:
"I like bananas as I am a monkey"however before publishing the string is modified by ParseText() which results in:
"I like <span object="banana" color="yellow" taste="yummy" >bananas</a> as I am a monkey"So when the content is published the special formatted text is displayed.
Now when the user goes and clicks on edit, the string:
"I like<span object="banana" color="yellow" taste="yummy" >bananas</a>as I am a monkey"
"I like bananas as I am a monkey"The problem is not about creating ParseText() nor UnParseText() but how to make them run on the appropiate events and modify the content.
Does what I need (adding calls to PHP functions on different hooks and modifying values) already exist?
Is it easy to accomplish what I am looking for? I have experience using PHP but zero experience on creating drupal modules.
Should I create a module for accomplishing my purpose?
Please point me to the right track :)
Thanks so much!
Rod
P.S: Sorry for publishing almost the same post twice, but I couldn't find a way to edit the post :)
How about this?
Why don't you store unparsed text in the database, and only parse it on output?
That way, you can put the parsing in your template. For example, in node.tpl.php, where it says
<?phpprint $content;
?>
<?phpprint ParseText($content);
?>
[Drupal++]
Server Load
Hi dmitrig, Thanks for the reply! :)
I didn't know that thanks.
However I had thought about that- parsing on view and saving unparse on db, but actually I don't think is a good idea because my function ParseText is quite heavy on resources.
So:
1) Given the assumption that there are far more "simple views" of the page that there are 'edits' or 'publishing', parsing the text each time it has to be seen would mean more server load wasted, as i would have to be parsed every time a user sees the page right?
So putting a Parse and Unparse function before publishing and saving to db, and Unparsing before Editing, would mean less server load.
Any ideas for that? Thanks so much! :)
Sounds like you want to
Sounds like you want to create a new "filter", which you can then add to the "Input Format" that your users are using (like "Filtered HTML", "Full HTML", etc). Note: Drupal actually saves whatever the user enters as-written, then filters on _output_
You can see the existing Filters on the Add-Input-Format screen at admin/settings/filters/add
There is documentation on the hooks you need to implement (to define the filter) at http://api.drupal.org/api/file/developer/examples/filter_example.module/5
Yes!
That seems to be!
I am now reading the source code...
Thanks to it I got to find another one:
I'll be modifying the module I found : http://drupal.org/project/pirate :)
One question though: how does drupal filter work?
Does it save both filtered and unfiltered text on database, or does it 're-filter' the text upon every display?
If it's the second case... then we have the same problem I stated before.
The correct terminology for what I am looking for seems to be 'cache'.
Does there exist such thing as a "filter" cache? If not, how can I do that, so that the filtered text is actually saved on db so that it doesn't have to be parsed on every view?
Thanks!
Rod
Like I said, the filter
Like I said, the filter happens on output, not input: what the user enters is saved exactly in the DB. This is normally the best way to do things, since you can change your filter, without having to go and reprocess all your nodes. The filter system does cache this filtered version, so performance is generally not a problem.
Chapter 11 in "Pro Drupal Development" (book) is about the Filter System.
THANKS THANKS THANKS!
Yes! I am so happy, then I have a solution :)
Is that book found online too?
(By the way how can I mail subscribe to this help groups so that I don't have to check everytime for an answer?)
Thanks for everything,
Rod
You can buy the PDF of the
You can buy the PDF of the book online. See http://www.drupalbook.com/
I don't know how to subscribe to a thread in the forums.