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.

Customising the CMS /

Gridfield ignoring Config & Validators


Reply


3 Posts   617 Views

Avatar
LinseyM

Community Member, 99 Posts

22 January 2013 at 4:57am

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

}
?>

Avatar
LinseyM

Community Member, 99 Posts

23 January 2013 at 10:38pm

Anyone got any suggestions? Ta, L xxx

Avatar
simon_w

Forum Moderator, 474 Posts

23 January 2013 at 11:52pm

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