2

We have an object which has two fields - one is Text, the other is HTMLText:

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

We are referencing this object in a Gridfield using 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'
    ]);

Yet the Answer column is displayed as raw HTML - with visible tags & no formatting.

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

How do we display the Answer column as formatted HTML?

BaronGrivet
  • 4,364
  • 5
  • 37
  • 52

2 Answers2

4

You can use 'HTMLFragment->RAW' for that column

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

If you want to modify a method on a DataObject subclass being rendered as a row in a GridField to achieve the same thing, you simply cast it as HTMLText:

    /**
     * @return HTMLText
     */
    public function ImageNice(): \HTMLText
    {
        $image = '<img src="/path/to/foo.png" />';
        return \DBField::create_field(\HTMLText::class, $image);
    }

theruss
  • 1,690
  • 1
  • 12
  • 18