phphyperlinksilverstripe

SilverStripe 3.1 GridField file link is re-written with HTML Entities


I'm very new to Silverstripe (3.1). I am using it to collect applications from users. Each user uploads a file which later in the CMS somebody can download. There is a has_one relationship for the file called 'Document.' I want to link to that file inside a GridField. So after some searching I did the solution below - easy, and it works except for one problem.

The link does appear inside the correct column in the GridField but it has been converted through something like HTMLSpecialChars() and I can see all the HTML. For the life me I cannot figure how to stop it. I would like to know where this conversion is taking place? and how can I circumvent it?

$submissionGrid = new GridField('submissions', 'Submissions', $submission, $config );

$submissionGrid->addDataFields(array(
        "Document" => function($row) {
            $link = '<a href="' . $row->Document()->getAbsoluteURL() . '">Download Document</a>';
            return $link;
        },
    ));

Solution

  • You are pretty close.

    Instead of addDataFields(), have you tried setFieldFormatting on the configuration of your gridfield?

    $submissionGrid = new GridField('submissions', 'Submissions', $submission, $config );
    $config = $submissionGrid->getConfig();
    $config->getComponentByType('GridFieldDataColumns')->setFieldFormatting(array(
        "Document" => function($value, $item) {
            $link = '<a href="' . $item->Document()->getAbsoluteURL() . '">Download Document</a>';
            return $link;
        },
    ));
    

    Depending on the fields available on the Submission Dataobject, if "Document" is something you are adding as a custom column to your gridfield, you will need to add it as well using setDisplayFields(). In this case, add this as well

    $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
        "Document" => "Link to document"
    ));