Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Gridfield ignoring Config & Validators

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

Page: 1
Go to End
Author Topic: 579 Views
  • LinseyM
    Avatar
    Community Member
    99 Posts

    Gridfield ignoring Config & Validators Link to this post

    I've set up a gridfield, and there is an image upload field on it... wanted to set it so that only PNG files could be uploaded, and also to specify the upload folder for the files... but it's totally ignoring the validator and the config. I can upload any type of file, and they just go into assets/uploads default folder... argh!

    BannerImage.php:

    <?php

    class BannerImage extends DataObject {

       // objects fields
       public static $db = array(
          'Title' => 'Varchar(255)'
       );

       // One-to-one relationship with picture and list page
       public static $has_one = array(
          'Banner' => 'Image',
          'Homepage' => 'Homepage'
       );

       // Summary fields for CMS Gridlist
       public static $summary_fields = array(
          'Banner.StripThumbnail' => 'Image',
          'Title' => 'Title'
       );
       

       static $default_sort = "ID DESC";
       
       public function getCMSFields_forPopup() {

          // define picture field
          $bannerField = new UploadField('Banner', 'Banner');
    $bannerField->setFolderName('assets/Uploads/HomepageElements/Banners');
          $bannerField->getValidator()->setAllowedExtensions(array('png'));

          // Define all fields
          return new FieldList(
             new TextField('Title', 'Title'),
             $bannerField
          );
       }

    }

    and on Homepage.php:

    <?php
    /**
    * Defines the Homepage page type
    */

    class Homepage extends Page {

       // One to many relationship with banner object
       public static $has_many = array(
          'BannerImages' => 'BannerImage'
       );
       
    static $db = array(
    );

    static $has_one = array(
    );

       public static $many_many = array(
       );

    static $icon = "framework/docs/en/tutorials/_images/treeicons/home-file.gif";

       // Create Grid Field
       public function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->removeFieldFromTab("Root.Admin", "ShowInDropDown");
          $fields->removeFieldFromTab("Root.Admin", "ShowListings");
          $fields->removeFieldFromTab("Root.Admin", "CustomBanner");
          $fields->removeFieldFromTab("Root.Admin", "CategoryImage");
          
          $gridFieldConfig2 = GridFieldConfig::create()->addComponents(
             new GridFieldToolbarHeader(),
             new GridFieldAddNewButton('toolbar-header-right'),
             new GridFieldSortableHeader(),
             new GridFieldDataColumns(),
             new GridFieldPaginator(10),
             new GridFieldEditButton(),
             new GridFieldDeleteAction(),
             new GridFieldDetailForm()
          );
          
          $gridField = new GridField("BannerImages", "Banners:", $this->BannerImages(), $gridFieldConfig2);
          $fields->addFieldToTab("Root.Admin", $gridField);
          return $fields;
       }

    }

    class Homepage_Controller extends Page_Controller {
       
       
       function RandomSliders($num = 4) {
              return DataObject::get("BannerImage", "", "RAND()", "",$num);
       }

    }
    ?>

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Gridfield ignoring Config & Validators Link to this post

    Anyone got any suggestions? Ta, L xxx

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Gridfield ignoring Config & Validators Link to this post

    GridField doesn't use getCMSFields_forPopup. You just override getCMSFields(), use $fields = parent::getCMSFields() and add/remove/update fields from there.

    579 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.