silverstripe - 如何在 Silverstripe 4 的 GridField DataColumn 中显示格式化的 HTML

标签 silverstripe silverstripe-4

我们有一个对象,它有两个字段 - 一个是 Text ,另一个是HTMLText :

private static $db = [
    'Question' => 'Varchar(255)',
    'Answer' => 'HTMLText'
];

我们在 Gridfield 中引用这个对象使用DataColumns :

$questionsGrid = GridField::create(
  'Questions', 'Questions', 
  $this->Questions(), 
  GridFieldConfig_RelationEditor::create()
);

$dataColumns = $questionsGrid->
  getConfig()->getComponentByType(GridFieldDataColumns::class);

$dataColumns->setDisplayFields([
        'Question' => 'Question',
        'Answer' => 'Answer'
    ]);
    $dataColumns->setFieldCasting([
        'Question' => 'Text',
        'Answer' => 'HTMLText'
    ]);

然而Answer列显示为原始 HTML - 带有可见标签且无格式。

<p>The answer to life the universe & everything is 42.</p><p>A second paragraph for good measure.</p>

我们如何显示Answer列是否采用 HTML 格式?

最佳答案

您可以对该列使用'HTMLFragment->RAW'

$dataColumns->setFieldCasting([
    'Question' => 'Text',
    'Answer' => 'HTMLFragment->RAW'
]);

关于silverstripe - 如何在 Silverstripe 4 的 GridField DataColumn 中显示格式化的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57750764/

相关文章:

php - Silverstripe 4 单元测试出现 "Couldn' t find object”错误,如何解决?

php - 如何在 silverstripe 数据扩展中自动发布图像

admin - 自动生成 SilverStripe 管理链接

Silverstripe 4 更改基本 URL

php - 升级到 Silverstripe 4.1.0 出现 fatal error : Class 'PageController' not found in ErrorPageController. php?

jquery - 默认情况下在 Silverstripe 的 ModelAdmin 中显示过滤器表单

PHP 银条 ORM : Duplicate key value violates unique constraint for DataObject write

filter - SilverStripe 3.1.x - DataList唯一字段值过滤器/如何使用ORM对分组列表中的项目进行计数?

php - 使用 ListBoxField 保存为 Many_many 关系

php - 未为类设置 Silverstripe table_name