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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

question add image template - noob


Go to End


20 Posts   3778 Views

Avatar
wachupe

Community Member, 15 Posts

14 July 2011 at 6:40am

Edited: 14/07/2011 6:42am

Hi I'm new to using silverstripe and I have a question
how do I show all images that are inside a folder in the template, and that if I change an image by a tab
automaticamanete change to the template?

pd: sorry for my english :(

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2011 at 3:21am

I've done something like that to parse a CSV upload and automatically attach images to a dataobject. No real reason you couldn't do something like that right on a page object, though it may not be the most efficient way to do it.

First you need to add a has one relation to your DataObject ( in this case I assume it would be a page)

static $has_one = array (
		//Folder
		"ImageFolder" => "SiteTree"
	);

The CMSField would look something like this

$fields->addFieldToTab("Root.Main", new TreeDropdownField('ImageFolderID', 'Image Folder', "Folder"));

Basically you're adding a new drop down field with the names of only Folders

Then you'd need a function to get the images in that folder

function GetMyImages(){

$folderID=$this->ImageFolderID;
// get images in the folder $folderIF
$images = DataObject::get("File", "ParentID ='$folderID');

if ($images) {
return $images;
} else {
return false;
}

That should work, I haven't tested it though, just going off the top of my head. Like I said I was parsing a CSV file to attach specific images to a dataobject. You should be able to access that in a template with:

<% control GetMyImages %>
$Image
<% end_control %>

You may run into problems if you have non images in the folder and you may need to filter out the images in the $images dataobject

Avatar
wachupe

Community Member, 15 Posts

15 July 2011 at 5:15am

hI zenmonkey, thx for you reply

add the tab main in the site tree , but when I select the folder shows nothing

this is mi php

<?php
/**
 * Defines the HomePage page type
 */
 
class HomePage extends Page {
   static $db = array(
   );
   static $has_one = array(
   "ImageFolder" => "SiteTree" 
   );
   
   
   	public function getCMSFields() {		
		
		$fields = parent::getCMSFields();     
		
		 
		$fields->addFieldToTab("Root.Main", new TreeDropdownField('ImageFolderID', 'Image Folder', "Folder"));
		return $fields;
	}
}
  
class HomePage_Controller extends Page_Controller {

		function GetMyImages(){

		$folderID=$this->ImageFolderID; 
		// get images in the folder $folderIF 
		$images = DataObject::get("File", "ParentID =$3");

		if ($images) { 
		return $images; 
		} else { 
		return false; 
		}
	}	    
}

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2011 at 6:16am

You do have an error in your code (as does my original, i forgot the closing quote)

$images = DataObject::get("File", "ParentID =$3");

Should be
$images = DataObject::get("File", "ParentID ='$folderID'");

Beyond that I just realized GetMyImages doesn't return the DataObject as an Image and therefor doesn't generate the tag for you. You need to use $Tag instead of $Image in your template or build the tag yourself <img src="$Filename" width="$Width" height="$Height" />

This isn't ideal, but maybe someone else has an easier way of converting the DataObject into a set of Images. The Gallery module may have some hints

Avatar
wachupe

Community Member, 15 Posts

15 July 2011 at 7:43am

Edited: 15/07/2011 8:02am

when I select the folder "Uploads" shows nothing :'c
and in the template the control only show 3 items found within the assets folder and not in uploads.

<img src="/assets/error-404.html" alt="error-404.html" />
<img src="/assets/error-500.html" alt="error-500.html" />
<img src="/assets/Uploads/" alt="Uploads" />

These are the archives showing

pd: sry for the problems :'c

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2011 at 8:00am

Are there images in the folder? Personally I'd recommend creating a separate folder to draw from. And make sure if you've uploaded via FTP to go the Files and Images tab and search for new images. You could also change

$images = DataObject::get("File", "ParentID ='$folderID'");

to
$images = DataObject::get("File", "ParentID ='$folderID' AND ClassName='Image'");

That should restrict it to only looking for Images and prevent it returning any subfolders and other files

Avatar
wachupe

Community Member, 15 Posts

15 July 2011 at 8:40am

thx, this works!!! :D

have a question, when add the images for a tab, that displays a pop up, in this add the external link.

I must add the control to select the image that takes me to that address

Avatar
zenmonkey

Community Member, 545 Posts

15 July 2011 at 8:44am

Not sure what you mean. Sorry.

Do you want the images to link to external Sites?

Do you have an example published somewhere to help?

Go to Top