I could likely figure this out, but I don’t have the time to stumble through it at the moment. I’m hoping people have done some similar things and can give me a head start, or this may inspire someone else to try it for a similar need.
Administration has decided on a change to our styles, specifically how we append class years:
Before: Nicholas Mischler’14 - (attached, any single quote)
After: Nicholas Mischler ’14 - (spaced, closing single quote)
Given this change, I want to try and achieve two things:
Have a run-once module (or method) that will go through database content (stories, profiles, etc) and retroactively update as many instances as possible.
Have a module(s) that will check content changes on save and enforcing this style rule. (i.e. people can do the old way out of habit, but the end result will be the new way)
#1 would be great, but may be too risky and isn’t strictly necessary. #2 is the key item I’d like to establish that ensures consistency moving forward (and with some retroactive corrections on edited content).
Thus:
Has anyone done either of these things and have code snippets or advice to share?
Otherwise, any ideas on how best to do either or both?
I haven’t done either of those things specifically. But I have added code for this type of clean-up in the onOutput handler of a module. So the data may never actually be correct, but visitors will never know because it is corrected before they see it.
That may meet your needs just fine.
Here are a couple of examples:
public function onOutput($buffer) {
global $_LW;
// Remove hyphen from all instances of 'e-mail" (case-insensitive)
if ( preg_match('#(e)-(mail)#i', $buffer) ) {
$buffer = preg_replace('#(e)-(mail)#i', '$1$2', $buffer);
}
// Convert phone numbers from (###) ###-#### format to ###-###-####
if ( preg_match('#\(([0-9]{3})\)\s?([0-9]{3}-[0-9]{4})#', $buffer) ) {
$buffer = preg_replace('#\(([0-9]{3})\)\s?([0-9]{3}-[0-9]{4})#', '$1-$2', $buffer);
}
return $buffer;
}
As a +1 to what Jon mentions, we mostly see this kind of thing being done onOutput rather than on saving or search-and-replacing.
One important caveat: the preg_replace() action is a “heavy” one in terms of LW page processing, so like in Jon’s example, it’s essential that custom code be wrapped in a tightly-scoped strpos or preg_match scan so that the code only runs when it will find something to run on. This is a mistake I’ve made in the past and you’d be surprised how much badly-scoped preg_replace functions can slog down a server. Learn from past me’s mistakes!
@jwilcox Thanks for the examples; exactly what I was looking for (+/- the handler, but…)
@karl I recalled you saying that previously about preg functions, and that’s why I was thinking of this approach.
Put the “heavy” processing on editor steps, where it will have little to no additional impact on users’ experiences because it is correct to begin with.
Have the style be correct in the content that editors see and work with, as a subtle nudge/reminder of what our styles are. (i.e. bad style habits on web may translate to print, email, etc.)
This class year change specifically would be very common and numerous (i.e. 200+ instances in magazine class notes). I suspect an onOutput solution (without a retroactive step) could be excessively “heavy” given the sheer volume of instances.
If adjusting on save is a technical possibility, I’d still be interested in looking into it. I’d also be curious to learn what complications there are or could be in such an approach (i.e. lack of a handler, hard to check custom fields, etc.).
Sure thing – probably a good starting point is to experiment with using (onBeforeValidate)[onBeforeValidate - LiveWhale Support] to check the $_POST data for anything you want to change. If I’m not mistaken, the global variable $_LW->save_data is what gets used for saving, so any replacements or edits you make to that array inside of onBeforeValidate should carry through to the saved item. Hope this helps!