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