Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Add CMSThumbnail in SS3.0.1 !


Go to End


5 Posts   2211 Views

Avatar
DesignerX.com.au

Community Member, 107 Posts

1 August 2012 at 7:16pm

Hi all, i am trying to make a basic gallery & add the CMSThumbnail but its not working for me.. here is the coe below :

SGalleryImage.php
<?php

class SGalleryImage extends DataObject {
/*
public static $plural_name = 'Gallery Images';
public static $singular_name = 'Gallery Image';
*/

static $db = array(
'Name' => 'Varchar(255)',
'Caption' => 'Text',
'SortID' => 'Int'
);
static $has_one = array(
'Image' => 'Image',
'SGallery' => 'SGallery'
);
// Summary fields
public static $summary_fields = array(
'Name' => 'Name',
'Caption' => 'Caption',
//"Image => 'Image',
);

function SummaryOfImage() {
if ($Image = $this->Image())
return $Image->CMSThumbnail();
else
return '(No Image)';
}

}

SGallery.PHP
<?php

Class SGallery extends Page {

static $singular_name = 'SGallery';
static $plural_name = 'SGalleries';
static $icon = '';
static $db = array(
'GalleryDesc' => "Text"
);
static $has_one = array(
'Folder' => 'Folder',
'CoverImage' => 'Image'
);
public static $has_many = array(
'Images' => 'SGalleryimage'
);

//*
function getCMSFields() {
$fields = parent::getCMSFields();
/*
* Add extra basic fields to the CMS + Extra Tab
* $newFieldsArray = list of fields need to be added to tab
* $newFieldsArray MUST be used to add array of fields as fields need to be declared before they can be added to a tab !
*/

$newFieldsArray = array(
new UploadField("CoverImage"),
new TextareaField("GalleryDesc")
);
$fields->addFieldsToTab("Root.Settings", $newFieldsArray, "");

/*
* Add Gallery Section to the page using has_many relation + GridFeild
*/

$config = GridFieldConfig_RelationEditor::create();
$config->addComponents(
new GridFieldSortableRows($this->Images->SortID)
);
/*
*

$config->setDisplayFields(array(
'ID' => 'ID',
'Title' => 'Title'
));

*
*/
$field = new GridField("SGalleryimages", "SGallery images", $this->Images(), $config);
$fields->addFieldToTab("Root.GalleryImages", $field);

return $fields;
}

/*
* Attempt to create custom folder for each SGallerypage
function findOrMakeFolder() {
$Title = SiteTree::generateURLSegment($this->Title);
if ($this->Folder() && $this->Folder()->exists()) {
$folder = $this->Folder();
} else {
$folder = Folder::findOrMake($Title);
$folder->write();
}
$folder->Name =$Title;
$folder->Title = $Title;
$this->FolderID = $folder->ID;
$folder->write();
}
*
*/

function onBeforeWrite() {
parent::onBeforeWrite();
//$this->findOrMakeFolder();
}

function onBeforeDelete() {
parent::onBeforeDelete();
}

function ClassName() {
return __CLASS__;
}

}

class SGallery_Controller extends Page_Controller {

function init() {
parent::init();
}

function RenderGallery($TemplateName=NULL) {
$renderdImages = $this->data()->Images . renderWith;
return $renderdImages;
}

}

Avatar
Hattori

Community Member, 20 Posts

2 August 2012 at 5:53am

Replace

return $Image->CMSThumbnail(); 

By:

return $Image->setWidth(100); //100 or the value of the thumbnail width you want to set  

Avatar
DesignerX.com.au

Community Member, 107 Posts

5 August 2012 at 8:29pm

There is something wrong & when I try to show the thumbnail in the CMS , its escaping the html tags .
I found this in another gallery module ;)
This is for has_many : Gallery has_many Images so the thumbnails of the images will show in the Table .

$gridFieldConfig = GridFieldConfig_RelationEditor::create();
$gridField->getConfig()->getComponentByType('GridFieldDataColumns')->setFieldFormatting(array(
'Thumbnail' => function($val, $obj) {
if ($obj instanceof SGalleryImage) {
return $obj->getThumbnail();
} else {
return $obj;
// return var_dump($obj);
}
},
));

Avatar
jak

Community Member, 46 Posts

6 August 2012 at 1:29am

Edited: 06/08/2012 1:32am

If you are running 3.0.1, the setFieldFormatting workaround that you use should no longer be necessary. Try to add or set summary_fields in SGalleryImage:

public static $summary_fields = array(
	'Image.CMSThumbnail' => 'Image',
	// and all other columns, like:
	// 'Caption' => 'Caption',
);

If you want a small thumbnail, you can also use StripThumbnail instead of CMSThumbnail.

BTW: If you have content that you don't want to have escaped, you need to add a cast to HTMLText. You can do this via the static variable $casting

static $casting = array(
		'SomeField' => 'HTMLText',
	);

or by using setFieldCasting() on a GridFieldDataColumns instance.

Avatar
DesignerX.com.au

Community Member, 107 Posts

6 August 2012 at 1:52am

Edited: 06/08/2012 1:57am


Hi:
Just to confirm, this is using 3.0.1 :
Edited : I tried below, it works
public static $summary_fields = array(
'Image.CMSThumbnail' => 'Image', // here it work. I can also use CroppedImage or any other re-size method .
// and all other columns, like:
// 'Caption' => 'Caption',
);

It can also be done here, this will override settings from public static $summary_fields
$gridField->getConfig()->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'SortID' => 'SortID',
'Image.CMSThumbnail' => 'Image', // here it work. I can also use CroppedImage or any other re-size method .
'Title' => 'Title',
'ID' => 'ID'
));

Also, Nice tips about $casting;
)