Jump to:

23358 Posts in 17875 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Add CMSThumbnail in SS3.0.1 !

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

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

Page: 1
Go to End
Author Topic: 928 Views
  • DesignerX.com.au
    Avatar
    Community Member
    107 Posts

    Add CMSThumbnail in SS3.0.1 ! Link to this post

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

    }

  • Hattori
    Avatar
    Community Member
    20 Posts

    Re: Add CMSThumbnail in SS3.0.1 ! Link to this post

    Replace

    return $Image->CMSThumbnail();

    By:

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

  • DesignerX.com.au
    Avatar
    Community Member
    107 Posts

    Re: Add CMSThumbnail in SS3.0.1 ! Link to this post

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

  • jak
    Avatar
    Community Member
    46 Posts

    Re: Add CMSThumbnail in SS3.0.1 ! Link to this post

    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.

  • DesignerX.com.au
    Avatar
    Community Member
    107 Posts

    Re: Add CMSThumbnail in SS3.0.1 ! Link to this post

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

    928 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.