Reputation: 486
how do I populate a Gridfield With Images from an external Url?
I can populate the gridfield with the actual url text, or even something like <img src="...">
, but how do I render it so it actually shows the image?
class GalleryPage extends Page
...
$list = $this->getListofImages();
$grid = new GridField('GridFieldName', 'Gallery of', $list);
$config = $grid->getConfig();
$dataColumns = $config->getComponentByType('GridFieldDataColumns');
$dataColumns->setDisplayFields(array(
"Image" => "Image",
'Title' => 'Title', 'Tags' => "Tags",
'Url' => "Url", 'ThumbUrl' => 'ThumbUrl'));
$fields->addFieldsToTab('Root.ImportGallery', array( $grid ));
I'm only using this in the admin so I'm not using a template file. "Image" is where I want to show the actual image.
Upvotes: 2
Views: 152
Reputation: 486
Thanks to jfbarrois, here's the answer -
$dataColumns->setFieldFormatting(array(
"Image" => function($value, $item) {
return '<img src="' . $item->Image . '"/>';
}
));
$item->Image
here is a string of the url not an actual image. Could also have used $item->Url
or $item->ThumbUrl
also in the original code.
Upvotes: 1
Reputation: 989
You can try the following
$dataColumns->setFieldFormatting(array(
"Image" => function($value, $item) {
return '<img src="' . $item->getAbsoluteURL() . '"/>';
}
);
You might need to tweak how the url is pulled, depending on what $list is made of, but that is the idea.
Upvotes: 2