Modify content before it is saved to the database

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Cerulean
Forum Members
Forum Members
Posts: 172
Joined: Mon Nov 01, 2010 8:56 am

Modify content before it is saved to the database

Post by Cerulean »

I'd like to replace straight "dumb" quotes with curly “smart” quotes in my content blocks. There is an existing php tool that does this nicely, and it's essentially a powerful str_replace so for the sake of simplicity let's imagine the code is:

Code: Select all

str_replace("cat","dog",$raw_text);
I understand how I could use this in a UDT and pass content to it from my template - something like:

Code: Select all

$raw_text = $params['text'];
$new_text = str_replace("cat","dog",$raw_text);
return $new_text;
But this calls the UDT every time the page is loaded when I really only need the replacement to be done once. I think it would be better to send text to the UDT as it comes from Edit Content screen (before it is saved to the database), process the text in the UDT, and then save the modified text to the database. To do this I expect I need to attach my UDT to an event such as ContentEditPre, but I'm not sure how I get the $raw_text and how I save the $new_text to the database.

Also, can I process multiple content blocks through the UDT if there is more than one in a page?
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

Re: Modify content before it is saved to the database

Post by Jo Morg »

Installation wrote:With Smarty

If your program use the Smarty template engine, PHP SmartyPants can now be used as a modifier for your templates. Rename “smartypants.php” to “modifier.smartypants.php” and put it in your smarty plugins folder.
8)
Then you would be able to use it like this:

Code: Select all

{$foo|smartypants}
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Cerulean
Forum Members
Forum Members
Posts: 172
Joined: Mon Nov 01, 2010 8:56 am

Re: Modify content before it is saved to the database

Post by Cerulean »

Thanks for the reply.

I understand that I can use SmartyPants as a Smarty modifier, but that means processing the content at every page load - it works, sure, but it's unnecessary.

My question is about how I can process content through SmartyPants (or any UDT) at the time the content is saved to the database.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1973
Joined: Mon Jan 29, 2007 4:47 pm

Re: Modify content before it is saved to the database

Post by Jo Morg »

I just don't see the need for it when you have a cache... Content only has to be processed before caching.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Cerulean
Forum Members
Forum Members
Posts: 172
Joined: Mon Nov 01, 2010 8:56 am

Re: Modify content before it is saved to the database

Post by Cerulean »

You're probably right - I don't know enough about which aspects and combinations of database-calls/templates/tags/Smarty-modifiers are cached and which are not.

But processing content through a UDT at the time it is saved rather than the time it is fetched seems like a broadly useful technique so I'd be keen to hear about how it can be done.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Modify content before it is saved to the database

Post by calguy1000 »

Though it is possible to edit content before saving it to the database:

Something like this should work:

Code: Select all

$content_obj =& $params['content'];
$text = $content_obj->GetPropertyValue('content_en');
// do something with text
$text = $content_obj->SetPropertyValue('content_en',$text);
$text = $content_obj->GetPropertyValue('myblock');
// do something with text
$content_obj->SetPropertyValue('myblock',$text);
You probably don't want to do it in cases like this. Replacing quotes " with some kind of <span></span> or other element could cause problems on subsequent edits with the WYSIWYG editor. or cause problems with quotes inside smarty blocks, or manually inserted HTML. Also, undoing those changes isn't easily possible.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Cerulean
Forum Members
Forum Members
Posts: 172
Joined: Mon Nov 01, 2010 8:56 am

Re: Modify content before it is saved to the database

Post by Cerulean »

Thanks calguy, that works great!

One thing: this requires that I address each content block by name. Is there a way to iterate through all content blocks in a foreach loop so that content blocks can be added and removed from templates without needing to edit the UDT?
Post Reply

Return to “CMSMS Core”