Page 1 of 1
Modify content before it is saved to the database
Posted: Fri Nov 01, 2013 10:21 pm
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?
Re: Modify content before it is saved to the database
Posted: Fri Nov 01, 2013 11:15 pm
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.

Then you would be able to use it like this:
Re: Modify content before it is saved to the database
Posted: Fri Nov 01, 2013 11:34 pm
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.
Re: Modify content before it is saved to the database
Posted: Fri Nov 01, 2013 11:39 pm
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.
Re: Modify content before it is saved to the database
Posted: Sat Nov 02, 2013 12:05 am
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.
Re: Modify content before it is saved to the database
Posted: Sat Nov 02, 2013 12:51 am
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.
Re: Modify content before it is saved to the database
Posted: Sat Nov 02, 2013 1:24 am
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?