SilverStripe用户友好的HTML值在summary_field


SilverStripe user friendly HTML value in summary_field

我有一个DataObject和GridField,其中包含一个HTMLText数据类型字段。我希望在GridField中显示它的预览,从HTML中剥离出来。

非理想溶液

private static $summary_fields = array(
    'Title' => 'Title',
    'Content.Summary' => 'Content'
);

使用Content.Summary可以很好地从原始值中剥离HTML。然而,换行符也被删除,留下所有的行彼此连接,没有空格。

我怎么能剥离HTML,但保留换行完整(仍然隐藏HTML
标签)?

在摘要中保留
标签:

private static $summary_fields = array(
    'Title' => 'Title',
    'ContentSummary' => 'Content'
);
private static $casting = array(
    'ContentSummary' => 'HTMLText'
);
public function getContentSummary() {
    $content = $this->dbObject('Content');
    $value = str_replace('<br>', '__br__', $content->getValue());
    $content->setValue($value);
    return str_replace('__br__', '<br>', $content->Summary());
}