In Silverstripe 3, this would be best achieved by creating a DataExtension class (as opposed to overriding the class). (Note: this would be possible in 2.4.x, but the code would be completely different.)
Create a new class called TextFormatter that extends Extension :
class TextFormatter extends Extension { public function NL2BR() { return nl2br($this->owner->value); } }
Specify in the configuration that the Text class should be extended with the new class. This can be done either in the _config.php file, or (preferably) in the YAML file.
If you do not already have one, create a new file in mysite/_config/extensions.yml with the following contents (or you can add it to an existing file):
Text: extensions: ['TextFormatter']
It simply says, βExtend the Text class with the TextFormatter class,β which will make our new NL2BR function available to all Text objects.
Now in your templates you can simply call $OfficeAddr.NL2BR , and the output will be launched through your function before exiting.
Note that I assumed that your model uses Text as the type of the field, and not an HTMLText , as the previous answer suggested. If you use HTMLText , you can simply extend this class by modifying the extensions.yml file as needed.
source share