Created
April 18, 2016 11:05
-
-
Save wernerkrauss/ec1730f10967bb373292e17852bb19fb to your computer and use it in GitHub Desktop.
Silverstripe extension to replace some stuff in $Title and other StringFields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by IntelliJ IDEA. | |
* User: Werner M. Krauß <werner.krauss@netwerkstatt.at> | |
* Date: 01.12.2015 | |
* Time: 15:36 | |
*/ | |
class NetwerkstattStringFieldExtension extends Extension { | |
private static $casting = [ | |
'Replace' => 'HTMLText' | |
]; | |
private static $replacements = [ | |
'(c)' => '©', | |
'(r)' => '<sup>®</sup>', | |
'(tm)' => '<sup>™</sup>', | |
'|' => '<br>' | |
]; | |
public function Replace() { | |
$replacements = Config::inst()->get('NetwerkstattStringFieldExtension', 'replacements'); | |
$search = array_keys($replacements); | |
$replace = array_values($replacements); | |
return str_replace($search, $replace, $this->owner->value); | |
} | |
} |
For a SS4 compatible and tested version see https://packagist.org/packages/wernerkrauss/ss-stringfield-replace
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can configure the replacements using the SilverStripe config API.