The generalized custom module to achieve the original post’s #2: enforcing style rules on save in the backend editors. Read through the entire module before use, this one is risky.
I have tested this as best I can on dev, and I’ve just implemented it on our production server. While I’m mostly confident that it will work well, I have it running with replacement disabled, debug enabled to see what real editors might stumble on. I’d welcome anyone willing to give it a try on their own dev server and contribute to further testing.
In Beloit’s case, I have started with the following three rules:
<?php
$universal_rules = [
'class_year' => [ // enforce name and class year format: [Last ’00]
'regex' => '/(\w+)(?!(?: |\x20){1}(?:’|’|’)\d{2})(?: |\x20| | |\xA0|\xC2\xA0)*(?:\'|'|'|\‘|‘|‘|\’|’|’|\´|´|\`|`)(?: |\x20| | |\xA0|\xC2\xA0)*(\d{2})/u',
'replace' => '$1 ’$2'
],
'pearsons_hall' => [ // enforce format of name "Pearsons Hall"
'regex' => '/(?!Pearsons Hall)[Pp]earson(?:\'|'|'|\‘|‘|‘|\’|’|’|\´|´|\`|`)?s\s*[Hh]all/u',
'replace' => 'Pearsons Hall'
],
'single_space' => [ // enforce use of single spaces
'regex' => '/(?: |\x20| | |\xA0|\xC2\xA0){2,}/u', // normal or 'nbsp' spaces
'replace' => ' '
],
];
?>
For those writing rules, note that for characters that are represented in different formats (such as spaces, the various apostrophes, etc) require that specific format. I initially tried to use html_entity_decode
to avoid this complexity, but doing so meant that fields with widgets (or images) would become broken and cause the field to save nothing. In the end, I’ve opted to avoid the issue altogether in my rules with these complex looking (x|y|z)
sets to catch any format. I hope you all love complex REGEX…
Let me know if you have any questions or suggestions.
Thanks,
Nick