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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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

Image Map


Go to End


2 Posts   692 Views

Avatar
Stef87

Community Member, 66 Posts

19 September 2012 at 8:48pm

Edited: 20/09/2012 2:44am

Hi

I'm having trouble with something and it's just occurred to me that it may be impossible to implement.

I want to create an imagefield that only allows the user to select existing images. I'm currently trying to implement a dropdown of images but I'm having trouble.

Here's what I've got so far.

$images = DataObject::get("Image", "ParentID = 5");
	$images->toDropdownMap("ID", "Image", $emptyString = "--select--");

$imageField = new DropdownField('Photo', 'Choose an image', $images);

I tried a treedropdownfield but I need a thumbnail of the image, not just the title.

I also tried this.

class OneFolderImageField extends ImageField {
 
	function __construct($name, $title, $value = null, $form = null, $rightTitle = null, $folderName = null) {
		$folder = Folder::findOrMake($folderName);
		$this->folderName = $folderName;
		parent::__construct($name, $title, $value = null, $form = null, $rightTitle = null, $folder->Name);
	}
 
	public function EditFileForm() {
		$filter = create_function(
			'$item', '
			$folder = Folder::findOrMake(\''.$this->folderName.'\');
				return
					(in_array("Image", ClassInfo::ancestry($item->ClassName)) && $item->ParentID == $folder->ID)
					||
					(in_array("Folder", ClassInfo::ancestry($item->ClassName)) && $item->ID == $folder->ID)
		;');
		$form = parent::EditFileForm();
		$form->dataFieldByName('ExistingFile')->setFilterFunction($filter);
		return $form;
	}
 
}

$imageField = new OfferImageField('Photo', 'Photo', 'Photo', $this, 'Test', $folderName);

But that didn't work as I thought it would.

Thanks in advance.

Avatar
Stef87

Community Member, 66 Posts

20 September 2012 at 2:43am

Edited: 20/09/2012 2:43am

Correction

After many long hours of trying many different convoluted methods it turns out all I need to do to get rid of the upload from computer option was this.

$imageField->setCanUploadNewFile(false);

I found it buried here http://api.silverstripe.org/2.4/forms/fields-files/FileIFrameField.html#methodsetCanUploadNewFile

Works in 2.4.7