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

对着背影说爱祢 提交于 2019-12-11 09:16:39

问题


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;
        },
    ));

回答1:


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"
));



回答2:


What actually worked:

I gave the right answer to jfbarrois for pointing me straight but thought I should post up the code that actually worked because it took me a while to find this answer.

It does have the inestimable advantage that it does actually work and a link is placed in a custom-formatted column in the GridField.

$config = GridFieldConfig_Base::create();

$config->getComponentByType('GridFieldDataColumns')->setDisplayFields($displayFields);
// Adding the custom named 'Download' column to the previously defined $displayFields
$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(
    array_merge($displayFields, array(
            "Download" => "Link to document"
        )
    ));

// Set the field formatting on the custom column inserting the real data from the 'Document' File Object
$config->getComponentByType('GridFieldDataColumns')->setFieldFormatting(array(
        "Download" => function($value, $item) {
            $link = '<a href="' . $item->Document()->getAbsoluteURL() . '">Download Document</a>';
            return $link;
        },
    ));

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


来源:https://stackoverflow.com/questions/28329984/silverstripe-3-1-gridfield-file-link-is-re-written-with-html-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!